list操作相关总结


#include <list>
#include <iostream>
using namespace std;


//
重载<<操作符. 发现重载<<时,第一个参数要为ostream类型,具体原因还没有了解. ostream & operator<<(ostream& ostr, const list<int> &listInt) { for (auto &i : listInt) { ostr << " " << i; } return ostr; } int main() { /////////四.修改器 ///////////// //1.比着vector多了push_front, pop_front. /////////五.操作 ///////////// //1.sort() 对元素进行排序 list<int> listInt1 = { 5,9,0,1,3 }; list<int> listInt2 = { 8,7,2,6,4 }; listInt1.sort(); listInt2.sort(); cout << listInt1 << endl;// 0 1 3 5 9 cout << listInt2 << endl;// 2 4 6 7 8 //2.merge(other) 合并两个!!已排序!!列表 listInt1.merge(listInt2); cout << listInt1 << endl;// 0 1 2 3 4 5 6 7 8 9 //3.splice(pos, other) 将other中内容转移给*this,塞到pos元素前. list<int> list1 = { 1, 2, 3, 4, 5 }; list<int> list2 = { 10, 20, 30, 40, 50 }; auto it = list1.begin(); advance(it, 2); list1.splice(it, list2); std::cout << "list1: " << list1 << " "; std::cout << "list2: " << list2 << " "; //4.!! remove(const T& value) 移除值为value的元素, //remove_if()移除满足条件的元素 list<int> list3 = { 1,100,2,3,310,1,11,-1,12 }; list3.remove(1); cout << list3; //100,2,3,3,10,11,-1,12 //5.reverse 翻转容器元素. list3.reverse(); //6.unique 移除连续相同的元素,只留下相等元素组的第一个元素. list3.unique(); return 0; }
新战场:https://blog.csdn.net/Stephen___Qin
原文地址:https://www.cnblogs.com/Stephen-Qin/p/12701634.html