C++:list常用接口的使用方法

目录

1.构造函数和赋值重载

2.遍历方法

3.插入、删除数据

[4.分配函数 assign](#4.分配函数 assign)

[5.拼接函数 splice](#5.拼接函数 splice)

[6.移除函数 remove](#6.移除函数 remove)

[7.合并函数 merge](#7.合并函数 merge)

[8.排序函数 sort](#8.排序函数 sort)


1.构造函数和赋值重载

构造函数有四种

1.默认构造

2.用n个val来构造

3.用任意容器的一段迭代器区间来构造

4.拷贝构造

| default (1) | explicit list (const allocator_type& alloc = allocator_type()); |
| fill (2) | explicit list (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()); |
| range (3) | template <class InputIterator> list (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); |

copy (4) list (const list& x);
cpp 复制代码
void Test_list1()
{
	//1.默认构造
	list<int> l1;

	//2.用n个val来构造
	list<int> l2(5, 1);

	//3.用任意容器的一段迭代器区间来构造
	vector<int> v(5, 2);
	list<int> l3(l2.begin(), l2.end());
	list<int> l4(v.begin(), v.end());

	//4.拷贝构造
	list<int> l5(l4);

	//赋值重载
	list<int> l6 = l5;
}

2.遍历方法

只有两种遍历方法

1.迭代器

2.范围for(本质上用的也是迭代器)

list中没有重载[ ] ,所以不能用[ ]+下标的方法来访问每个元素

cpp 复制代码
void Test_list2()
{
	list<int> L(5, 1);

	//1.迭代器
	list<int>::iterator it = L.begin();
	while (it != L.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//2.范围for(本质上用的也是迭代器)
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

}

3.插入、删除数据

头插:push_front / emplace_front

尾插:push_back / emplace_back

插入:insert

头删:pop_front

尾删:pop_back

删除:erase

cpp 复制代码
void Test_list3()
{
	list<int> L;

	//头插:push_front / emplace_front
	L.push_front(1);
	L.emplace_front(1);
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	//尾插:push_back / emplace_back
	L.push_back(2);
	L.emplace_back(2);
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	//插入:insert
	L.insert(L.begin(), 3);
	L.insert(L.end(), 3);
	L.insert(++L.begin(), 4);
	L.insert(--L.end(), 4);
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	//头删:pop_front
	L.pop_front();
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	//尾删:pop_back
	L.pop_back();
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	//删除:erase
	L.erase(++L.begin(), --L.end());
	L.erase(L.begin(), L.end());
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;
}

4.分配函数 assign

| range (1) | template <class InputIterator> void assign (InputIterator first, InputIterator last); |

fill (2) void assign (size_type n, const value_type& val);

功能:将新内容分配给列表容器,替换其当前内容,并相应地修改其大小。

cpp 复制代码
void Test_list4()
{
	list<int> L1(5, 1);
	list<int> L2(5, 2);

	L1.assign(5, 3);
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;

	L1.assign(L2.begin(),L2.end());
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;

}

5.拼接函数 splice

功能:将元素从 x 传输到容器中,将它们插入到适当的位置

这有效地将这些元素插入到容器中并将它们从 x 中删除,从而改变两个容器的大小(被转移的数据将从原来的list中删除,这个过程相当于是"剪贴",是此消彼长的)。该作不涉及任何元素的构造或销毁。无论 x 是左值还是右值,或者 value_type 是否支持移动构造,它们都会被转移。

| entire list (1) | void splice (iterator position, list& x); |
| single element (2) | void splice (iterator position, list& x, iterator i); |

element range (3) void splice (iterator position, list& x, iterator first, iterator last);

第一个版本 (1)x 的所有元素转移到容器中。

第二个版本 (2) 仅将 i 指向的元素从 x 传输到容器中。

第三个版本 (3) 将范围 [first,last) 从 x 传输到容器中。

cpp 复制代码
void Test_list5()
{
	list<int> L1(5, 1);
	list<int> L2{1,2,3,4,5};

	//(1) 将 x 的所有元素转移到容器中
	L1.splice(L1.begin(),L2);
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;
	for (auto a : L2)
	{
		cout << a << " ";
	}
	cout << endl;

	//(2) 仅将 i 指向的元素从 x 传输到容器中
	L2.assign({ 1,2,3,4,5 });
	L1.splice(L1.begin(), L2, L2.begin());
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;
	for (auto a : L2)
	{
		cout << a << " ";
	}
	cout << endl;

	//(3) 将范围 [first,last) 从 x 传输到容器中
	L2.assign({ 1,2,3,4,5 });
	L1.splice(L1.begin(), L2, L2.begin(),L2.end());
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;
	for (auto a : L2)
	{
		cout << a << " ";
	}
	cout << endl;
}

6.移除函数 remove

复制代码
格式:void remove (const value_type& val);

功能:删除具有特定值的元素

从容器中删除所有与 val 比较的元素。这会调用这些对象的析构函数,并按删除的元素数减小容器大小

与成员函数 list::erase 不同,它按元素的位置擦除元素(使用迭代器),此函数 (list::remove) 按元素的值删除元素。

存在类似的函数 list::remove_if,它允许使用相等比较以外的条件来确定是否删除元素。

cpp 复制代码
void Test_list6()
{
	list<int> L{ 1,2,2,3,4,5,3,4,5 };
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	L.remove(2);
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;

	L.remove(3);
	for (auto a : L)
	{
		cout << a << " ";
	}
	cout << endl;
}

7.合并函数 merge

功能:将两个有序list按顺序合并起来

L1.merge(L2)中,L1为合并好的list,这时L2为空,相当于还是不断地进行"剪贴"的操作

cpp 复制代码
void Test_list7()
{
	list<int> L1{ 1,3,4,8,9 };
	list<int> L2{ 2,3,5,6,8 };
	L1.merge(L2);

	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;
	for (auto a : L2)
	{
		cout << a << " ";
	}
	cout << endl;
}

8.排序函数 sort

默认从小到大排序

cpp 复制代码
void Test_list8()
{
	list<int> L1{ 5,4,7,8,2 };

	L1.sort();
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;
}

如果希望从大到小排序,则可以用仿函数

cpp 复制代码
void Test_list8()
{
	list<int> L1{ 5,4,7,8,2 };

	L1.sort(greater<int>());
	for (auto a : L1)
	{
		cout << a << " ";
	}
	cout << endl;
}