顺序容器操作2 在顺序容器中添加元素

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <string>

using namespace std;

int main()
{
	vector<string> svec;
	list<string>   slist;
	deque<string> sdeq;

	svec.push_back("xiao");
	svec.push_back("cui");
	svec.push_back("ai");

	slist.push_back("shi");
	slist.push_front("wo");

	sdeq.push_front("C++");

	for (list<string>::iterator iter = slist.begin(); iter != slist.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;

	list<string>::iterator it = slist.begin();
	cout << *it << endl;

	slist.insert(it, "Hello");//insert 插入,在it迭代器的前面插入,
	for (list<string>::iterator iter = slist.begin(); iter != slist.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;

	slist.insert(it, 8, "Hello");  //在it迭代器前边插入8个Hello,
	for (list<string>::iterator iter = slist.begin(); iter != slist.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;

	string ing[6] = { "yes", "xiao", "cui", "ai", "ni", "sure" };
	it = slist.begin();
	++it;
	cout << *it << endl;
	slist.insert(it, ing+1, ing+5); // 在it迭代器的前边加入ing【】迭代器的从第二个字符到第五个字符,不包括第六个字符,
	for (list<string>::iterator iter = slist.begin(); iter != slist.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;   // 输出的是Hello xiao cui ai ni Hello Hello Hello Hello Hello Hello Hello Hello wo shi, 

	list<int> ilist;
	ilist.push_back(100);
	ilist.push_back(200);

	list<int>::iterator first = ilist.begin();
	while (first != ilist.end())
	{
		ilist.insert(first, 20);
		++first;
	}
	for (list<int>::iterator i = ilist.begin(); i != ilist.end(); ++i)
	{
		cout << *i << " ";
	}
	cout << endl;  //输出的是 20 100 20 200,


	return 0;
}


你可能感兴趣的:(顺序容器操作2 在顺序容器中添加元素)