日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C++ list模板類介紹

 ONLY_影 2015-02-02

簡介

         List是一種可在常數(shù)時間內(nèi)在任何位置執(zhí)行插入和刪除操作的順序容器。list是雙向鏈表,其迭代器是雙向的。與其他順序容器(array, vector, deque)相比,list容器在任意位置執(zhí)行插入、提取、和移動元素的操作更高效,但它不能通過在容器中的位置直接獲取元素。

成員函數(shù)


復制控制


list::list()

         構(gòu)造函數(shù):構(gòu)造一個新的list對象,根據(jù)參數(shù)初始化list容器的內(nèi)容。

list::~list()

         析構(gòu)函數(shù):銷毀以list對象。

list::operator=

         為容器分配新的內(nèi)容,代替當前的內(nèi)容,隨之修改其大小。

示例代碼

  1. #include<iostream>  
  2. #include<list>  
  3.    
  4. using namespace std;  
  5.    
  6. void  
  7. print(list<int> l)  
  8. {  
  9.     for(list<int>::iterator it = l.begin(); it != l.end(); ++it)  
  10.     {  
  11.        cout << *it << "";  
  12.     }  
  13.     cout << endl;  
  14. }  
  15.    
  16. int  
  17. main(void)  
  18. {  
  19.     // list::list()  
  20.     list<int> first;  
  21.     list<int> second(5, 10);  
  22.     list<int> third(second.begin(), second.end());  
  23.     list<int> fourth(third);  
  24.     int arr[] = {1, 2, 3, 4, 5};  
  25.     list<int> fifth(arr, arr + sizeof(arr)/sizeof(int));  
  26.     print(second);  
  27.     print(third);  
  28.     print(fourth);  
  29.     print(fifth);  
  30.      
  31.     // list::operator=  
  32.     first = fifth;  
  33.     print(first);  
  34.      
  35.     return(0);  
  36. }  

Iterator


list::begin()

         返回一個迭代器,指向list的第一個元素。返回值類型:iterator/const_iterator。

list::end()

         返回一個迭代器,指向list的最后一個元素的下一個位置。返回值類型:iterator/const_iterator。

list::rbegin()

        返回一個反轉(zhuǎn)迭代器,指向list的最后一個元素。返回值類型:reverse_iterator/reverse_const_iterator。

list::rend()

         返回一個反轉(zhuǎn)迭代器,指向list的第一個元素的前一個位置。返回值類型:reverse_iterator/reverse_const_iterator。

list::cbegin()

         begin()的const版本。

list::cend()

         end()的const版本

list::crbegin()

         rbegin()的cosnt版本。

list::crend()

         rend()的const版本。

示例代碼

  1. #include <iostream>  
  2. #include <list>  
  3.   
  4. using namespace std;  
  5.   
  6. int  
  7. main(void)  
  8. {  
  9.     int arr[] = {1, 2, 3, 4, 5};  
  10.     list<int> l(arr, arr + sizeof(arr)/sizeof(int));  
  11.       
  12.     // list::begin end  
  13.     for(list<int>::iterator it = l.begin(); it != l.end(); ++it)  
  14.     {  
  15.         cout << *it << " ";  
  16.     }  
  17.     cout << endl;  
  18.       
  19.     // list::rbegin rend  
  20.     for(list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)  
  21.     {  
  22.         cout << *it << " ";  
  23.     }  
  24.     cout << endl;  
  25.       
  26.     // list::cbegin cend  
  27.     for(list<int>::const_iterator it = l.cbegin(); it != l.cend(); ++it)  
  28.     {  
  29.         cout << *it << " ";  
  30.     }  
  31.     cout << endl;  
  32.       
  33.     // list::cbegin cend  
  34.     for(list<int>::const_reverse_iterator it = l.crbegin(); it != l.crend(); ++it)  
  35.     {  
  36.         cout << *it << " ";  
  37.     }  
  38.     cout << endl;  
  39.       
  40.     return 0;  
  41. }  

Capacity


list::empty()

         測試list是否為空,空返回true,否則返回false。

list::size()

         返回list中元素個數(shù)。返回值類型list::size_type。

list::max_size()

         返回list能夠容納元素的最大個數(shù)。返回值類型list::size_type。

示例代碼

 

  1. #include<iostream>  
  2. #include<list>  
  3.    
  4. using namespace std;  
  5.    
  6. int  
  7. main(void)  
  8. {  
  9.     list<int> l(10, 20);  
  10.      
  11.     // list::empty  
  12.     if(l.empty()){  
  13.        cout << "Listis empty" << endl;  
  14.     }else{  
  15.        cout << "Listis not empty" << endl;  
  16.     }  
  17.      
  18.     cout << "Listsize : " << l.size() << endl;  
  19.     cout << "Listmax size : " << l.size() << endl;  
  20.      
  21.     return(0);  
  22. }  

Element access


list::front()

         返回list第一個元素的引用。返回值類型reference/const_reference。

list::end()

         返回list最后一個元素的引用。返回值類型reference/const_reference。

示例代碼

  1. #include<iostream>  
  2. #include<list>  
  3.    
  4. using namespace std;  
  5.    
  6. int  
  7. main(void)  
  8. {  
  9.     int arr[] = {1, 2, 3, 4, 5};  
  10.     list<int> l(arr, arr + sizeof(arr)/sizeof(int));  
  11.      
  12.     // list::front  
  13.     cout << "Thefirst element is : " << l.front() << endl;  
  14.     // list::back  
  15.     cout << "Thesecond element is : " << l.back() << endl;  
  16.    
  17.     return(0);  
  18. }  

Modifiers


list::assign()

         為list分配新的內(nèi)容,以代替當前內(nèi)容,并隨之改變其大小。

list::emplace_front()

        在list的前端插入一個元素。插入的元素由其構(gòu)造函數(shù)創(chuàng)建。

list::push_front()

         在list的前端插入一個元素。與emplace_front()不同,它的插入方式是將一個已存在的元素復制或移動到list前端。

list::pop_front()

         刪除list的第一個元素。

list::emplace_back()

         在list的末端插入一個元素。插入的元素由其構(gòu)造函數(shù)創(chuàng)建。

list::push_back()

         在list的末端插入一個元素。與emplace_back()不同,它的插入方式是將一個已存在的元素復制或移動到list末端。

list::pop_back()

         刪除list的最后一個元素。

list::emplace()

         在指定位置插入一個元素。插入的元素由其構(gòu)造函數(shù)創(chuàng)建。

list::insert()

         在指定位置插入一個或多個元素。對于插入大量元素來說是非常高效的。

list::erase()

         從list中刪除指定位置的一個或一定范圍的元素。

list::swap()

         交換兩個list中的元素。兩個list類型必須相同,大小可以不同。

list::resize()

         重新分配list的大小,使其能容納指定數(shù)量的元素。

list::clear

         刪除list中的所有元素。

示例代碼

  1. #include<iostream>  
  2. #include<list>  
  3. #include<vector>  
  4.    
  5. using namespace std;  
  6.    
  7. void  
  8. print(list<int> l)  
  9. {  
  10.     for(list<int>::iterator it = l.begin(); it != l.end(); ++it)  
  11.     {  
  12.        cout << *it << "";  
  13.     }  
  14.     cout << endl;  
  15. }  
  16.    
  17. int  
  18. main(void)  
  19. {  
  20.     list<int> first;  
  21.     list<int> second;  
  22.     list<int> third;  
  23.     int arr[] = {1, 2, 3, 4, 5};  
  24.      
  25.     // list::assign  
  26.     first.assign(5, 10);  
  27.     second.assign(first.begin(), first.end());  
  28.     third.assign(arr, arr + sizeof(arr)/sizeof(int));  
  29.     print(first);  
  30.     print(second);  
  31.     print(third);  
  32.      
  33.     // list::emplace emplace_front emplace_back  
  34.     list<pair<intchar>> pl;  
  35.     pl.emplace_front(1, 'a');  
  36.     pl.emplace_front(2, 'b');  
  37.     pl.emplace_back(3, 'c');  
  38.     pl.emplace(pl.end(), 4, 'd');  
  39.     for(pair<intchar> x :pl)  
  40.     {  
  41.         cout<< x.first << "" << x.second << endl;  
  42.     }  
  43.      
  44.     //list::push_front push_back  
  45.     third.push_front(10);  
  46.     third.push_back(20);  
  47.     print(third);  
  48.      
  49.     // list::insert  
  50.     first.insert(first.begin(), 20);  
  51.     first.insert(first.end(), 2, 30);  
  52.     vector<int> vec(2, 40);  
  53.     first.insert(first.end(), vec.begin(), vec.end());  
  54.     print(first);  
  55.      
  56.     // list::erase  
  57.     second.erase(second.begin());  
  58.     print(second);  
  59.     second.erase(++second.begin(), second.end());  
  60.     print(second);  
  61.      
  62.     // list::swap  
  63.     second.swap(first);  
  64.     print(first);  
  65.     print(second);  
  66.      
  67.     // list::resize  
  68.     third.resize(5);  
  69.     print(third);  
  70.     third.resize(10);  
  71.     print(third);  
  72.     third.resize(15, 100);  
  73.     print(third);  
  74.      
  75.     // list::clear  
  76.     third.clear();  
  77.     cout << "Thesize of third is : " << third.size() << endl;  
  78.    
  79.     return(0);  
  80. }  

