C++ list容器使用
2014-10-21 16:04
459 查看
//----------------------------------------------------
//AUTHOR: lanyang123456
//DATE: 2014-10-21
//----------------------------------------------------
$ g++ -o test list.cpp
$ ./test
0 2 0 5 6
再举一个字符串的例子
参考
http://blog.csdn.net/whz_zb/article/details/6831817
http://blog.chinaunix.net/uid-26527046-id-3465518.html
//AUTHOR: lanyang123456
//DATE: 2014-10-21
//----------------------------------------------------
#include <iostream> #include <list> using namespace std; int main() { list<int> L; L.push_back(0); // Insert a new element at the end L.push_front(0); // Insert a new element at the beginning L.insert(++L.begin(),2); // Insert "2" before position of first argument // (Place before second argument) L.push_back(5); L.push_back(6); list<int>::iterator i; for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; cout << endl; return 0; }
$ g++ -o test list.cpp
$ ./test
0 2 0 5 6
再举一个字符串的例子
#include <iostream> #include <list> using namespace std; int main() { list<string> L; L.push_back("Tom"); // Insert a new element at the end L.push_front("Jerry"); // Insert a new element at the beginning L.insert(++L.begin(), "Mily"); // Insert "2" before position of first argument // (Place before second argument) L.push_back("Alex"); L.push_back("Jack"); cout<<"list size = "<<L.size()<<endl; cout<<"list max_size = "<<L.max_size()<<endl; list<string>::iterator iter; for(iter = L.begin(); iter != L.end(); ++iter) cout<<*iter<<endl; L.erase(--iter);//remove the last one cout<<"------after erase---------"<<endl; cout<<"list size = "<<L.size()<<endl; cout<<"list max_size = "<<L.max_size()<<endl; for(iter = L.begin(); iter != L.end(); ++iter) cout<<*iter<<endl; return 0; } /* $ ./test list size = 5 list max_size = 768614336404564650 Jerry Mily Tom Alex Jack ------after erase--------- list size = 4 list max_size = 768614336404564650 Jerry Mily Tom Alex */
参考
http://blog.csdn.net/whz_zb/article/details/6831817
http://blog.chinaunix.net/uid-26527046-id-3465518.html
相关文章推荐
- c++中list容器的简要使用说明(1)
- C++ STL容器的学习使用(vector、queue、list、set、map)
- C++ 中list容器的简要使用说明(2)
- C++中三种容器的使用:vector,list,deque
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- C++容器-2顺序容器vector、list、deque的使用
- C/C++中文件输入输出流的一个示例(使用list容器)
- c++ list 容器使用例子
- c++ list容器的使用
- C++ list容器基础使用
- 浅谈STL list<T>链表容器和迭代器的使用C++实现
- C++ STL基本容器的使用(vector、list、deque、map、stack、queue)
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- 【C++】vector容器和list容器中使用单个迭代器的erase函数学习笔记
- 【语言-C++】C++容器:list、vector、hash、map的区别及使用范围
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- C++ STL容器的使用方法(vector、queue、list、set、map)
- [C++/C] Boost Adjacency_list 使用
- C++ 标准库 vector list map 基本使用方法 示例程序