关于iterator迭代器的问题

书上说,list的迭代器只能进行“++”或“——”操作,不能进行+n或-n的操作,因为元素的位置并不是物理相连的。。

好吧,那就弄个for循环来实现+n的效果。

下面是我的代码:

 1 #include<iostream>
 2 #include<list>
 3 using namespace std;
 4  5 int main()
 6 {
 7     int m,n;
 8     cin>>m>>n;
 9     list<int> l;
10     for(int i=1; i<=m; i++)
11     {
12         l.push_back(i);
13     }
14     list<int>::iterator it=l.begin();
15     for(int i=1; i<=n; i++,it++)cout<<*it<<endl;;
16     cout<<*it<<endl;
17     18 19     return 0;
20 }

输入

20 5

输出

1

2

3

4

5

6

真是奇了怪了!!!!

(11.7)

(⊙o⊙)…知道怎么回事了,一个关于for循环很简单的问题。。

 例如:for(i=1; i<=10; i++) 语句; 上例中先给i赋初值1, 判断i是否小于等于10, 若是则执行语句, 之后值增 加1

搞清楚它的执行顺序——先for循环内部执行语句,再执行自增语句!!!

原文地址:https://www.cnblogs.com/fengyanlover/p/4943425.html