我是靠谱客的博主 娇气网络,这篇文章主要介绍c++ iterator it,现在分享给大家,希望可以做个参考。

迭代器用法:

map迭代器

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream> #include<map> using namespace std; int main() { map<int,int> a; a[1]=2; a[3]=4; a[5]=6; a[9]=10;//a[7]和a[9]是乱序的遍历时map会自动排序 a[7]=8; map<int,int>::iterator it;//使用迭代器 for(it=a.begin();it!=a.end();it++) { cout<<"a["<<it->first<<"]="<<it->second<<endl; } //it->first是下标,it->second是值 }

运行结果:

复制代码
1
2
3
4
5
6
a[1]=2 a[3]=4 a[5]=6 a[7]=8 a[9]=10

vector迭代器

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream> #include<vector> using namespace std; int main() { vector<int> a(5); a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5; vector<int>::iterator it; for(it=a.begin();it!=a.end();it++) cout<<*it<<endl; }

运行结果:

复制代码
1
2
3
4
5
6
1 2 3 4 5

最后

以上就是娇气网络最近收集整理的关于c++ iterator it的全部内容,更多相关c++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部