作为一个新手,我正在尝试使用list-class在C++中实现排序功能.但是,运行代码我得到的错误是列表迭代器不可递增...但是它似乎不太可能,因为它应该是可递增的!
码:
void shuffle (list<int> &list1)
{
list<int> smaller;
list<int> larger;
if (list1.size() > 1)
{
list<int>::iterator it;
//int it;
int x = list1.front();
for (it = list1.begin(); it != list1.end(); it++)
{
if(*it <= x)
{
smaller.push_front(*it);
list1.pop_front();
}
else
{
larger.push_back(*it);
list1.pop_front();
}
shuffle (smaller);
shuffle (larger);
}
}
else
{
print(smaller);
print(larger);
//cout << "No sorting needed! The list still looks like: ";
//print(list1);
}
print(smaller);
print(larger);
}
Run Code Online (Sandbox Code Playgroud)
我在de CPP文件中主要实现了这个功能.
有人有什么建议吗?
jal*_*alf 13
您对list1.pop_front()的调用将删除迭代器最初指向的元素,使其无效.无效的迭代器无法递增.:)
用调试器找了几分钟.当你逐步完成程序时,请注意'它'的价值.我不知道你是否知道如何使用调试器,但如果没有,请自己帮忙并学习它.这是一个非常宝贵的工具.
(顺便说一句,将来,请明确说明错误是在编译时还是在运行程序时发生的.你的问题说明了"编译程序"时发生的错误.我刚刚为你编辑了这个问题,希望你不介意.但这是一个重要的区别,并且更难以准确地回答你的问题)
归档时间: |
|
查看次数: |
11593 次 |
最近记录: |