c++ STL迭代器实例

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

    // Display contents of vector
	cout <<"Original deque: ";    
	vector::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 
#include 

using namespace std;

int main(int argc, char* argv[])
{
	// Create and populate the deque
	deque dequeTemp;
	for (int i = 0; i<6; i++)
	{
		dequeTemp.push_back(i);
	}

	// Display contents of deque
	cout <<"Original deque: ";
	deque::iterator it;
	for (it = dequeTemp.begin(); it != dequeTemp.end(); it++)
	{
		cout <<*it <<" ";
	}
	cout <

 

3、list

#include 
#include 

using namespace std;

int main(int argc, char* argv[])
{
	// Create and populate the list
	list listTemp;
	for (int i = 0; i<6; i++)
	{
		listTemp.push_back(i);
	}

	// Display contents of list
	cout << "Original list: ";
	list::iterator it;
	for (it = listTemp.begin(); it != listTemp.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	// Insert five 9 into the list
	list::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 
#include 

using namespace std;

int main(int argc, char* argv[])
{
	// Create and populate the set
	set setTemp;
	for (int i = 0; i<6; i++)
	{
		setTemp.insert('F'-i);
	}

	// Display contents of set
	cout <<"Original set: ";
	set::iterator it;
	for (it = setTemp.begin(); it != setTemp.end(); it++)
	{
		cout <<*it <<" ";
	}
	cout <

 

5、map

#include 
#include 

using namespace std;

typedef map 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: " < ";
		cout << (*it).second << std::endl;
	}
	cout < F
1 --> E
2 --> D
3 --> C
4 --> B
5 --> A
*/

你可能感兴趣的:(cpp)