您的位置:首页 > 其它

遇到的一个关于自增的小问题、、

2012-10-09 19:50 162 查看
# include <iostream>
# include <vector>
# include <list>
# include <deque>

using namespace std;

int main (void)
{
list<int> ilist(10, 2);

list<int>::iterator front = ilist.begin(), back = ilist.end();

while (back-- != front)
{
cout << *back << endl;
}
return 0;
}


程序功能:倒序输出list容器中的值。

但输出值后出现错误。

当改成

while (back != front)
{
--back;
cout << *back << endl;
}


后,程序正常运行。

因为back--:先保存back的值,把back减一,在把back先前保存的值和front比较,而back减一后超出了list的范围。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: