程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++ list用法,list用法

C++ list用法,list用法

編輯:C++入門知識

C++ list用法,list用法


創建一個list實例並賦值:

 1 // 創建實例以及賦值
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     //第一種,通過構造函數
 7     int myints[] = {75,23,65,42,13};
 8     list<int> mylist1(myints, myints+5);
 9     list<int> mylist2(2,100);         // 2個值為100的元素
10     //第二種,用push_back,或push_front
11     for (int i = 1; i <= 5; ++i) mylist1.push_back(i);
12     mylist2.push_front (200);
13     mylist2.push_front (300);
14     //第三種,用assign
15     list<int> first;
16     list<int> second;
17     first.assign(7,100);                       // 給first添加7個值為100的元素
18     second.assign(first.begin(), first.end()); // 復制first給second
19     int myints[] = {16, 8, 4};
20     first.assign (myints, myints + 3);         // 將數組myints的內容添加給first
21 
22     //第四種,見insert函數
23     return 0;
24 }

 

成員函數:

Iterator:  (可用於遍歷list)

iterator begin();  //返回指向第一個元素的迭代器

iterator end();  //返回指向最後一個元素的迭代器

reverse_iterator rbegin();  //返回指向第一個元素的逆向迭代器

reverse_rend();  //返回指向最後一個元素的逆向迭代器

 1 //list的遍歷
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     int myints[] = {75,23,65,42,13};
 7     list<int> mylist (myints,myints+5);
 8     cout << "mylist contains:";
 9     //這是正序輸出:
10     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
11         cout << ' ' << *it;
12     cout << '\n';
13 
14     list.clear();
15     //逆序輸出:
16     for (int i = 1; i <= 5; ++i) mylist.push_back(i);
17     cout << "mylist backwards:";
18     for (list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit)
19         cout << ' ' << *rit;
20     cout << '\n';
21     return 0;
22 }

輸出結果為:
mylist contains: 75 23 65 42 13
mylist backwards: 5 4 3 2 1

 

Capacity: (用於獲取list容器大小信息)

bool empty() const;  //list為空時返回true

size_type size() const;  //返回list容器裡元素的個數

size_type max_size() const;  //返回list容器最大能容納的元素的個數,主要用於調用list的resize()函數時,檢查所請求的size大小是否被允許

Element access:(用於獲取首尾元素)

reference front();  //返回第一個元素的引用

const_reference front() const;

reference back();  //返回最後一個元素的引用

const_reference front() const;

Modifiers:

  • asign  //給容器添加新內容:

template<class InputIterator>

void assign(InputIterator first, InputIterator last);  //first,last是一個序列中起始和結束的迭代器的值,[first, last)包含了序列中所有元素

void assign(size_type n, const value_type& val);  //給list賦值n個值為val的元素

 1 // list::assign
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> first;
 7     list<int> second;
 8     first.assign(7,100);                      // 給first添加7個值為100的元素
 9     second.assign(first.begin(), first.end()); // 復制first給second
10 
11     int myints[] = {16, 8, 4};
12     first.assign (myints, myints + 3);            // 將數組myints的內容添加給first
13 
14     cout << "Size of first: " << int (first.size()) << '\n';
15     cout << "Size of second: " << int (second.size()) << '\n';
16     return 0;
17 }
輸出結果為:
Size of first: 3
Size of second: 7
  • push_front, pop_front, push_back, pop_back

void push_front(const value_type& val);  //在list頭添加元素

void pop_front();  //刪除list頭的元素

void push_back(const value_type& val);  //在list尾添加元素

