assign() 给list赋值
back() 返回最后一个元素
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
empty() 如果list是空的则返回true
end() 返回末尾的迭代器
erase() 删除一个元素
front() 返回第一个元素
get_allocator() 返回list的配置器
insert() 插入一个元素到list中
max_size() 返回list能容纳的最大元素数量
merge() 合并两个list
pop_back() 删除最后一个元素
pop_front() 删除第一个元素
push_back() 在list的末尾添加一个元素
push_front() 在list的头部添加一个元素
rbegin() 返回指向第一个元素的逆向迭代器
remove() 从list删除元素
remove_if() 按指定条件删除元素
rend() 指向list末尾的逆向迭代器
resize() 改变list的大小
reverse() 把list的元素倒转
size() 返回list中的元素个数
sort() 给list排序
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素
实例一:
- #include <iostream>
- #include <list>
- #include <numeric>
- #include <algorithm>
- using namespace std;
-
-
- typedef list<int> LISTINT;
-
- typedef list<char> LISTCHAR;
-
- void main()
- {
-
-
- LISTINT listOne;
-
- LISTINT::iterator i;
-
-
- listOne.push_front (2);
- listOne.push_front (1);
-
-
- listOne.push_back (3);
- listOne.push_back (4);
-
-
- cout<<"listOne.begin()--- listOne.end():"<<endl;
- for (i = listOne.begin(); i != listOne.end(); ++i)
- cout << *i << " ";
- cout << endl;
-
-
- LISTINT::reverse_iterator ir;
- cout<<"listOne.rbegin()---listOne.rend():"<<endl;
- for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
- cout << *ir << " ";
- }
- cout << endl;
-
-
- int result = accumulate(listOne.begin(), listOne.end(),0);
- cout<<"Sum="<<result<<endl;
- cout<<"------------------"<<endl;
-
-
-
-
-
-
- LISTCHAR listTwo;
-
- LISTCHAR::iterator j;
-
-
- listTwo.push_front ('A');
- listTwo.push_front ('B');
-
-
- listTwo.push_back ('x');
- listTwo.push_back ('y');
-
-
- cout<<"listTwo.begin()---listTwo.end():"<<endl;
- for (j = listTwo.begin(); j != listTwo.end(); ++j)
- cout << char(*j) << " ";
- cout << endl;
-
-
- j=max_element(listTwo.begin(),listTwo.end());
- cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
- }
-
-
- typedef list<int> LISTINT;
int main(void) {
int a[5] = {1,5,3,5,6}; LISTINT ls1;
ls1.assign(a,a+5); LISTINT::iterator it;
for( it=ls1.begin(); it!=ls1.end(); it++) cout<<*it<<" "; cout<<endl;
//输出 1 5 3 5 6
ls1.insert( ls1.end(), 4 );
for( it=ls1.begin(); it!=ls1.end(); it++) cout<<*it<<" "; cout<<endl;
//输出 1 5 3 5 6 4 ls1.remove( 5 );
for( it=ls1.begin(); it!=ls1.end(); it++) cout<<*it<<" "; cout<<endl; }
//输出 1 3 6 4 【5元素全部被删除了】