list基础用法

list基础用法

1.list的访问就不能用下标+\[\]了,用迭代器

cpp 复制代码
void test1()
{
	list<int>lt;//list要带头文件,同理,vector,string也是
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	//list的访问就不能用下标+[]了
	//用迭代器
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it;
		it++;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e;
	} 
	cout << endl;

}

2.emplace_back()几乎是与push_back()用法一致,但也有差别

cpp 复制代码
void test2()
{
	list<A>lt;
	A a1(1, 2);
	lt.push_back(a1);//又名
	lt.push_back(A(1, 1));//匿名
	lt.emplace_back(a1);
	lt.emplace_back(A(1, 1));

	//差别:
	//push_back()只能传一个参数,emplace_back()支持直接传构造A的对象参数
	lt.emplace_back(3, 4);


}

3.insert(),erase()的用法

cpp 复制代码
void test_list3()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);

	//insert()
	auto it = lt.begin();
	int k = 3;
	while (k--)
	{
		++it;
	}

	lt.insert(it, 30);

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	//erase()
	int x = 0;
	cin >> x;
	it = find(lt.begin(), lt.end(), x);
	if (it != lt.end())
	{
		lt.erase(it);
	}

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}

4.reverse()

cpp 复制代码
     lt.reverse();//or
	reverse(lt.begin(), lt.end());

5.排序

cpp 复制代码
    // 升序
	lt.sort();
	//or
	less<int> ls;
    lt.sort(ls);
    //降序
     greater<int> gt;
	 lt.sort(ls);
	 //or
	 lt.sort(greater<int>());

	

关于链表排序:链表排序效率非常的低。

6.合并

cpp 复制代码
std::list<double> first, second;

	first.push_back(3.1);
	first.push_back(2.2);
	first.push_back(2.9);

	second.push_back(3.7);
	second.push_back(7.1);
	second.push_back(1.4);

	first.sort();
	second.sort();

	first.merge(second);//second就为空了

7.unique()(去重)

前提:必须保证有序

cpp 复制代码
void test5()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(20);
	lt.push_back(3);
	lt.push_back(5);
	lt.push_back(5);
	lt.push_back(4);
	lt.push_back(5);
	lt.push_back(6);

	lt.sort();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	lt.unique();
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
}

8.splice剪切再粘贴

cpp 复制代码
void test6()
{
	// 一个链表节点转移给另一个链表
	std::list<int> mylist1, mylist2;
	std::list<int>::iterator it;

	// set some initial values:
	for (int i = 1; i <= 4; ++i)
		mylist1.push_back(i);      // mylist1: 1 2 3 4

	for (int i = 1; i <= 3; ++i)
		mylist2.push_back(i * 10);   // mylist2: 10 20 30

	it = mylist1.begin();
	++it;                         // points to 2

	mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
								  // mylist2 (empty)
								  // "it" still points to 2 (the 5th element
}
相关推荐
m0_547486663 天前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr3 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_33 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
All for pursuit.3 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.3 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣3 天前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.9923 天前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
淡海水3 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1013 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
2401_839080544 天前
C++常见八股
数据结构·c++·算法