void pop_back();  //刪除list尾的元素

 1 #include <iostream>
 2 #include <list>
 3 using namespace std;
 4 int main () {
 5     list<int> mylist (2,100);         // 2個值為100的元素
 6     // list::push_front
 7     mylist.push_front (200);
 8     mylist.push_front (300);
 9 
10     cout << "mylist contains:";
11     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
12         cout << ' ' << *it;
13     cout << '\n';
14 
15     // list::pop_front
16     cout << "Popping out the elements in mylist:";
17     while (!mylist.empty()) {
18         cout << ' ' << mylist.front();
19         mylist.pop_front();
20     }
21     cout << "\nFinal size of mylist is " << mylist.size() << '\n';
22 
23     // list::push_back
24     int myint;
25     cout << "Please enter some integers (enter 0 to end):\n";
26     do {
27         cin >> myint;
28         mylist.push_back (myint);
29     } while (myint);
30     cout << "mylist contains:";
31     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
32         cout << ' ' << *it;
33     cout << '\n';
34 
35     // list::pop_back
36     while (!mylist.empty()) {
37         cout << ' ' <<  mylist.back();
38         mylist.pop_back();
39     }
40     cout << "\nFinal size of mylist is " << mylist.size() << '\n';
41 
42     return 0;
43 }
輸出結果:
mylist contai: 300 200 100 100
Popping out the elements in mylist: 300 200 100 100
Final size of mylist is 0
Please enter some integers (enter 0 to end):
56 23 8 5 6 0
mylist contains: 56 23 8 5 6 0
0 6 5 8 23 56
Final size of mylist is 0
  • insert  //插入元素:

iterator insert (iterator position, const value_type& val);  //position是要插入的這個list的迭代器,val是要插入的值

void insert (iterator position, size_type n, const value_type& val);  //從該list容器中的position位置處開始,插入n個值為val的元素

template <class InputIterator>

void insert (iterator position, InputIterator first, InputIterator last);  //first,last是我們選擇的把值插入到這個list中的值所在的容器的迭代器

 1 // inserting into a list
 2 #include <iostream>
 3 #include <list>
 4 #include <vector>
 5 using namespace std;
 6 int main () {
 7     list<int> mylist;
 8     list<int>::iterator it;
 9     // 初始化
10     for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5
11     it = mylist.begin();
12     ++it;       // 迭代器it現在指向數字2                      ^
13     //在i0t指向的位置出插入元素10
14     mylist.insert (it,10);                        // 1 10 2 3 4 5
15 
16     // "it" 仍然指向數字2                                   ^
17     //在it指向的位置出插入兩個元素20
18     mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5
19 
20     --it;       // 現在it指向數字20                             ^
21 
22     vector<int> myvector (2,30); //創建vector容器,並初始化為含有2個值為30的元素
23     //將vector容器的值插入list中
24     mylist.insert (it,myvector.begin(),myvector.end());
25                                                 // 1 10 20 30 30 20 2 3 4 5
26     //it仍然指向數字20                            //               ^
27     cout << "mylist contains:";
28     for (it = mylist.begin(); it != mylist.end(); ++it)
29         cout << ' ' << *it;
30     cout << '\n';
31 
32     return 0;
33 }
輸出結果:
mylist contains: 1 10 20 30 30 20 2 3 4 5

 

  • erase  //刪除元素:

iterator erase (iterator position);  //刪除迭代器position指向的值,也可以不用變量接收其返回值

iterator erase (iterator first, iterator last);  //刪除[first, last)中的值,也可以不用變量接收其返回值

 1 // erasing from list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7     list<int>::iterator it1,it2;
 8 
 9     // set some values:
10     for (int i = 1; i < 10; ++i) mylist.push_back(i*10);
11 
12                                 // 10 20 30 40 50 60 70 80 90
13     it1 = it2 = mylist.begin(); // ^^
14     advance (it2,6);            // ^                 ^
15     ++it1;                      //    ^              ^
16 
17     it1 = mylist.erase (it1);   // 10 30 40 50 60 70 80 90
18                                 //    ^           ^
19 
20     it2 = mylist.erase (it2);   // 10 30 40 50 60 80 90
21                                 //    ^           ^
22 
23     ++it1;                      //       ^        ^
24     --it2;                      //       ^     ^
25     //沒有變量接收其返回值
26     mylist.erase (it1,it2);     // 10 30 60 80 90
27                                 //       ^
28     cout << "*it1 : " << *it1 << endl;
29     cout << "mylist contains:";
30     for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
31         cout << ' ' << *it1;
32     cout << '\n';
33 
34   return 0;
35 }
輸出結果:
it1 : 40
mylist contains: 10 30 60 80 90

 

  • swap  //交換兩個list的內容

void swap(list& x);  //要交換的兩個列表的存儲的元素的類型必須是一樣的,列表大小可以不同

 1 // swap lists
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> first (3,100);   // 三個值為100的元素
 7     list<int> second (5,200);  // 五個值為200的元素
 8 
 9     first.swap(second);
