list容器
list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。 使用list容器之前必须加上#include<list>头文件; list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;
list是一个双向链表,因此它的内存空间是可以不连续的,通过指针来进行数据的访问,这使list的随机存储变得非常低效,因此list没有提供[]操作符的重载。但list可以很好地支持任意地方的插入和删除,只需移动相应的指针即可。
1、成员函数 c.begin()返回指向链表第一个元素的迭代器。c.end()返回指向链表最后一个元素之后的迭代器。
list<int> a1{1,2,3,4,5};
list<int>::iterator it;
for(it = a1.begin();it!=a1.end();it++)
{
cout << *it << "\t";
}
cout << endl;
2、c.rbegin()返回逆向链表的第一个元素,即c链表的最后一个数据。c.rend()返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
list<int> a1{1,2,3,4,5};
list<int>::reverse_iterator it;
for(it = a1.rbegin();it!=a1.rend();it++)
{
cout << *it << "\t";
}
cout << endl;
3、c.assign(n,num) 将n个num拷贝赋值给链表c。c.assign(beg,end) 将[beg,end)区间的元素拷贝赋值给链表c。
int a[5] = {1,2,3,4,5};
list<int> a1;
list<int>::iterator it;
a1.assign(2,10);
for(it = a1.begin();it!=a1.end();it++)
{
cout << *it << " ";
}
cout << endl;
a1.assign(a,a+5);
for(it = a1.begin();it!=a1.end();it++)
{
cout << *it << " ";
}
cout << endl;
4、c.front()返回链表c的第一个元素。c.back()返回链表c的最后一个元素。
list<int> a1{1,2,3,4,5};
if(!a1.empty())
{
cout << "the first number is:" << a1.front() << endl;
cout << "the last number is:" << a1.back() << endl; 5
}
5、c.empty()判断链表是否为空。
6、c.size()返回链表c中实际元素的个数。
7、c.max_size()返回链表c可能容纳的最大元素数量。
8、c.clear()清除链表c中的所有元素。
9、c.insert(pos,num)在pos位置插入元素num。
10、c.insert(pos,n,num)在pos位置插入n个元素num。
11、c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素。
12、c.erase(pos)删除pos位置的元素。
13、c.push_back(num)在末尾增加一个元素。
14、c.pop_back()删除末尾的元素。
15、c.push_front(num)在开始位置增加一个元素。
16、c.pop_front()删除第一个元素。
17、resize(n) 重新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
18、resize(n,num) 重新定义链表的长度,超出原始长度部分用num代替。
list<int> a1{1,2,3,4,5};
a1.resize(8);
list<int>::iterator it;
cout << "resize(n):";
for(it = a1.begin();it!=a1.end();it++)
{
cout << *it << " "; 7
}
cout << endl;
a1.resize(10, 10);
cout << "resize(n,num):";
for(it = a1.begin();it!=a1.end();it++)
{
cout << *it << " ";
}
cout << endl;
19、c1.swap(c2);wap(c1,c2);将c1和c2交换
list<int> a1{1,2,3,4,5},a2,a3;
a2.swap(a1);
list<int>::iterator it;
cout << "a2.swap(a1):";
for(it = a2.begin();it!=a2.end();it++)
{
cout << *it << " "; 7
}
cout << endl;
swap(a3,a2);
cout << "swap(a3,a2):";
for(it = a3.begin();it!=a3.end();it++)
{
cout << *it << " "; 14
}
return 0;
20、c1.merge(c2) 合并2个有序的链表并使之有序,重新放到c1里,释放c2。 c1.merge(c2,comp)合并2个有序的链表并使之按照自定义规则排序之后重新放到c1中,释放c2。
list<int> a1{1,2,3},a2{4,5,6};
a1.merge(a2);
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a2.merge(a1,[](int n1,int n2){return n1>n2;});
cout << "a2.merge(a1,comp):";
for(it = a2.begin();it!=a2.end();it++){
cout << *it << " ";
}
cout << endl;
21、c1.splice(c1.beg,c2) 将c2连接在c1的beg位置,释放c2
list<int> a1{1,2,3},a2{4,5,6};
a1.splice(a1.begin(), a2);
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
22、c1.splice(c1.beg,c2,c2.beg) 将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素
list<int> a1{1,2,3},a2{4,5,6};
a1.splice(a1.begin(), a2,++a2.begin());
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
return 0;
23、c1.splice(c1.beg,c2,c2.beg,c2.end)将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素
24、remove(num)删除链表中匹配num的元素。
list<int> a1{1,2,3,4,5};
a1.remove(3);
list<int>::iterator it;
cout << "remove():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
25、remove_if(comp)删除条件满足的元素,参数为自定义的回调函数。
list<int> a1{1,2,3,4,5};
a1.remove_if([](int n){return n<3;});
list<int>::iterator it;
cout << "remove_if():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
26、reverse()反转链表
list<int> a1{1,2,3,4,5};
a1.reverse();
list<int>::iterator it;
cout << "reverse:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
27、c.sort()将链表排序,默认升序
28、c.sort(comp)自定义回调函数实现自定义排序
list<int> a1{1,3,2,5,4};
a1.sort();
list<int>::iterator it;
cout << "sort():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
class Game//自定义排序对象
{
public:
int iNum;
char name[20];
Game()
{
iNum=0;
memset(name,0,sizeof(name));
}
};
bool __stdcall SortFunc(const Game &t1,const Game &t2)//自定义排序回调函数 SortFunc
{
return t1.iNum>t2.iNum;
}
std::list<Game> vec;
vec.clear();
Game RedGames[5];
RedGames[0].iNum=10;
memcpy(RedGames[0].name,"RedCf ",sizeof(RedGames[0].name));
RedGames[1].iNum=20;
memcpy(RedGames[1].name,"NiZhan ",sizeof(RedGames[1].name));
RedGames[2].iNum=300;
memcpy(RedGames[2].name,"XuanWu ",sizeof(RedGames[2].name));
RedGames[3].iNum=50;
memcpy(RedGames[3].name,"YingXiong ",sizeof(RedGames[3].name));
RedGames[4].iNum=200;
memcpy(RedGames[4].name,"KeKeKeLe ",sizeof(RedGames[4].name));
for(int i=0;i<5;i++)//向vec容器添加对象
{
vec.push_back(RedGames[i]);
}
vec.sort(SortFunc);//自定义排序
//和vector容器不一样,list容器并不能使用通用模板函数sort排序函数进行排序,list容器有自带排序函数