Lists將元素按順序儲存在鏈表中. 與 向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢. STL中 end()指向的總是無效值,取值都用迭代器,用法跟指針差不多。 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中重復的元素 附List用法實例: #include <iostream> #include <list> #include <numeric> #include <algorithm> using namespace std; //創建一個list容器的實例LISTINT typedef list<int> LISTINT; //創建一個list容器的實例LISTCHAR typedef list<char> 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 << endl; //輸出為 1 2 3 4 //從后向后顯示listOne中的數據 LISTINT::reverse_iterator ir; cout<<"listOne.rbegin()---listOne.rend():"<<endl; for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) { cout << *ir << " "; } cout << endl; //輸出為 4 3 2 1 //使用STL的accumulate(累加)算法 int result = accumulate(listOne.begin(), listOne.end(),0); cout<<"Sum="<<result<<endl; cout<<"------------------"<<endl; //輸出為 Sum=10 //-------------------------- //用list容器處理字符型數據 //-------------------------- //用LISTCHAR創建一個名為listOne的list對象 LISTCHAR listTwo; //聲明i為迭代器 LISTCHAR::iterator j; //從前面向listTwo容器中添加數據 listTwo.push_front ('A'); listTwo.push_front ('B'); //從后面向listTwo容器中添加數據 listTwo.push_back ('x'); listTwo.push_back ('y'); //從前向后顯示listTwo中的數據 cout<<"listTwo.begin()---listTwo.end():"<<endl; for (j = listTwo.begin(); j != listTwo.end(); ++j) cout << char(*j) << " "; cout << endl; //輸出為 B A x y //使用STL的max_element算法求listTwo中的最大元素並顯示 j=max_element(listTwo.begin(),listTwo.end()); cout << "The maximum element in listTwo is: "<<char(*j)<<endl; } //輸出為: The maximum element in listTwo is: y #include <iostream> #include <list> using namespace std; typedef list<int> INTLIST; //從前向后顯示list隊列的全部元素 void put_list(INTLIST list, char *name) { INTLIST::iterator plist; cout << "The contents of " << name << " : "; for(plist = list.begin(); plist != list.end(); plist++) cout << *plist << " "; cout<<endl; } //測試list容器的功能 void main(void) { //list1對象初始為空 INTLIST list1; //list2對象最初有10個值為6的元素 INTLIST list2(10,6); //list3對象最初有9個值為6的元素 INTLIST list3(list2.begin(),--list2.end()); //聲明一個名為i的雙向迭代器 INTLIST::iterator i; //從前向后顯示各list對象的元素 put_list(list1,"list1"); put_list(list2,"list2"); put_list(list3,"list3"); // 輸出: 空行一行;10個6一行;9個6一行。 //從list1序列后面添加兩個元素 list1.push_back(2); list1.push_back(4); cout<<"list1.push_back(2) and list1.push_back(4):"<<endl; put_list(list1,"list1"); //輸出 2 4 //從list1序列前面添加兩個元素 list1.push_front(5); list1.push_front(7); cout<<"list1.push_front(5) and list1.push_front(7):"<<endl; put_list(list1,"list1"); //輸出 7 5 2 4 //在list1序列中間插入數據 list1.insert(++list1.begin(),3,9); cout<<"list1.insert(list1.begin()+1,3,9):"<<endl; put_list(list1,"list1"); //輸出 7 9 9 9 5 2 4 //測試引用類函數 cout<<"list1.front()="<<list1.front()<<endl; //輸出 7 cout<<"list1.back()="<<list1.back()<<endl; //輸出 4 //從list1序列的前后各移去一個元素 list1.pop_front(); list1.pop_back(); cout<<"list1.pop_front() and list1.pop_back():"<<endl; put_list(list1,"list1"); //輸出 9 9 9 5 2 //清除list1中的第2個元素 list1.erase(++list1.begin()); cout<<"list1.erase(++list1.begin()):"<<endl; put_list(list1,"list1"); //輸出 9 9 5 2 //對list2賦值並顯示 list2.assign(8,1); cout<<"list2.assign(8,1):"<<endl; put_list(list2,"list2"); //輸出 1 1 1 1 1 1 1 1 【八個1】 //顯示序列的狀態信息 cout<<"list1.max_size(): "<<list1.max_size()<<endl; //輸出 1073741823 cout<<"list1.size(): "<<list1.size()<<endl; //輸出 4 cout<<"list1.empty(): "<<list1.empty()<<endl; //輸出 0 //list序列容器的運算 put_list(list1,"list1"); //輸出 9 9 5 2 put_list(list3,"list3"); //輸出 9 9 9 9 9 9 9 9 9 【9個9】 cout<<"list1>list3: "<<(list1>list3)<<endl; //輸出 1 cout<<"list1<list3: "<<(list1<list3)<<endl; //輸出 0 //對list1容器排序 list1.sort(); put_list(list1,"list1"); //輸出 2 5 9 9 //結合處理 list1.splice(++list1.begin(), list3); put_list(list1,"list1"); //輸出 2 6 6 6 6 6 6 6 6 6 5 9 9【在2后面插入list3】 put_list(list3,"list3"); //輸出 空行 } 補充:STL標准函數find進行vector 、list鏈表查找 #include <vector> #include <algorithm> #include <iostream> using namespace std; class example { public: example(int val) { i = val; } bool operator==(example const & rhs) { return (i == rhs.i) ? true : false; } private: int i; }; int main(void) { vector<example> ve; ve.push_back(1); //若這里為壓入2,則程序運行就會奔潰,因為迭代器指針it未指向任何有 //效的地址!!! vector<example>::iterator it; example elem(1); //定義類對象elem it = find(ve.begin(), ve.end(), elem); cout<<boolalpha<<(*it == elem); //輸出 true } #include <list> #include <vector> #include <algorithm> #include <iostream> using namespace std; 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元素全部被刪除了】 /* 輸出如下 1 5 3 5 6 1 5 3 5 6 4 1 3 6 4 */