10 
11     cout << "first contains:";
12     for (list<int>::iterator it = first.begin(); it != first.end(); it++)
13         cout << ' ' << *it;
14     cout << '\n';
15 
16     cout << "second contains:";
17     for (list<int>::iterator it = second.begin(); it != second.end(); it++)
18         cout << ' ' << *it;
19     cout << '\n';
20 
21     return 0;
22 }
輸出結果:
first contains: 200 200 200 200 200
second contains: 100 100 100

 

  • resize  //調整list大小

void resize (size_type n, value_type val = value_type());  //將list大小調整為能容納n個元素,若n大於當前list大小,則會從list末尾一直插入val值,直到list大小滿足n;

 1 // resizing list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7 
 8     // 初始化
 9     for (int i = 1; i < 10; ++i) mylist.push_back(i);
10 
11     mylist.resize(5);
12     mylist.resize(8,100);
13     mylist.resize(12);
14 
15     cout << "mylist contains:";
16     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
17         cout << ' ' << *it;
18     cout << '\n';
19 
20   return 0;
21 }
輸出結果:
mylist contains: 1 2 3 4 5 100 100 100 0 0 0 0

 

  • clear  //清空list

void clear();  //刪除list的所有元素

 1 // clearing lists
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7     list<int>::iterator it;
 8 
 9     mylist.push_back (100);
10     mylist.push_back (200);
11     mylist.push_back (300);
12 
13     cout << "mylist contains:";
14     for (it = mylist.begin(); it != mylist.end(); ++it)
15         cout << ' ' << *it;
16     cout << '\n';
17 
18     mylist.clear();
19     mylist.push_back (1101);
20     mylist.push_back (2202);
21 
22     cout << "mylist contains:";
23     for (it = mylist.begin(); it != mylist.end(); ++it)
24         cout << ' ' << *it;
25     cout << '\n';
26 
27   return 0;
28 }
輸出結果:
mylist contains: 100 200 300
mylist contains: 1101 2202

 

Operations:

  • splice  //將一個list中的值移到另一個list中

void splice (iterator position, list& x);  //將列表x中的所有元素移到當前list中,從當前列表的position指向的位置開始,此時列表x為空

void splice (iterator position, list& x, iterator i);  //將列表x中迭代器 i 指向的元素移到當前list的position指向的位置處,由於i指向的元素從列表x中被移除,所以迭代器 i 此時是invalid的;position是當前列表的迭代器,i是列表x的迭代器

void splice (iterator position, list& x, iterator first, iterator last);  //將列表x中[first, last)的元素移到當前list中,從position指向的位置開始;first, last是列表x的迭代器

 1 // splicing lists
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist1, mylist2;
 7     list<int>::iterator it;
 8 
 9     // 初始化
10     for (int i = 1; i <= 4; ++i)
11        mylist1.push_back(i);      // mylist1: 1 2 3 4
12 
13     for (int i = 1; i <= 3; ++i)
14        mylist2.push_back(i*10);   // mylist2: 10 20 30
15 
16     it = mylist1.begin();
17     ++it;                         // 指向數字2
18 
19     mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
20                                   // mylist2 (empty)
21                                   // "it" 仍然指向數字2
22 
23     mylist2.splice (mylist2.begin(),mylist1, it);
24                                 // mylist1: 1 10 20 30 3 4
25                                 // mylist2: 2
26                                 // "it" 此時已經無效了
27     it = mylist1.begin();
28     advance(it,3);           // "it" 指向數字30
29 
30     mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
31                                 // mylist1: 30 3 4 1 10 20
32 
33     cout << "mylist1 contains:";
34     for (it = mylist1.begin(); it != mylist1.end(); ++it)
35         cout << ' ' << *it;
36     cout << '\n';
37 
38     cout << "mylist2 contains:";
39     for (it = mylist2.begin(); it != mylist2.end(); ++it)
40         cout << ' ' << *it;
41     cout << '\n';
42 
43   return 0;
44 }
輸出結果:
mylist1 contains: 30 3 4 1 10 20
mylist2 contains: 2

 

  • remove  //刪除list中特定的值

