c++中的list用法

来源:百度知道 编辑:UC知道 时间:2024/05/09 02:28:22
请问C++中list的具体用法
比如说list是干什么用的,常用的格式是什么,这种格式能起到什么效果.特别是如list<T>啊,iterator的用法

#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>

using namespace std;

//创建一个list容器的实例LISTINT
typedef list<int> LISTINT;

//创建一个list容器的实例LISTCHAR
typedef list<int> LISTCHAR;

void main(void)
{
//--------------------------
//用list容器处理整型数据
//--------------------------
//用LISTINT创建一个名为listOne的list对象
LISTINT listOne;
//声明i为迭代器
LISTINT::iterator i;

//从前面向listOne容器中添加数据
listOne.push_front (2);
listOne.push_front (1);

//从后面向listOne容器中添加数据
listOne.push_back (3);
listOne.push_back (4);

//从前向后显示listOne中的数据
cout<<"listOne.begin()--- listOne.end():"<<endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout <<