list同vector一樣是c++中的一個模板類。關於它的詳細內容可查看c++的文檔 http://www.cplusplus.com/reference/list/list/
C++中list的使用方法及常用list操作總結
一、List定義:
List是stl實現的雙向鏈表,與向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢。使用時需要添加頭文件
#include <list>
二、List定義和初始化:
list<int>lst1; //創建空list
list<int> lst2(5); //創建含有5個元素的list
list<int>lst3(3,2); //創建含有3個元素的list
list<int>lst4(lst2); //使用lst2初始化lst4
list<int>lst5(lst2.begin(),lst2.end()); //同lst4
三、List常用操作函數:
Lst1.assign() 給list賦值
Lst1.back() 返回最后一個元素
Lst1.begin() 返回指向第一個元素的迭代器
Lst1.clear() 刪除所有元素
Lst1.empty() 如果list是空的則返回true
Lst1.end() 返回末尾的迭代器
Lst1.erase() 刪除一個元素
Lst1.front() 返回第一個元素
Lst1.get_allocator() 返回list的配置器
Lst1.insert() 插入一個元素到list中
Lst1.max_size() 返回list能容納的最大元素數量
Lst1.merge() 合並兩個list
Lst1.pop_back() 刪除最后一個元素
Lst1.pop_front() 刪除第一個元素
Lst1.push_back() 在list的末尾添加一個元素
Lst1.push_front() 在list的頭部添加一個元素
Lst1.rbegin() 返回指向第一個元素的逆向迭代器
Lst1.remove() 從list刪除元素
Lst1.remove_if() 按指定條件刪除元素
Lst1.rend() 指向list末尾的逆向迭代器
Lst1.resize() 改變list的大小
Lst1.reverse() 把list的元素倒轉
Lst1.size() 返回list中的元素個數
Lst1.sort() 給list排序
Lst1.splice() 合並兩個list
Lst1.swap() 交換兩個list
Lst1.unique() 刪除list中重復的元素
四、List使用示例:
示例1:遍歷List
//迭代器法
for(list<int>::const_iteratoriter = lst1.begin();iter != lst1.end();iter++) { cout<<*iter; } cout<<endl;
示例2
1 #include <iostream> 2 #include <list> 3 #include <numeric> 4 #include <algorithm> 5 #include <windows.h> 6 using namespace std; 7 8 typedef list<int> LISTINT; 9 typedef list<int> LISTCHAR; 10 11 void main() 12 { 13 //用LISTINT創建一個list對象 14 LISTINT listOne; 15 //聲明i為迭代器 16 LISTINT::iterator i; 17 18 listOne.push_front(3); 19 listOne.push_front(2); 20 listOne.push_front(1); 21 22 listOne.push_back(4); 23 listOne.push_back(5); 24 listOne.push_back(6); 25 26 cout << "listOne.begin()--- listOne.end():" << endl; 27 for (i = listOne.begin(); i != listOne.end(); ++i) 28 cout << *i << " "; 29 cout << endl; 30 31 LISTINT::reverse_iterator ir; 32 cout << "listOne.rbegin()---listOne.rend():" << endl; 33 for (ir = listOne.rbegin(); ir != listOne.rend(); ir++) { 34 cout << *ir << " "; 35 } 36 cout << endl; 37 38 int result = accumulate(listOne.begin(), listOne.end(), 0); 39 cout << "Sum=" << result << endl; 40 cout << "------------------" << endl; 41 42 //用LISTCHAR創建一個list對象 43 LISTCHAR listTwo; 44 //聲明i為迭代器 45 LISTCHAR::iterator j; 46 47 listTwo.push_front('C'); 48 listTwo.push_front('B'); 49 listTwo.push_front('A'); 50 51 listTwo.push_back('D'); 52 listTwo.push_back('E'); 53 listTwo.push_back('F'); 54 55 cout << "listTwo.begin()---listTwo.end():" << endl; 56 for (j = listTwo.begin(); j != listTwo.end(); ++j) 57 cout << char(*j) << " "; 58 cout << endl; 59 60 j = max_element(listTwo.begin(), listTwo.end()); 61 cout << "The maximum element in listTwo is: " << char(*j) << endl; 62 Sleep(10000); 63 } 64
示例3
1 #include <iostream> 2 #include <list> 3 #include <windows.h> 4 5 using namespace std; 6 typedef list<int> INTLIST; 7 8 //從前向后顯示list隊列的全部元素 9 void put_list(INTLIST list, char *name) 10 { 11 INTLIST::iterator plist; 12 13 cout << "The contents of " << name << " : "; 14 for (plist = list.begin(); plist != list.end(); plist++) 15 cout << *plist << " "; 16 cout << endl; 17 } 18 19 //測試list容器的功能 20 void main(void) 21 { 22 //list1對象初始為空 23 INTLIST list1; 24 INTLIST list2(5, 1); 25 INTLIST list3(list2.begin(), --list2.end()); 26 27 //聲明一個名為i的雙向迭代器 28 INTLIST::iterator i; 29 30 put_list(list1, "list1"); 31 put_list(list2, "list2"); 32 put_list(list3, "list3"); 33 34 list1.push_back(7); 35 list1.push_back(8); 36 cout << "list1.push_back(7) and list1.push_back(8):" << endl; 37 put_list(list1, "list1"); 38 39 list1.push_front(6); 40 list1.push_front(5); 41 cout << "list1.push_front(6) and list1.push_front(5):" << endl; 42 put_list(list1, "list1"); 43 44 list1.insert(++list1.begin(), 3, 9); 45 cout << "list1.insert(list1.begin()+1,3,9):" << endl; 46 put_list(list1, "list1"); 47 48 //測試引用類函數 49 cout << "list1.front()=" << list1.front() << endl; 50 cout << "list1.back()=" << list1.back() << endl; 51 52 list1.pop_front(); 53 list1.pop_back(); 54 cout << "list1.pop_front() and list1.pop_back():" << endl; 55 put_list(list1, "list1"); 56 57 list1.erase(++list1.begin()); 58 cout << "list1.erase(++list1.begin()):" << endl; 59 put_list(list1, "list1"); 60 61 list2.assign(8, 1); 62 cout << "list2.assign(8,1):" << endl; 63 put_list(list2, "list2"); 64 65 cout << "list1.max_size(): " << list1.max_size() << endl; 66 cout << "list1.size(): " << list1.size() << endl; 67 cout << "list1.empty(): " << list1.empty() << endl; 68 69 put_list(list1, "list1"); 70 put_list(list3, "list3"); 71 cout << "list1>list3: " << (list1 > list3) << endl; 72 cout << "list1<list3: " << (list1 < list3) << endl; 73 74 list1.sort(); 75 put_list(list1, "list1"); 76 77 list1.splice(++list1.begin(), list3); 78 put_list(list1, "list1"); 79 put_list(list3, "list3"); 80 Sleep(10000); 81 }
參考:http://www.jb51.net/article/115201.htm