c++ STL迭代器实例

   日期:2020-05-25     浏览:87    评论:0    
核心提示:1、vector#include #include using namespace std;int main(int argc, char* argv[]){ // Create and populate the vector vector vecTemp; for (int i = 0; i<6; i++) { vecTemp.push_back(i); } // Disc/c++

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;
}


 

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;
}

 

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;
}

 

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;
}

 

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;
}
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服