C++ 标准库的双向链表

	//std list
	//初始化方式
	list Nodelist;
	list Nodelist1({ "sda","dsad","dsa","dsa" });
	list Nodelist2 = { "sda","dsad","dsa","dsa" };
	list Nodelist3 = Nodelist1;	//深拷贝
	list Nodelist5(Nodelist1);	//深拷贝
	string a11[] = { "sda","dsad","dsa","dsa" };
	list 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 ddd = { 321,53,1234,34 };
	ddd.sort(); //升序
	ddd.sort(greater()); //降序

	//移除
	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 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;
	}

 

你可能感兴趣的:(c++,stl,list,api)