【STL】list的常用方法
来源:互联网 发布:js遍历set集合对象 编辑:程序博客网 时间:2024/06/06 05:27
list的常用方法一览
- 【1】insert
- 【2】merge
- 【3】splice
- 【4】erase
- 【5】remove
- 【6】sort
- 【7】reverse
- 【8】swap
- 【9】unique
程序实例
#include <iostream>#include <list>using namespace std;bool mysort(int x,int y){ return x>y;}int main(){ int a[] = {9,4,6,3,8}; int b[] = {4,5,3,1,0}; int len = sizeof(a)/sizeof(int); int len1 = sizeof(b)/sizeof(int); list<int>iLi(a,a+len); list<int>iLi1(b,b+len1); iLi.sort(); iLi1.sort(); cout<<"iLi"<<endl; list<int>::iterator it = iLi.begin(); for(;it != iLi.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"iLi1"<<endl; list<int>::iterator it1 = iLi1.begin(); for(;it1 != iLi1.end();it1++) { cout<<*it1<<" "; } cout<<endl; cout<<"splice之后"<<endl; iLi.splice(iLi.begin(),iLi1); cout<<"iLi"<<endl; it = iLi.begin(); for(;it != iLi.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"iLi1"<<endl; it1 = iLi1.begin(); for(;it1 != iLi1.end();it1++) { cout<<*it1<<" "; } cout<<endl; cout<<"merge之后"<<endl; list<int>iLi2(a,a+len); list<int>iLi3(b,b+len1); iLi2.sort(); iLi3.sort(); iLi2.merge(iLi3); cout<<"iLi2"<<endl; it = iLi2.begin(); for(;it != iLi2.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"iLi3"<<endl; it1 = iLi3.begin(); for(;it1 != iLi3.end();it1++) { cout<<*it1<<" "; } cout<<endl; cout<<"reverse之后iLi2"<<endl; iLi2.reverse(); it = iLi2.begin(); for(;it != iLi2.end();it++) { cout<<*it<<" "; } cout<<endl; iLi2.reverse(); cout<<"sort按照逆序排列iLi2"<<endl; iLi2.sort(mysort); it = iLi2.begin(); for(;it != iLi2.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"insert之后,iLi2"<<endl; iLi2.insert(iLi2.begin(),100); //iLi2.insert(iLi2.begin(),iLi.begin(),iLi.end()); //iLi2.insert(iLi2.begin(),2,99); it = iLi2.begin(); for(;it != iLi2.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"remove之后"<<endl; iLi2.remove(99); for(it = iLi2.begin();it != iLi2.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"erase之后"<<endl; //iLi2.erase(++iLi2.begin()); //iLi2.erase(iLi2.begin()); int Which = 2;int i; for(it = iLi2.begin(),i = 0; i < Which-1;i++) { it++; } iLi2.erase(it); for(it = iLi2.begin();it != (iLi2.end());it++) { cout<<*it<<" "; } cout<<endl; cout<<"unique之后"<<endl; iLi2.unique(); for(it = iLi2.begin();it != iLi2.end();it++) { cout<<*it<<" "; } cout<<endl; cout<<"swap之后"<<endl; iLi2.swap(iLi); cout<<"iLi"<<endl; for(it = iLi.begin();it != iLi.end();it++) cout<<*it<<" "; cout<<endl; cout<<"iLi2"<<endl; for(it = iLi2.begin();it != iLi2.end();it++) cout<<*it<<" "; cout<<endl; system("pause"); return 0;}
结果展示
0 0
- 【STL】list的常用方法
- 【STL】list的常用方法
- List常用的方法
- List的常用方法
- 【STL】list常用函数的例子
- C++的STL库常用API--list
- STL 中list的常用接口
- 【STL】常用容器的方法
- STL容器的常用方法
- 【STL】deque的常用方法
- stl map list queue等容器的常用方法 (自我总结)
- STL list常用函数
- STL <list> 常用函数
- STL中list的erase()方法
- stl的list、set、map删除方法
- STL中list的erase()方法
- STL中部分常用的方法
- STL,map常用方法
- cvte most money cost just 0
- Unity一键构造Android包
- 数值存储(一)-CPU大端和小端模式详解
- 二叉树操作大全
- poj 3983 快算24
- 【STL】list的常用方法
- poj1609 Tiling Up Blocks(LIS)
- 定时器
- 解决TextView富文本显示时emoj或图片和文字不对齐的问题
- 80386ASM程序设计基础
- python 网络编程学习 非阻塞socket
- 广播接收者
- poj 2352 Stars
- Atom代码美化,atom-beautify和php-cs-fixer