C++ STL list
2016-11-03 20:33
399 查看
转载自http://www.cnblogs.com/scandy-yuan/archive/2013/01/08/2851324.html
List 容器
list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。
使用list容器之前必须加上<vector>头文件:#include<list>;
list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;
构造函数
list<int> c0; //空链表
list<int> c1(3); //建一个含三个默认值是0的元素的链表
list<int> c2(5,2); //建一个含五个元素的链表,值都是2
list<int> c4(c2); //建一个c2的copy链表
list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。
成员函数
c.begin() 返回指向链表第一个元素的迭代器。
c.end() 返回指向链表最后一个元素之后的迭代器。
list<int>::iterator it;
c.rbegin() 返回逆向链表的第一个元素,即c链表的最后一个数据。
c.rend() 返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
list<int>::reverse_iterator it;
operator= 重载赋值运算符。
c.assign(n,num) 将n个num拷贝赋值给链表c。
c.assign(beg,end) 将[beg,end)区间的元素拷贝赋值给链表c。
c.front() 返回链表c的第一个元素。
c.back() 返回链表c的最后一个元素。
c.empty() 判断链表是否为空。
c.size() 返回链表c中实际元素的个数。
c.max_size() 返回链表c可能容纳的最大元素数量。
c.clear() 清除链表c中的所有元素。
c.insert(pos,num) 在pos位置插入元素num。
c.insert(pos,n,num) 在pos位置插入n个元素num。
c.insert(pos,beg,end) 在pos位置插入区间为[beg,end)的元素。
c.erase(pos) 删除pos位置的元素。
c.push_back(num) 在末尾增加一个元素。
c.pop_back() 删除末尾的元素。
c.push_front(num) 在开始位置增加一个元素。
c.pop_front() 删除第一个元素。
resize(n) 从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
resize(n,num) 从新定义链表的长度,超出原始长度部分用num代替。
c1.swap(c2); 将c1和c2交换。
swap(c1,c2); 同上。
c1.merge(c2) 合并2个有序的链表并使之有序,从新放到c1里,释放c2。
c1.merge(c2,comp) 合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。
c1.splice(c1.beg,c2) 将c2连接在c1的beg位置,释放c2
c1.splice(c1.beg,c2,c2.beg) 将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素
c1.splice(c1.beg,c2,c2.beg,c2.end) 将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素
remove(num) 删除链表中匹配num的元素。
remove_if(comp) 删除条件满足的元素,参数为自定义的回调函数。
reverse() 反转链表
unique() 删除相邻的元素
c.sort() 将链表排序,默认升序
c.sort(comp) 自定义回调函数实现自定义排序
List 容器
list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。
使用list容器之前必须加上<vector>头文件:#include<list>;
list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;
构造函数
list<int> c0; //空链表
list<int> c1(3); //建一个含三个默认值是0的元素的链表
list<int> c2(5,2); //建一个含五个元素的链表,值都是2
list<int> c4(c2); //建一个c2的copy链表
list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。
成员函数
c.begin() 返回指向链表第一个元素的迭代器。
c.end() 返回指向链表最后一个元素之后的迭代器。
list<int>::iterator it;
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;
c.rbegin() 返回逆向链表的第一个元素,即c链表的最后一个数据。
c.rend() 返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。
list<int>::reverse_iterator it;
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;
operator= 重载赋值运算符。
list<int> a1 {1,2,3,4,5},a2; a2 = a1; list<int>::iterator it; for(it = a2.begin();it!=a2.end();it++){ cout << *it << endl; }
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;
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; }
c.empty() 判断链表是否为空。
list<int> a1{1,2,3,4,5}; if(!a1.empty()) cout << "a1 is not empty" << endl; else cout << " a1 is empty" << endl;
c.size() 返回链表c中实际元素的个数。
list<int> a1{1,2,3,4,5}; cout << a1.size() << endl;
c.max_size() 返回链表c可能容纳的最大元素数量。
list<int> a1{1,2,3,4,5}; cout << a1.max_size() << endl;
c.clear() 清除链表c中的所有元素。
list<int> a1{1,2,3,4,5}; list<int>::iterator it; cout << "clear before:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << "\t"; } cout << endl; a1.clear(); cout << "clear after:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << "\t"; } cout << endl;
c.insert(pos,num) 在pos位置插入元素num。
c.insert(pos,n,num) 在pos位置插入n个元素num。
c.insert(pos,beg,end) 在pos位置插入区间为[beg,end)的元素。
list<int> a1{1,2,3,4,5}; list<int>::iterator it; cout << "insert before:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; a1.insert(a1.begin(),0); cout << "insert(pos,num) after:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; a1.insert(a1.begin(),2,88); cout << "insert(pos,n,num) after:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; int arr[5] = {11,22,33,44,55}; a1.insert(a1.begin(),arr,arr+3); cout << "insert(pos,beg,end) after:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl;
c.erase(pos) 删除pos位置的元素。
list<int> a1{1,2,3,4,5}; list<int>::iterator it; cout << "erase before:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; a1.erase(a1.begin()); cout << "erase after:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl;
c.push_back(num) 在末尾增加一个元素。
c.pop_back() 删除末尾的元素。
c.push_front(num) 在开始位置增加一个元素。
c.pop_front() 删除第一个元素。
list<int> a1{1,2,3,4,5}; a1.push_back(10); list<int>::iterator it; cout << "push_back:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; a1.pop_back(); cout << "pop_back:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; a1.push_front(20); cout << "push_front:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl; a1.pop_front(); cout << "pop_front:"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl;
resize(n) 从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
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 << " "; } cout << endl; a1.resize(10, 10); cout << "resize(n,num):"; for(it = a1.begin();it!=a1.end();it++){ cout << *it << " "; } cout << endl;
c1.swap(c2); 将c1和c2交换。
swap(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 << " "; } cout << endl; swap(a3,a2); cout << "swap(a3,a2):"; for(it = a3.begin();it!=a3.end();it++){ cout << *it << " "; } return 0;
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;
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;
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;
c1.splice(c1.beg,c2,c2.beg,c2.end) 将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素
1 list<int> a1{1,2,3},a2{4,5,6}; 2 a1.splice(a1.begin(),a2,a2.begin(),a2.end()); 3 list<int>::iterator it; 4 cout << "a1.merge(a2):"; 5 for(it = a1.begin();it!=a1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl; 9 return 0;
remove(num) 删除链表中匹配num的元素。
1 list<int> a1{1,2,3,4,5}; 2 a1.remove(3); 3 list<int>::iterator it; 4 cout << "remove():"; 5 for(it = a1.begin();it!=a1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl;
remove_if(comp) 删除条件满足的元素,参数为自定义的回调函数。
1 list<int> a1{1,2,3,4,5}; 2 a1.remove_if([](int n){return n<3;}); 3 list<int>::iterator it; 4 cout << "remove_if():"; 5 for(it = a1.begin();it!=a1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl;
reverse() 反转链表
1 list<int> a1{1,2,3,4,5}; 2 a1.reverse(); 3 list<int>::iterator it; 4 cout << "reverse:"; 5 for(it = a1.begin();it!=a1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl;
unique() 删除相邻的元素
1 list<int> a1{1,1,3,3,5}; 2 a1.unique(); 3 list<int>::iterator it; 4 cout << "unique:"; 5 for(it = a1.begin();it!=a1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl; 9 return 0;
c.sort() 将链表排序,默认升序
c.sort(comp) 自定义回调函数实现自定义排序
1 list<int> a1{1,3,2,5,4}; 2 a1.sort(); 3 list<int>::iterator it; 4 cout << "sort():"; 5 for(it = a1.begin();it!=a1.end();it++){ 6 cout << *it << " "; 7 } 8 cout << endl; 9 10 a1.sort([](int n1,int n2){return n1>n2;}); 11 cout << "sort(function point):"; 12 for(it = a1.begin();it!=a1.end();it++){ 13 cout << *it << " "; 14 } 15 cout << endl;
相关文章推荐
- C++的STL中list
- [C++][stl]vector、list、deque
- C++ STL 容器技术 之 list双向链表容器
- C++中STL STD::list使用说明 (ZZ)
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- 对C++ STL,list元素的循环删除操作
- 【C++ STL学习之四】容器list深入学习
- 【C++ STL】细数C++ STL 的那些事---List(双向链表)
- C++STL之list用法总结
- C++ STL中判断list为空,size()==0和empty()有什么区别
- 【C++】 STL vector list deque
- 【转】C++ List的用法 (STL)
- [C++][STL] C++ STL 之 List
- 【C++ STL学习之四】容器list深入学习
- C++ STL学习之list
- 【C++】 STL vector list deque
- [C++][stl]vector、list、deque
- C++ STL List
- 对C++ STL,list元素的循环删除操作 //断链,出错地方
- C++ 中list容器,自定义sort排序规则,stl中sort自定义排序规则