c++ STL迭代器实例
【摘要】 1、vector
#include <iostream>#include <vector> using namespace std; int main(int argc, char* argv[]){ // Create and populate the vector vector<int> vecTemp; for (int i = 0...
1、vector
-
#include <iostream>
-
#include <vector>
-
-
using namespace std;
-
-
int main(int argc, char* argv[])
-
{
-
// Create and populate the vector
-
vector<int> vecTemp;
-
for (int i = 0; i<6; i++)
-
{
-
vecTemp.push_back(i);
-
}
-
-
// Display contents of vector
-
cout <<"Original deque: ";
-
vector<int>::iterator it;
-
for (it = vecTemp.begin(); it!=vecTemp.end(); it++)
-
{
-
cout <<*it <<" ";
-
}
-
-
return 0;
-
}
-
-
/*
-
输出结果:
-
Original deque: 0 1 2 3 4 5
-
*/
2、deque
-
#include <iostream>
-
#include <deque>
-
-
using namespace std;
-
-
int main(int argc, char* argv[])
-
{
-
// Create and populate the deque
-
deque<int> dequeTemp;
-
for (int i = 0; i<6; i++)
-
{
-
dequeTemp.push_back(i);
-
}
-
-
// Display contents of deque
-
cout <<"Original deque: ";
-
deque<int>::iterator it;
-
for (it = dequeTemp.begin(); it != dequeTemp.end(); it++)
-
{
-
cout <<*it <<" ";
-
}
-
cout <<endl;
-
-
return 0;
-
}
-
/*
-
输出结果:
-
Original deque: 0 1 2 3 4 5
-
*/
3、list
-
#include <iostream>
-
#include <list>
-
-
using namespace std;
-
-
int main(int argc, char* argv[])
-
{
-
// Create and populate the list
-
list<int> listTemp;
-
for (int i = 0; i<6; i++)
-
{
-
listTemp.push_back(i);
-
}
-
-
// Display contents of list
-
cout << "Original list: ";
-
list<int>::iterator it;
-
for (it = listTemp.begin(); it != listTemp.end(); it++)
-
{
-
cout << *it << " ";
-
}
-
cout << endl;
-
-
// Insert five 9 into the list
-
list<int>::iterator itStart = listTemp.begin();
-
listTemp.insert(itStart,5,9);
-
-
// Display the result
-
cout << "Result of list: ";
-
for (it = listTemp.begin(); it != listTemp.end(); it++)
-
{
-
cout << *it << " ";
-
}
-
cout << endl;
-
-
return 0;
-
}
-
/*
-
输出结果:
-
Original list: 0 1 2 3 4 5
-
Result of list: 9 9 9 9 9 0 1 2 3 4 5
-
*/
4、set
-
#include <iostream>
-
#include <set>
-
-
using namespace std;
-
-
int main(int argc, char* argv[])
-
{
-
// Create and populate the set
-
set<char> setTemp;
-
for (int i = 0; i<6; i++)
-
{
-
setTemp.insert('F'-i);
-
}
-
-
// Display contents of set
-
cout <<"Original set: ";
-
set<char>::iterator it;
-
for (it = setTemp.begin(); it != setTemp.end(); it++)
-
{
-
cout <<*it <<" ";
-
}
-
cout <<endl;
-
-
return 0;
-
}
-
/*
-
输出结果:
-
Original set: A B C D E F
-
*/
5、map
-
#include <iostream>
-
#include <map>
-
-
using namespace std;
-
-
typedef map<int, char> MyMap;
-
-
int main(int argc, char* argv[])
-
{
-
// Create and populate the map
-
MyMap mapTemp;
-
for (int i = 0; i<6; i++)
-
{
-
mapTemp[i] = ('F'-i);
-
}
-
-
// Display contents of map
-
cout <<"Original map: " <<endl;
-
MyMap::iterator it;
-
for (it = mapTemp.begin(); it != mapTemp.end(); it++)
-
{
-
cout << (*it).first << " --> ";
-
cout << (*it).second << std::endl;
-
}
-
cout <<endl;
-
-
return 0;
-
}
-
/*
-
输出结果:
-
Original map:
-
0 --> F
-
1 --> E
-
2 --> D
-
3 --> C
-
4 --> B
-
5 --> A
-
*/
文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。
原文链接:fantianzuo.blog.csdn.net/article/details/106273773
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)