初始化

#include <list>

list<char> clist;

算法:

clist.push_back(c);

clist.remove('d');

代码:

 #include <list>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std; int main()
{
list<char> clist; //can't put iter initialization here
//because clist is empty now, iter will be NULL
//list<char>::iterator iter = clist.begin(); for(char c = 'a'; c < 'f'; ++c)
clist.push_back(c); list<char>::iterator iter = clist.begin(); for(; iter != clist.end(); ++iter)
cout << *iter << " | "; cout << endl; //高效率remove,利用list特点,只移动一个元素
clist.remove('d');
copy(clist.begin(), clist.end(), ostream_iterator<char>(cout, " ")); cout << endl; //低效率remove, 算法,需要移动很多元素
clist.erase(remove(clist.begin(), clist.end(), 'c'), clist.end());
copy(clist.begin(), clist.end(), ostream_iterator<char>(cout, " ")); return ;
}

输出:

$ ./a.exe
a | b | c | d | e |
a b c e
a b e
04-13 19:05