list相当于是一个双链表,可以快速的从任意位置删除和插入,但查询不方便!
#include <iostream>
using namespace std;
#include <list>
//创建list对象
void f1()
{
list<int> l;
//在后面增加元素
l.push_back(10);
l.push_back(20);
l.push_back(30);// 10, 20, 30
//在前面增加元素
l.push_front(2);//2, 10, 20, 30
list<int>::iterator it;
for (it = l.begin(); it != l.end(); ++it)
{
cout << *it << endl;
}
cout << "----------------------------------" << endl;
it = l.begin();
it++;
l.erase(it);//根据指针删除元素 2, 20, 30
it = l.begin();
for (it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
cout << "=================" << endl;
l.push_back(20);// 2, 20, 30, 20
l.remove(50); //根据元值删除元素 2, 20, 30, 20
// l.clear(); //全部删除
l.sort(); //从小到大排序
l.reverse(); //倒序
it = l.begin();
for (it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
}
int main()
{
f1();
return 0;
}