【发布时间】:2012-10-11 02:47:24
【问题描述】:
所以增加或减少 end() 迭代器是在标准中定义的?在 linux 上,begin() 被实现为 end()++。
#include <list>
#include <iostream>
int main()
{
std::list<int> numbers;
for (int i = 0; i < 10; i++)
numbers.push_back(i);
auto it = numbers.begin();
int count = 3;
while (count)
{
std::cout << *it++;
if (it == numbers.end())
{
++it; // is this ok ???
--count;
std::cout << '\n';
}
}
}
所以每个平台上的输出总是相同的?
输出:
0123456789
0123456789
0123456789
【问题讨论】:
-
将
++it;更改为it = numbers.begin();并且您已经定义了行为。