void remove (const value_type& val);  //從list中刪除所有值為val的元素

 1 // remove from list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     int myints[]= {17, 89, 7, 89, 14};
 7     list<int> mylist (myints,myints+5);
 8 
 9     mylist.remove(89);
10 
11     cout << "mylist contains:";
12     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
13         cout << ' ' << *it;
14     cout << '\n';
15 
16     return 0;
17 }
輸出結果:
mylist contains: 17 7 14

 

  • remove_if  //按條件刪除

template <class Predicate>

void remove_if (Predicate pred);  //pred可以是一個函數,也可以是一個class,但它需要有一個參數,且參數類型跟list中存儲元素類型相同,滿足條件就返回true

 1 // list::remove_if
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 // a predicate implemented as a function:
 6 bool single_digit (const int& value) { return (value < 10); }
 7 
 8 // a predicate implemented as a class:
 9 struct is_odd {
10   //重載操作符 ()
11   bool operator() (const int& value) { return (value % 2) == 1; }
12 };
13 
14 int main () {
15     int myints[] = {15, 36, 7, 17, 20, 39, 4, 1};
16     list<int> mylist (myints, myints + 8);   // 15 36 7 17 20 39 4 1
17 
18     mylist.remove_if (single_digit);           // 15 36 17 20 39
19 
20     mylist.remove_if (is_odd());               // 36 20
21 
22     cout << "mylist contains:";
23     for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
24         cout << ' ' << *it;
25     cout << '\n';
26 
27     return 0;
28 }
輸出結果:
mylist contains: 36 20

 

  • unique  //刪除重復值

void unique();  //只能刪除相鄰的重復元素,然後保留第一個值,因此這個函數只對排好序的list有用

template <class BinaryPredicate>

void unique (BinaryPredicate binary_pred);  //binary_pred可以是函數,也可以是class,但它需要有兩個參數,且類型跟list中存儲的值類型相同,滿足某個條件就返回true

 1 // list::unique
 2 #include <iostream>
 3 #include <cmath>
 4 #include <list>
 5 using namespace std;
 6 // a binary predicate implemented as a function:
 7 bool same_integral_part (double first, double second) { return ( int(first)==int(second) ); }
 8 
 9 // a binary predicate implemented as a class:
10 struct is_near {
11     bool operator() (double first, double second) { return (fabs(first-second)<5.0); }
12 };
13 
14 int main () {
15     double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
16                        12.77, 73.35, 72.25, 15.3, 72.25 };
17     list<double> mylist (mydoubles,mydoubles+10);
18     cout << "mylist contains:";
19     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
20         cout << ' ' << *it;
21     cout << '\n';
22     
23     mylist.unique();
24     cout << "mylist contains:";
25     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
26         cout << ' ' << *it;
27     cout << '\n';
28 
29     mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
30                              // 15.3,  72.25, 72.25, 73.0,  73.35
31 
32     mylist.unique();           //  2.72,  3.14, 12.15, 12.77
33                              // 15.3,  72.25, 73.0,  73.35
34     cout << "mylist contains:";
35     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
36         cout << ' ' << *it;
37     cout << '\n';
38 
39     mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15
40                                        // 15.3,  72.25, 73.0
41     cout << "mylist contains:";
42     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
43         cout << ' ' << *it;
44     cout << '\n';
45 
46     mylist.unique (is_near());           //  2.72, 12.15, 72.25
47 
48     cout << "mylist contains:";
49     for (list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
50         cout << ' ' << *it;
51     cout << '\n';
52 
53   return 0;
54 }
輸出結果:
mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25
mylist contains: 12.15 2.72 73 12.77 3.14 12.77 73.35 72.25 15.3 72.25
mylist contains: 2.72 3.14 12.15 12.77 15.3 72.25 73 73.35
mylist contains: 2.72 3.14 12.15 15.3 72.25 73
mylist contains: 2.72 12.15 72.25


 

  • merge  //合並有序的list

void merge(list &x);  //會將列表x中的元素按默認的順序移入當前列表當中,此時列表x為空,當前列表仍為有序列表

template<class Compare>

