1 | std::vector<int> v; |
结果是 1 3 5 7 9
常见iterator自增,如果多增加,可能报错1
2
3
4
5
6
7
8
9
10
11
12std::vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
{
it = it + 5;
cout << *it << endl;
}
这样的程序是错的,会越界。 应该改成这样1
2
3
4
5
6for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
{
int step = v.end() - it;
it = it+step/5;
cout << *it << endl;
}