C++ 标准库的双向链表

本文详细介绍了C++中list容器的初始化、添加、删除、赋值、插入、排序及遍历等操作,包括深拷贝、push_back、pop_front、emplace、sort、unique、erase、assign、insert等功能的使用,同时展示了如何使用迭代器进行正反向遍历。此外,还涉及了自定义类型在list中的比较操作。通过实例演示,帮助读者深入理解STL list的高效管理与操作。
摘要由CSDN通过智能技术生成
	//std list
	//初始化方式
	list<string> Nodelist;
	list<string> Nodelist1({ "sda","dsad","dsa","dsa" });
	list<string> Nodelist2 = { "sda","dsad","dsa","dsa" };
	list<string> Nodelist3 = Nodelist1;	//深拷贝
	list<string> Nodelist5(Nodelist1);	//深拷贝
	string a11[] = { "sda","dsad","dsa","dsa" };
	list<string> NodeList4(a11, a11 + 2);

	//List添加
	NodeList4.push_back("xxxx");
	NodeList4.push_back("xx2xx");
	NodeList4.push_front("xx2xx");
	NodeList4.push_front("xx2xx");

	NodeList4.pop_back();
	NodeList4.pop_front();

	NodeList4.emplace_back("RRR"); //比push_back少执行一次拷贝构造
	NodeList4.emplace_front("FFFF");

	//赋值
	NodeList4.assign({ "dsadw","dsacs","ewq","csad" });
	NodeList4.assign({});
	NodeList4.assign(Nodelist1.begin(), Nodelist1.end());
	NodeList4.assign(100, "0000");
	NodeList4.emplace_front("FFFF222");

	//移除
	NodeList4.erase();

	NodeList4.size();
	NodeList4.clear();
	NodeList4.swap(Nodelist3);

	//插入
	NodeList4.insert(NodeList4.begin(), "fewfwe");
	NodeList4.insert(NodeList4.begin(), { "fewfwe" ,"dsadwq","csadeqw" });
	NodeList4.insert(NodeList4.end(), Nodelist1.begin(), Nodelist1.end());

	NodeList4.sort();
	list<int> ddd = { 321,53,1234,34 };
	ddd.sort(); //升序
	ddd.sort(greater<int>()); //降序

	//移除
	ddd.remove(321);
	ddd.remove_if([](int v) {return v > 50; }); //范围性的移除

	ddd.unique();
	struct FHello
	{
		FHello()
		{
			a = 0;
		}
		FHello(int newa)
			:a(newa)
		{
	
		}
		int a;

		bool operator==(const FHello& v)
		{
			return a == v.a;
		}
	};

	list<FHello> llo;
	llo.push_front(FHello(1));
	llp.push_front(FHello(1));
	llp.push_front(FHello(1));
	llp.push_front(FHello(2));
	llp.push_front(FHello(2));
	llo.unique();

	//list遍历
	for (auto It = NodeList4.begin(); It != NodeList4.end(); It++)
	{
		cout << *It << endl;
	}

	for (auto& Tmp : NodeList4)
	{
		cout << Tmp << endl;
	}

	for (auto It = NodeList4.cbegin(); It != NodeList4.cend(); It++)
	{
		cout << *It << endl;
	}

	for (auto It1 = NodeList4.rbegin(); It1 != NodeList4.rend(); It1++)
	{
		cout << *It1 << endl;
		advance(It1, 2);
	}

	auto ListV = { 1,2,3,4,5 }; 
	//逆序
	for (auto It = rbegin(ListV); It != rend(ListV); ++It)
	{
		cout << *It << endl;
	}
	
	//正序
	for (auto It = begin(ListV); It != end(ListV); ++It)
	{
		cout << *It << endl;
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值