Operations


list::splice()

         將一個list A中的元素轉(zhuǎn)移到list B的指定位置,并將A中被轉(zhuǎn)移的元素刪除。

list::remove()

         將list中指定的元素刪除。

list::remove_if()

         根據(jù)判斷條件刪除list的元素,如果條件為真則刪除該元素。

list::unique()

         刪除list中具有相同值的元素,只保留第一個。也可以根據(jù)條件刪除具有相同條件的元素,只保留第一個。

list::merge()

         合并兩個list,在合并之前兩個list應該先排序,合并之后的list依然有序。也可以自定義排序的條件。

list::sort()

         對list中的元素進行排序,變更它們在容器中的位置。sort()還可以按給定條件進行排序。

list::reverse()

         改變list中元素的順序。

示例程序

  1. #include<iostream>  
  2. #include<cmath>  
  3. #include<list>  
  4.    
  5. using namespace std;  
  6.    
  7. void  
  8. print(list<int> l)  
  9. {  
  10.     for(list<int>::iterator it = l.begin(); it != l.end(); ++it)  
  11.     {  
  12.        cout << *it << "";  
  13.     }  
  14.     cout << endl;  
  15. }  
  16.    
  17. bool  
  18. single_dight(const int &val)  
  19. {  
  20.     return val > 10;  
  21. }  
  22.    
  23. bool  
  24. is_near(int first, int second)  
  25. {  
  26.     return(fabs(first - second) < 5);  
  27. }  
  28.    
  29. bool  
  30. reverse(int first, int second)  
  31. {  
  32.     return((first -second) > 0);  
  33. }  
  34.    
  35. int  
  36. main(void)  
  37. {  
  38.     list<int> first(2, 10);  
  39.     list<int> second(2, 20);  
  40.     list<int>::iteratorit;  
  41.      
  42.     // list::splice  
  43.     it = first.begin();  
  44.     first.splice(it, second);  
  45.     print(first);  
  46.     if(second.empty())  
  47.     {  
  48.        cout << "Secondis empty" << endl;  
  49.     }  
  50.     else  
  51.     {  
  52.        cout << "Secondis not empty" << endl;  
  53.     }  
  54.     // it still point to 10(the 3th element)  
  55.     cout << *it << endl;  
  56.     second.splice(second.begin(), first, it);  
  57.     print(second);  
  58.      
  59.     it = first.begin();  
  60.     advance(it, 2);  
  61.     print(first);  
  62.     first.splice(first.begin(), first, it, first.end());  
  63.     print(first);  
  64.      
  65.     // list::remove  
  66.     cout << "Beforeremove : ";  
  67.     print(first);  
  68.     first.remove(10);  
  69.     cout << "Afterremove : ";  
  70.     print(first);  
  71.      
  72.     // list::remove_if  
  73.     cout << "Beforeremove_if : ";  
  74.     first.push_back(10);  
  75.     print(first);  
  76.     first.remove_if(single_dight);  
  77.     cout << "Afterremove_if : ";  
  78.     print(first);  
  79.      
  80.     // list::unique  
  81.     first.push_back(20);  
  82.     first.push_back(20);  
  83.     first.push_back(21);  
  84.     cout << "Beforecall unique() : ";  
  85.     print(first);  
  86.     first.unique();  
  87.     cout << "Aftercall unique() : ";  
  88.     print(first);  
  89.      
  90.     cout << "Beforecall unique() : ";  
  91.     print(first);  
  92.     first.unique(is_near);  
  93.     cout << "Aftercall unique() : ";  
  94.     print(first);  
  95.      
  96.     // list::merge  
  97.     first.push_back(5);  
  98.     first.push_back(12);  
  99.     first.sort();  
  100.     print(first);  
  101.     second.push_back(9);  
  102.     second.push_back(17);  
  103.     second.sort();  
  104.     print(second);  
  105.      
  106.     first.merge(second);  
  107.     print(first);  
  108.      
  109.     // list::sort  
  110.     first.sort(reverse);  
  111.     print(first);  
  112.      
  113.     // list::reverse  
  114.     first.reverse();  
  115.     print(first);  
  116.     return(0);  
  117. }  

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多