"String Iterators Incompatible" 冒泡排序程序 运行 时的错误信息
"String Iterators Incompatible" error message when running bubble sort program
我是一个初学者,试图在 C++ 中对对象向量进行冒泡排序。我的目标是按每个对象元素的成员变量的成员变量对向量进行排序。所以最后,我希望所有矢量元素的属性都相同,只是按不同的顺序排序。当我 运行 程序时,我收到以下消息:
这是我的代码:
void sortInventory(vector<Vehicle> &carList)
{
bool swap;
Vehicle temp;
do
{
swap = false;
for (int count = 0; count < carList.size(); count++)
{
transform(carList[count].getVIN().begin(), carList[count].getVIN().end(), carList[count].getVIN().begin(), ::tolower);
if (carList[count].getVIN() > carList[count + 1].getVIN())
{
temp = carList[count];
carList[count] = carList[count + 1];
carList[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
这是我的 class 声明:
class Vehicle
{
private:
string VIN;
public:
string getVIN();
void setVIN(string);
};
这是我的 class 实现:
string Vehicle::getVIN()
{ return VIN; }
void Vehicle::setVIN(string input)
{ VIN = input; }
顺便说一句,我知道我没有使用有效的方法,但我才刚刚开始学习语言,我正在学习编写代码。
我问了一个与此类似的问题。然而,none 的答案让我到达了我想去的地方,尽管我觉得我的方向是正确的。
这行代码试图将 VIN 的字符串转换为小写文本,但失败了:
transform(carList[count].getVIN().begin(),
carList[count].getVIN().end(),
carList[count].getVIN().begin(),
::tolower);
每次调用 getVIN()
都会产生一个单独的 string
实例。由于迭代器不是来自同一个 string
实例,因此失败是结果。
您没有显示如何填充 carList
,但解决此问题的一种可能方法是在将 VIN 保存在 [=14] 中时将 VIN
保存为小写=].
正如 jxh 所说,你的转换行失败了,因为你正在制作迭代器来分隔字符串对象。为什么不尝试使转换成为一个单独的例程?
如果你想要花哨的,你可以在排序例程中将它定义为 lambda 函数。或者你可以让它成为一个单独定义的单独例程。
// returns a lower case version of the string
std::string lower_case(std::string VIN_number){
auto begin = std::begin(VIN_number);
auto end = std::end(VIN_number);
// Your code acting on one fixed string
std::transform(begin, end, begin, ::tolower);
return VIN_number;
}
然后当你进行比较时,做类似的事情
if ( lower_case(carList[count].getVIN()) > lower_case(carList[count + 1]).getVIN()) )
我是一个初学者,试图在 C++ 中对对象向量进行冒泡排序。我的目标是按每个对象元素的成员变量的成员变量对向量进行排序。所以最后,我希望所有矢量元素的属性都相同,只是按不同的顺序排序。当我 运行 程序时,我收到以下消息:
这是我的代码:
void sortInventory(vector<Vehicle> &carList)
{
bool swap;
Vehicle temp;
do
{
swap = false;
for (int count = 0; count < carList.size(); count++)
{
transform(carList[count].getVIN().begin(), carList[count].getVIN().end(), carList[count].getVIN().begin(), ::tolower);
if (carList[count].getVIN() > carList[count + 1].getVIN())
{
temp = carList[count];
carList[count] = carList[count + 1];
carList[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
这是我的 class 声明:
class Vehicle
{
private:
string VIN;
public:
string getVIN();
void setVIN(string);
};
这是我的 class 实现:
string Vehicle::getVIN()
{ return VIN; }
void Vehicle::setVIN(string input)
{ VIN = input; }
顺便说一句,我知道我没有使用有效的方法,但我才刚刚开始学习语言,我正在学习编写代码。
我问了一个与此类似的问题
这行代码试图将 VIN 的字符串转换为小写文本,但失败了:
transform(carList[count].getVIN().begin(),
carList[count].getVIN().end(),
carList[count].getVIN().begin(),
::tolower);
每次调用 getVIN()
都会产生一个单独的 string
实例。由于迭代器不是来自同一个 string
实例,因此失败是结果。
您没有显示如何填充 carList
,但解决此问题的一种可能方法是在将 VIN 保存在 [=14] 中时将 VIN
保存为小写=].
正如 jxh 所说,你的转换行失败了,因为你正在制作迭代器来分隔字符串对象。为什么不尝试使转换成为一个单独的例程? 如果你想要花哨的,你可以在排序例程中将它定义为 lambda 函数。或者你可以让它成为一个单独定义的单独例程。
// returns a lower case version of the string
std::string lower_case(std::string VIN_number){
auto begin = std::begin(VIN_number);
auto end = std::end(VIN_number);
// Your code acting on one fixed string
std::transform(begin, end, begin, ::tolower);
return VIN_number;
}
然后当你进行比较时,做类似的事情
if ( lower_case(carList[count].getVIN()) > lower_case(carList[count + 1]).getVIN()) )