C++ list模板类介绍

时间:2014-05-22 11:44:49   收藏:0   阅读:433

简介

         List是一种可在常数时间内在任何位置执行插入和删除操作的顺序容器。list是双向链表,其迭代器是双向的。与其他顺序容器(array, vector, deque)相比,list容器在任意位置执行插入、提取、和移动元素的操作更高效,但它不能通过在容器中的位置直接获取元素。

成员函数


复制控制


list::list()

         构造函数:构造一个新的list对象,根据参数初始化list容器的内容。

list::~list()

         析构函数:销毁以list对象。

list::operator=

         为容器分配新的内容,代替当前的内容,随之修改其大小。

示例代码

  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()

        返回一个反转迭代器,指向list的最后一个元素。返回值类型:reverse_iterator/reverse_const_iterator。

list::rend()

         返回一个反转迭代器,指向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中元素个数。返回值类型list::size_type。

list::max_size()

         返回list能够容纳元素的最大个数。返回值类型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分配新的内容,以代替当前内容,并随之改变其大小。

list::emplace_front()

        在list的前端插入一个元素。插入的元素由其构造函数创建。

list::push_front()

         在list的前端插入一个元素。与emplace_front()不同,它的插入方式是将一个已存在的元素复制或移动到list前端。

list::pop_front()

         删除list的第一个元素。

list::emplace_back()

         在list的末端插入一个元素。插入的元素由其构造函数创建。

list::push_back()

         在list的末端插入一个元素。与emplace_back()不同,它的插入方式是将一个已存在的元素复制或移动到list末端。

list::pop_back()

         删除list的最后一个元素。

list::emplace()

         在指定位置插入一个元素。插入的元素由其构造函数创建。

list::insert()

         在指定位置插入一个或多个元素。对于插入大量元素来说是非常高效的。

list::erase()

         从list中删除指定位置的一个或一定范围的元素。

list::swap()

         交换两个list中的元素。两个list类型必须相同,大小可以不同。

list::resize()

         重新分配list的大小,使其能容纳指定数量的元素。

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中的元素转移到list B的指定位置,并将A中被转移的元素删除。

list::remove()

         将list中指定的元素删除。

list::remove_if()

         根据判断条件删除list的元素,如果条件为真则删除该元素。

list::unique()

         删除list中具有相同值的元素,只保留第一个。也可以根据条件删除具有相同条件的元素,只保留第一个。

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. }  

C++ list模板类介绍,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!