STL list型容器的使用

#pragma warning(disable:4786)

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

using namespace std;

template<class T> void print(const T& Ele)
{
	cout << " " << Ele << ";" << endl;
}

void Print_D(double& Ele)
{
	cout.width(5);
	cout.precision(1);
	cout << std::fixed << Ele << ", ";
}

void Print_I(int& Ele)
{
	cout << Ele << ", ";
}

int main()
{
	/************************************************************************/
	/* 2种list型容器的定义形式                                              */
	/************************************************************************/
	list<string> mylist_string;
	list<double> mylist_double(6);

	/************************************************************************/
	/*push_front在容器对象的首端添加元素                                    */
	/*push_back 在容器对象的末端添加元素                                    */
	/*pop_front 在容器对象的首端移除元素                                    */
	/*pop_back  在容器对象的末端移除元素                                    */
	/************************************************************************/
	mylist_string.push_front("1: Jack");
	mylist_string.push_front("2: Tom");
	mylist_string.push_front("3: Mike");

	mylist_double.push_front(10.0);
	mylist_double.push_front(20.0);
	mylist_double.push_front(30.0);
	mylist_double.push_front(40.0);
	mylist_double.push_front(50.0);

	/************************************************************************/
	/* 3种list型容器的定义形式                                              */
	/************************************************************************/
	list<int> mylist_int(6, 0);
	list<double> mylist_double2(6, 0.0);
	list<int> else_list(mylist_int);
	list<double> Iterlist(mylist_double.begin(), mylist_double.end());

	/************************************************************************/
	/* 各容器的容量                                                         */
	/* resize的参数比原本的size返回的值小,末端数据被删除                   */
	/* resize的参数比原本的size返回的值大,末端数据默认初始化               */
	/************************************************************************/
	int size = mylist_string.size();
	int max_size = mylist_string.max_size();
	mylist_string.resize(5);

	size = mylist_double.size();
	max_size = mylist_double.max_size();
	mylist_double.resize(5);

	size = mylist_double2.size();
	max_size = mylist_double2.max_size();
	mylist_double2.resize(5);

	size = Iterlist.size();
	max_size = Iterlist.max_size();
	Iterlist.resize(5);

	size = mylist_int.size();
	max_size = mylist_int.max_size();
	mylist_int.resize(5);

	size = else_list.size();
	max_size = else_list.max_size();
	else_list.resize(5);

	/************************************************************************/
	/* 使用迭代器相关的函数                                                 */
	/************************************************************************/
	list<double>::iterator Iter_D;
	list<double>::reverse_iterator Iter_rD;

	double tmp = 0.0;
	Iter_D = mylist_double.begin();
	tmp = *Iter_D;
	cout << "The beginning element of the mylist_double: " << endl;
	cout << tmp << endl;

	Iter_rD = mylist_double.rbegin();
	tmp = *Iter_rD;
	cout << "The reverse beginning element of the mylist_double: " << endl;
	cout << tmp << endl;

	/************************************************************************/
	/*front返回容器对象第一个元素                                           */
	/*back返回容器对象最后一个元素                                          */
	/************************************************************************/
	tmp = mylist_double.front();
	cout << "The front element of the mylist_double: " << endl;
	cout << tmp << endl;

	tmp = mylist_double.back();
	cout << "The back element of the mylist_double: " << endl;
	cout << tmp << endl;

	/**************************************************************************************/
	/* 元素重置                                                                           */
	/* assign(5, 5.6),清除容器对象的元素,将5个值5.6重置给容器对象                        */
	/* assign(list_one.begin(), list_one.end()) 清除容器对象的元素,迭代器范围重置        */
	/**************************************************************************************/
	list<double> list_one, list_two, list_three;
	list_two.push_back(50.0);
	list_three.push_back(100.0);
	double td = 0.0;
	for (int i = 0; i < 10; i++)
	{
		td = i+i/10.0;
		list_one.push_back(td);
	}
	list_two.assign(5, 5.6);
	list_three.assign(list_one.begin(), list_one.end());

	/************************************************************************/
	/* 交换两个list对象的内容,容器的大小会变化                             */
	/************************************************************************/
	list_one.swap(list_two);

	/**************************************************************************************/
	/* 元素的插入和删除                                                                   */
	/* insert(it, const T& value) 在迭代器it指向的位置之前插入值value                     */
	/* insert(it, n, const T& value)在迭代器it指向的位置之前插入n个元素值value            */
	/* insert(it, n, const itb, const itl)在迭代器it指向的位置之前插入迭代器范围内的元素  */
	/* erase(it) 删除迭代器it指向的元素                                                   */
	/* erase(first, last) 删除迭代器范围的元素                                            */
	/**************************************************************************************/
	list<int> mylt;
	list<int> mylttmp(5, 0);
	list<int>::iterator it;
	list<int>::iterator itNext;
	for (int i = 0; i < 20; i++)
	{
		mylt.push_back(i+i*3);
	}
	it = mylt.begin();
	mylt.insert(++it, 100);
	it = mylt.begin();
	mylt.insert(it, 5, 30);
	it = mylt.begin();
	mylt.insert(it, mylttmp.begin(), mylttmp.end());

	it = mylt.begin();
	mylt.erase(it);
	mylttmp.erase(mylttmp.begin(), mylttmp.end());

	/**********************************************************************************/
	/* 迭代器的算术操作                                                               */
	/* 只有vector支持it+n, it-n, it2-it1的操作,其他类型的容器仅支持自增自减操作    */
	/**********************************************************************************/
	vector<int> vc(5, 6);
	vector<int>::iterator itv = vc.begin();
	vector<int>::iterator ite = vc.end();
	itv + 5;
	ite - itv;

	/************************************************************************/
	/* 运算符函数,容器对象比较                                              */
	/************************************************************************/
	list<int> L1, L2;
	L1.push_back(1);
	L2.push_back(2);
	L2.assign(L1.begin(), L1.end());

	if (L1==L2)
	{
		cout << "L1==L2" << endl;
	}
	L1.push_back(1);
	L2.push_back(3);
	if (L1<L2)
	{
		cout << "L1<L2" << endl;
	}
	else if (L1>L2)
	{
		cout << "L1>L2" << endl;
	}
	
	if (L1!=L2)
	{
		cout << "L1!=L2" << endl;
	}

	/***********************************************************************************************/
	/* merge将两个list对象合并为一个list对象,合并之后所有元素自动从小到大排序,合并完成参数对象清空*/
	/* sort()默认从小到大排序                                                                      */
	/* sort(greater<int>())降序排列                                                                */
	/* splice(I1, V2)将对象V2插入到迭代器I1的后面,清空V2对象                                      */
	/* splice(I1, V3, I2)将对象V3中迭代器I2指向的元素插入到I1的后面,V3中移除对应元素              */
	/* splice(I1, V3, I2, I3)将对象V3中迭代器范围元素插入到I1的后面,V3中对应范围元素移除          */
	/***********************************************************************************************/
	list<int> V1, V2, V3;
	list<int>::iterator I1, I2, I3;
	V1.push_back(1);
	V1.push_back(5);
	V2.push_back(2);
	V2.push_back(3);
	V3.push_back(7);
	V3.push_back(8);

	V1.merge(V2);
	V1.merge(V3);

	V1.sort();
	V1.sort(greater<int>());

	V2.push_back(21);
	V2.push_back(30);
	V3.push_back(17);
	V3.push_back(28);
	V3.push_back(11);
	V3.push_back(42);

	I1 = V1.end();
	V1.splice(I1, V2);
	I1 = V1.end();
	I2 = V3.begin();
	V1.splice(I1, V3, I2);
	I1 = V1.end();
	I2 = V3.begin();
	I3 = V3.end();
	V1.splice(I1, V3, I2, I3);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值