list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素。
在STL 中,list和vector 一样,是两个常被使用的容器。
和vector不一样的是,list不支持对元素的任意存取。
list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。
禾口vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效。
还是举《C++之vector》中的例子:int data[6]={3,5,7,9,2,4};list< int> lidata(data, data+6);lidata.push_back(6);list初始化时,申请的空间大小为6,存放下了data中的6个元素,当向lidata插入第7个元素“6”,list申请新的节点单元,插入到list链表中,数据存放结构如图1所示:插入节点"6拆之后的list图1 list的存储结构list每次增加一个元素,不存在重新申请内存的情况,它的成本是恒定的。
而vector每当增加关键元素的时候,都需要重新申请新的更大的内存空间,会调用元素的自身的复制构造函数,存在构造成本。
在销毁旧内存的时候,会调用析构函数, 存在析构成本。
所以在存储复杂类型和大量元素的情况下,list比vector更有优势!List是一个双向链表,双链表既可以向前又向后链接他的元素。
List将元素按顺序储存在链表中.与向量(vector)相比,它允许快速的插入和删除,但是随机访问却比较慢。
assign()给list 赋值back()返回最后一个元素begin()返回指向第一个元素的迭代器clear()删除所有元素empty() 如果list是空的则返回trueen d()返回末尾的迭代器erase()删除一个元素fron t() 返回第一个元素get_allocator() 返回list 的配置器insert() 插入一个元素到list中max_size() 返回list能容纳的最大元素数量merge() 合并两个listpop_back() 删除最后一个元素pop_fro nt() 删除第一个元素push_back() 在list的末尾添加一个元素push_front() 在list的头部添加一个元素rbegi n() 返回指向第一个元素的逆向迭代器remove() 从list删除元素remove_if() 按指定条件删除元素rend()指向list末尾的逆向迭代器resize。
改变list的大小reverse。
把list的元素倒转size()返回list中的元素个数sort()给list 排序splice() 合并两个listswap() 交换两个listunique() 删除list中重复的元素List使用实例1#in elude <iostream>#in clude <list>#in clude <nu meric>#i nclude <algorithm>using n amespace std;〃创建一个list容器的实例LISTINT typedef list<int> LISTINT; 〃创建一个list容器的实例LISTCHAR typedef list<char> LISTCHAR;int main (i nt argc, char *argv[]){// -------------------------//用list容器处理整型数据// -------------------------〃用LISTINT创建一个名为listOne的list对象LISTINT list One;//声明i为迭代器LISTINT::iterator i;〃从前面向listOne容器中添加数据listO ne.push_fro nt (2);listO ne.push_fro nt (1);〃从后面向listOne容器中添加数据list On e.push_back (3);listO ne.push_back (4);〃从前向后显示listOne中的数据cout<<"list On e.beg in()--- list On e.e nd():"«e ndl;for (i = list On e.begi n(); i 匸list On e.e nd(); ++i) cout << *i << "";cout << en dl;〃从后向后显示listOne中的数据LISTINT::reverse_iterator ir;cout<<"listOne.rbegin()---list On e.re nd():"v<e ndl; for (ir =list On e.rbeg in (); ir!=list On e.re nd();ir++) { cout << *ir << "";}cout << en dl;〃使用STL的accumulate(累加)算法int result = accumulate(list On e.beg in(), list On e.e nd(),0); cout«"Sum二"vvresultvve ndl;cout«" --------------------- "<<e ndl;// -------------------------//用list容器处理字符型数据// -------------------------〃用LISTCHAR创建一个名为list One的list对象LISTCHAR listTwo;//声明i为迭代器LISTCHAR::iterator j;//从前面向listTwo容器中添加数据listTwo.push_fro nt ('A');listTwo.push_fro nt ('B');//从后面向listTwo容器中添加数据listTwo.push_back ('x'); listTwo.push_back ('y');〃从前向后显示listTwo中的数据cout«"listTwo.begin()---listTwo.e nd():"v<e ndl;for (j = listTwo.begin(); j != listTwo.end(); ++j)cout << char(*j) << "";cout << en dl;〃使用STL的max_element算法求listTwo中的最大元素并显示j=max_eleme nt(listTwo.begi n( ),listTwo.e nd());cout << "The maximum eleme nt in listTwo is: "v<char(*j)v<e ndl;return 0;}List使用实例2list: Lin ked list of variables, struct or objects. In sert/remove any where.Two examples are give n:1.The first STL example is for data type int2.The sec ond for a list of class in sta nces.They are used to show a simple example and a more complex real world applicati on.1. Lets start with a simple example of a program using STL for a linked list:// Simple example uses type int#in clude <iostream> #in clude <list>//In sert a new eleme nt at the end//Insert a new eleme nt at the beg inning// Insert "2" before position of first argument// (Place before sec ond argume nt)L.push_back(5);L.push_back(6);list <in t>::iterator i;for(i=L.begin(); i != L.end(); ++i) cout << *i << "";cout << en dl;return 0;}Compile: g++ example1.cppRun: ./a.outOutput: 0 2 0 5 62. The STL tutorials and texts seem to give simple examples which do notapply to the real world. The following example is for a doubly linked list.Since we are using a class and we are not using defined built-in C++types we have in cluded the followi ng:* To make this example more complete, a copy con structor has bee nin cluded although the compiler will gen erate a member-wise one automatically if needed. This has the samefunctionality as the assignment operator (=).« The assig nment (=) operator must be specified so that sort routi nes can assig n a new order to the members of the using n amespace std;int mai n()list<i nt> L;L.push_back(O); L.push_fro nt(0); L.i nsert(++L.beg in( ),2);list.« The "less than" (<) operator must be specified so that sort routines candetermine if one class instance is "less than" another.« The "equals to" (==) operator must be specified so that sort routines candetermine if one class instance is "equals to" another.// Stan dard Template Library example using a class.#in clude <iostream>#in clude <list>using n amespace std;// The List STL template requires overloading operators =, == and <.〃vc2005调试没有错(红色字体部分可去掉)、可用vc6.0却报错了“ 'operator <<' is ambiguous ” (vc6.0的加上红色字体部分)class AAA;ostream &operator«(ostream &output, const AAA &aaa);class AAA{frie nd ostream &operator<<(ostream &, const AAA &);public:int x;int y;float 乙AAA();AAA(co nst AAA &);~AAA(){};AAA & operator=(co nst AAA &rhs);int operator==(c onst AAA &rhs) con st;int operator<(c onst AAA &rhs) con st;};AAA::AAA() // Con structor{x = 0;y = 0;z = 0;}AAA::AAA(c onst AAA © in) // Copy con structor to han die pass by value. {x = copy in.x;y = copyi n.y;z = copy in.z;}ostream &operator<<(ostream &output, const AAA &aaa){output << aaa.x << ' ' << aaa.y << ' ' << aaa.z << en di;return output;}AAA& AAA::operator=(co nst AAA &rhs)this->x = rhs.x;this->y = rhs.y;this->z = rhs. z;return *this;}int AAA::operator==(c onst AAA &rhs) const{if( this->x != rhs.x) return 0;if( this->y != rhs.y) return 0;if( this->z != rhs.z) return 0;return 1;}// This function is required for built-in STL list functions like sortint AAA::operator<(c onst AAA &rhs) const{if( this->x == rhs.x && this->y == rhs.y && this->z < rhs.z) return 1;if( this->x == rhs.x && this->y < rhs.y) retur n 1;if( this->x < rhs.x ) return 1;return 0;}int mai n(){list<AAA> L;AAA Ablob ;Ablob.x=7;Ablob.y=2;Ablob.z=4.2355;L.push_back(Ablob); //In sert a new eleme nt at the endAblob.x=5;L.push_back(Ablob); // Object passed by value. Uses default member-wise // copy con structor Ablob.z=3.2355;L.push_back(Ablob);Ablob.x=3;Ablob.y=7;Ablob.z=7.2355;L.push_back(Ablob);list<AAA>::iterator i;for(i=L.beg in(); i 匸L.e nd(); ++i) cout << (*i).x << " "; // print membercout << en dl;for(i=L.beg in(); i 匸L.e nd(); ++i) cout << *i << " "; // print with overloadedoperatorcout << en dl;cout << "Sorted: " << en dl;L.sort();for(i二L.beg in(); i 匸L.e nd(); ++i) cout << *i << " "; // print with overloaded operator cout << en dl;return 0;}Output:7 5 5 37 2 4.23555 2 4.23555 2 3.23553 7 7.2355Sorted:3 7 7.23555 2 3.2355 5 2 4.2355 7 2 4.2355。