C++ List的用法(整理)


轉自http://blog.csdn.net/lskyne/article/details/10418823

Lists將元素按順序儲存在鏈表中. 與 向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢.


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中重復的元素

 

實例一:

 1 #include <iostream> 
 2 #include <list> 
 3 #include <numeric> 
 4 #include <algorithm> 
 5 using namespace std; 
 6 
 7 //創建一個list容器的實例LISTINT 
 8 typedef list<int> LISTINT; 
 9 //創建一個list容器的實例LISTCHAR 
10 typedef list<int> LISTCHAR; 
11 
12 void main() 
13 { 
14     //用list容器處理整型數據  
15     //用LISTINT創建一個名為listOne的list對象 
16     LISTINT listOne; 
17     //聲明i為迭代器 
18     LISTINT::iterator i; 
19     
20     //從前面向listOne容器中添加數據 
21     listOne.push_front (2); 
22     listOne.push_front (1); 
23     
24     //從后面向listOne容器中添加數據 
25     listOne.push_back (3); 
26     listOne.push_back (4); 
27     
28     //從前向后顯示listOne中的數據 
29     cout<<"listOne.begin()--- listOne.end():"<<endl; 
30     for (i = listOne.begin(); i != listOne.end(); ++i) 
31         cout << *i << " "; 
32     cout << endl; 
33     
34     //從后向后顯示listOne中的數據 
35     LISTINT::reverse_iterator ir; 
36     cout<<"listOne.rbegin()---listOne.rend():"<<endl; 
37     for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) { 
38         cout << *ir << " "; 
39     } 
40     cout << endl; 
41     
42     //使用STL的accumulate(累加)算法 
43     int result = accumulate(listOne.begin(), listOne.end(),0); 
44     cout<<"Sum="<<result<<endl; 
45     cout<<"------------------"<<endl; 
46     
47     //-------------------------- 
48     //用list容器處理字符型數據 
49     //-------------------------- 
50     
51     //用LISTCHAR創建一個名為listOne的list對象 
52     LISTCHAR listTwo; 
53     //聲明i為迭代器 
54     LISTCHAR::iterator j; 
55     
56     //從前面向listTwo容器中添加數據 
57     listTwo.push_front ('A'); 
58     listTwo.push_front ('B'); 
59     
60     //從后面向listTwo容器中添加數據 
61     listTwo.push_back ('x'); 
62     listTwo.push_back ('y'); 
63     
64     //從前向后顯示listTwo中的數據 
65     cout<<"listTwo.begin()---listTwo.end():"<<endl; 
66     for (j = listTwo.begin(); j != listTwo.end(); ++j) 
67         cout << char(*j) << " "; 
68     cout << endl; 
69     
70     //使用STL的max_element算法求listTwo中的最大元素並顯示 
71     j=max_element(listTwo.begin(),listTwo.end()); 
72     cout << "The maximum element in listTwo is: "<<char(*j)<<endl; 
73 } 

結果:

 

listOne.begin()--- listOne.end():
1 2 3 4
listOne.rbegin()---listOne.rend():
4 3 2 1
Sum=10
------------------
listTwo.begin()---listTwo.end():
B A x y
The maximum element in listTwo is: y
Press any key to continue

 

實例二:

 1 #include <iostream> 
 2 #include <list> 
 3 
 4 using namespace std; 
 5 typedef list<int> INTLIST; 
 6 
 7 //從前向后顯示list隊列的全部元素 
 8 void put_list(INTLIST list, char *name) 
 9 { 
10     INTLIST::iterator plist; 
11     
12     cout << "The contents of " << name << " : "; 
13     for(plist = list.begin(); plist != list.end(); plist++) 
14         cout << *plist << " "; 
15     cout<<endl; 
16 } 
17 
18 //測試list容器的功能 
19 void main(void) 
20 { 
21     //list1對象初始為空 
22     INTLIST list1; 
23     //list2對象最初有10個值為6的元素 
24     INTLIST list2(10,6); 
25     //list3對象最初有3個值為6的元素 
26     INTLIST list3(list2.begin(),--list2.end()); 
27     
28     //聲明一個名為i的雙向迭代器 
29     INTLIST::iterator i; 
30     
31     //從前向后顯示各list對象的元素 
32     put_list(list1,"list1"); 
33     put_list(list2,"list2"); 
34     put_list(list3,"list3"); 
35     
36     //從list1序列后面添加兩個元素 
37     list1.push_back(2); 
38     list1.push_back(4); 
39     cout<<"list1.push_back(2) and list1.push_back(4):"<<endl; 
40     put_list(list1,"list1"); 
41     
42     //從list1序列前面添加兩個元素 
43     list1.push_front(5); 
44     list1.push_front(7); 
45     cout<<"list1.push_front(5) and list1.push_front(7):"<<endl; 
46     put_list(list1,"list1"); 
47     
48     //在list1序列中間插入數據 
49     list1.insert(++list1.begin(),3,9); 
50     cout<<"list1.insert(list1.begin()+1,3,9):"<<endl; 
51     put_list(list1,"list1"); 
52     
53     //測試引用類函數 
54     cout<<"list1.front()="<<list1.front()<<endl; 
55     cout<<"list1.back()="<<list1.back()<<endl; 
56     
57     //從list1序列的前后各移去一個元素 
58     list1.pop_front(); 
59     list1.pop_back(); 
60     cout<<"list1.pop_front() and list1.pop_back():"<<endl; 
61     put_list(list1,"list1"); 
62     
63     //清除list1中的第2個元素 
64     list1.erase(++list1.begin()); 
65     cout<<"list1.erase(++list1.begin()):"<<endl; 
66     put_list(list1,"list1"); 
67     
68     //對list2賦值並顯示 
69     list2.assign(8,1); 
70     cout<<"list2.assign(8,1):"<<endl; 
71     put_list(list2,"list2"); 
72     
73     //顯示序列的狀態信息 
74     cout<<"list1.max_size(): "<<list1.max_size()<<endl; 
75     cout<<"list1.size(): "<<list1.size()<<endl; 
76     cout<<"list1.empty(): "<<list1.empty()<<endl; 
77     
78     //list序列容器的運算 
79     put_list(list1,"list1"); 
80     put_list(list3,"list3"); 
81     cout<<"list1>list3: "<<(list1>list3)<<endl; 
82     cout<<"list1<list3: "<<(list1<list3)<<endl; 
83     
84     //對list1容器排序 
85     list1.sort(); 
86     put_list(list1,"list1"); 
87     
88     //結合處理 
89     list1.splice(++list1.begin(), list3); 
90     put_list(list1,"list1"); 
91     put_list(list3,"list3"); 
92 } 


結果:

 

The contents of list1 :
The contents of list2 : 6 6 6 6 6 6 6 6 6 6
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1.push_back(2) and list1.push_back(4):
The contents of list1 : 2 4
list1.push_front(5) and list1.push_front(7):
The contents of list1 : 7 5 2 4
list1.insert(list1.begin()+1,3,9):
The contents of list1 : 7 9 9 9 5 2 4
list1.front()=7
list1.back()=4
list1.pop_front() and list1.pop_back():
The contents of list1 : 9 9 9 5 2
list1.erase(++list1.begin()):
The contents of list1 : 9 9 5 2
list2.assign(8,1):
The contents of list2 : 1 1 1 1 1 1 1 1
list1.max_size(): 1073741823
list1.size(): 4
list1.empty(): 0
The contents of list1 : 9 9 5 2
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1>list3: 1
list1<list3: 0
The contents of list1 : 2 5 9 9
The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
The contents of list3 :
Press any key to continue


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM