码迷,mamicode.com
首页 > 其他好文 > 详细

list操作相关总结

时间:2020-04-14 23:00:50      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:参数   names   div   元素   修改器   容器   ice   tin   color   


#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 << "\n"; std::cout << "list2: " << list2 << "\n"; //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; }

 

list操作相关总结

标签:参数   names   div   元素   修改器   容器   ice   tin   color   

原文地址:https://www.cnblogs.com/Stephen-Qin/p/12701634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!