C++顺序容器之list初探
来源:互联网 发布:淘宝团队规模 编辑:程序博客网 时间:2024/05/26 08:42
C++顺序容器之list初探
双向链表,支持双向顺序访问。在list中任何位置进行插入和删除速度都很快。
list不支持随机访问,为了访问一个元素,必须遍历整个容器。
#include<iostream>#include<list>#include<algorithm>using namespace std;void printList(const list<int>& a);void printList(const list<int>& a){ // 注意形参中是const list,所以下面也需要用const_iterator // 否则distance无法使用 list<int>::const_iterator iter; for (iter = a.begin(); iter != a.end(); ++iter) { size_t index = distance(a.begin(), iter); cout << "a[" << index << "] = " << *iter << endl; } cout << endl;}int main(){ list<int> a; // push_front、push_back插入数据 a.push_front(4); a.push_front(3); a.push_front(2); a.push_front(1); a.push_back(50); printList(a); // insert插入数据 list<int>::iterator iter; iter = a.begin(); a.insert(iter, 0); a.insert(++iter, 10); a.insert(++iter, 4, 20); // 插入4个20 printList(a); // sort排序 cout << "sort排序" << endl; a.sort(); printList(a); // reverse逆序 cout << "reverse逆序" << endl; a.reverse(); printList(a); // erase删除指定元素 cout << "erase删除指定元素" << endl; a.erase(iter); printList(a); // erase删除指定区间的元素 cout << "erase删除指定区间元素" << endl; a.erase(++a.begin(), --a.end()); printList(a); system("pause"); return 0;}
阅读全文
0 0
- C++顺序容器之list初探
- 顺序容器之list
- 顺序容器之list
- 初探STL容器之List
- Collection容器之List初探
- c++STL之顺序容器(vector、deque、list)分析
- C++顺序容器之deque初探
- C++STL之list容器
- 顺序容器list vector
- STL顺序容器-list
- 顺序容器 list
- 顺序容器 list 2
- 【C++】顺序容器之vector用法
- C++primer_顺序容器之string操作
- c++STL(三)顺序容器deque,list,forward_list
- 【day0412 C++】顺序容器 STL list类的应用
- c++primer之顺序容器(容器库概览)
- 【C++】STL常用容器总结之二:顺序容器
- Codeforces Round #431 (Div. 2) (Codeforces 850C) E. Goodbye Souvenir cdq分治
- http 协议
- python random模块生成随机数
- unix ip 地址值
- init命令
- C++顺序容器之list初探
- bzoj1529: [POI2005]ska Piggy banks(tarjan||并查集)
- webpack配置及优化
- display:none和visibility:hidden的区别
- 头文件与链接
- 关于js中++的小问题
- osg源码剖析(2)eventTravel事件处理
- python Excel的操作
- Java线程模型