STL之list基本使用实例

list其实就是双向链表,好好复习list的使用。


Cplusplus描述的list方法:

STL之list基本使用实例_第1张图片

基本操作实例:


void test_list()
{
	list pList;
	cout << "push_back() , pop_back() ,begin() ,  end()测试:" << endl;
	pList.push_back(1); //尾插
	pList.push_back(2);
	pList.push_back(3);
	pList.push_back(4);
	pList.push_back(5);
	cout <<*( pList.begin() ) << endl;
	cout << (pList.back()) << endl; // end()返回最后一个元素后面的迭代器
	pList.pop_back();  //尾删
	cout << *(pList.begin()) << endl;
	cout << (pList.back()) << endl;
	pList.pop_front();  //头删
	cout << *(pList.begin()) << endl;
	cout << (pList.back()) << endl;
	
	cout << "rbegin() , rend() 测试 : " << endl;
	cout <<*( pList.rbegin() )<::iterator it = pList.begin();

		pList.insert(it, 8);

	for (it = pList.begin(); it != pList.end(); ++it)
	{
		cout << *it << "    ";
	}
	cout << endl;

	for (it = pList.begin(); it != pList.end(); )  //查看是否迭代器会失效
	{
		it = pList.erase(it );  //必须接受erase的返回值,否则出错咯
	}
	cout << pList.empty() << endl;


	pList.push_back(5);
	pList.push_back(3);
	pList.push_back(1);
	pList.push_back(7);
	pList.push_back(3);
	cout << "sort() , unique()  , remove() , splice()"<< endl;

	pList.sort();  //排序
	for (it = pList.begin(); it != pList.end(); ++it)
	{
		cout << *it << "    ";
	}
	cout << endl;

	pList.unique();  //去重!
	for (it = pList.begin(); it != pList.end(); ++it)
	{
		cout << *it << "    ";
	}
	cout << endl;

	pList.remove(7);  //移除具体的内容

	for (it = pList.begin(); it != pList.end(); ++it)
	{
		cout << *it << "    ";
	}
	cout << endl;
	it = pList.begin();
	//pList.splice  //现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list
	list list1;
	list1.push_back(0);
	list1.push_back(2);
	list1.push_back(4);
	pList.splice(it, list1);
	for (it = pList.begin(); it != pList.end(); ++it)
	{
		cout << *it << "    ";
	}
	cout << endl;
	cout<< list1.empty() << endl;
}



你可能感兴趣的:(C++基础)