void merge (list& x, Compare comp);  //comp可以為一個函數,要求有兩個參數且參數類型跟list中存儲的元素類型相同,當滿足條件時返回true,merge就按照這個條件將兩個列表合並

 1 // list::merge
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 // compare only integral part:
 6 bool mycomparison (double first, double second) { return ( (first)<(second) ); }
 7 
 8 int main () {
 9     list<double> first, second;
10 
11     first.push_back (3.1);
12     first.push_back (2.2);
13     first.push_back (2.9);
14 
15     second.push_back (3.7);
16     second.push_back (7.1);
17     second.push_back (1.4);
18 
19     first.sort();
20     second.sort();
21 
22     first.merge(second);
23     cout << "first contains:";
24     for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
25         cout << ' ' << *it;
26     cout << '\n';
27     // (second 現在為空)
28 
29     second.push_back (2.1);
30     second.push_back(2.5);
31 
32     first.merge(second,mycomparison);
33     cout << "first contains:";
34     for (list<double>::iterator it = first.begin(); it != first.end(); ++it)
35         cout << ' ' << *it;
36     cout << '\n';
37 
38   return 0;
39 }
輸出結果:
first contains: 1.4 2.2 2.9 3.1 3.7 7.1
first contains: 1.4 2.1 2.2 2.5 2.9 3.1 3.7 7.1

 

  • sort  //排序

void sort();  //默認升序排列

template <class Compare>

void sort (Compare comp);  //comp可以是一個函數,要求有兩個參數,類型跟list中元素類型相同,滿足條件時返回true,sort()函數就按照comp中制定的規則對元素進行排序

 1 // list::sort
 2 #include <iostream>
 3 #include <list>
 4 #include <string>
 5 #include <cctype>
 6 using namespace std;
 7 // comparison, not case sensitive.
 8 bool compare_nocase (const string& first, const string& second) {
 9     unsigned int i = 0;
10     while ((i < first.length()) && (i < second.length()) ) {
11         //將大寫字母轉為小寫字母
12         if (tolower(first[i]) < tolower(second[i])) return true;
13         else if (tolower(first[i]) > tolower(second[i])) return false;
14         ++i;
15     }
16     return ( first.length() < second.length() );
17 }
18 
19 int main () {
20     list<string> mylist;
21     list<string>::iterator it;
22     mylist.push_back ("one");
23     mylist.push_back ("two");
24     mylist.push_back ("Three");
25 
26     mylist.sort();
27 
28     cout << "mylist contains:";
29     for (it = mylist.begin(); it != mylist.end(); ++it)
30         cout << ' ' << *it;
31     cout << '\n';
32 
33     mylist.sort(compare_nocase);
34 
35     cout << "mylist contains:";
36     for (it = mylist.begin(); it != mylist.end(); ++it)
37         cout << ' ' << *it;
38     cout << '\n';
39 
40   return 0;
41 }
輸出結果:
mylist contains: Three one two
mylist contains: one Three two

 

  • reverse  //逆序:

void reverse();  //將list中元素的順序逆轉過來

 1 // reversing list
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7 
 8     for (int i = 1; i < 10; ++i) mylist.push_back(i);
 9 
10     mylist.reverse();
11 
12     cout << "mylist contains:";
13     for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
14         cout << ' ' << *it;
15     cout << '\n';
16 
17   return 0;
18 }
輸出結果:
mylist contains: 9 8 7 6 5 4 3 2 1

 

Observers:

  • get_allocator  //返回一個跟該list有關的分配器對象

allocator_type get_allocator() const;  //可以用來給數組動態分配空間

 1 // list::get_allocator
 2 #include <iostream>
 3 #include <list>
 4 using namespace std;
 5 int main () {
 6     list<int> mylist;
 7     int * p;
 8 
 9     // allocate an array of 5 elements using mylist's allocator:
10     p = mylist.get_allocator().allocate(5);
11 
12     // assign some values to array
13     for (int i = 0; i < 5; ++i) p[i] = i;
14 
15     cout << "The allocated array contains:";
16     for (int i = 0; i < 5; ++i) cout << ' ' << p[i];
17     cout << '\n';
18 
19     mylist.get_allocator().deallocate(p,5);
20 
21     return 0;
22 }
輸出結果:
The allocated array contains: 9 8 7 6 5 4 3 2 1

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved