2、【C++ STL】容器之序列式容器

一、序列式容器

  Vector中所采用的数据结构非常简单:线性连续空间。当分配空间被占满而仍然需要添加元素时,vector便会进行一场空间重新配置的大工程!在这里,程序员需要注意的是,一旦引起空间重新配置,之前指向原vector的所有迭代器就都失效了,这一点在工程中容易引起bug

         List则对空间的运用有绝对的精准,一点也不浪费。注意,list内部构成的实际是一个环状的双向链表!所以只需要一个指针,便可以完整地表现整个链表。

         Deque相比于vector而言,它没有容量的概念,因为deque是动态地以分段连续空间组合而成,随时都可以增加一段新的空间并链接起来。为了使得deque在逻辑上看起来是连续空间,在STL内部实现实际是使用了一块map(不是STL中的map容器)作为主控,map是一小块连续空间,其中每个元素都是指针,指向另一段较大的连续线性空间,称为缓冲区,这些缓冲区才是真正存放deque元素的主体。

、Vector

  (1)将元素置于一个动态数组中加以管理。

  (2)可以随机存取元素(用索引字节存取)

  (3)数组尾部添加或移除元素非常快速。当在头部或中部安插元素比较费时。

1、vector成员函数

1     c.push_back(elem)在尾部插入一个elem数据。
2     c.pop_back()删除末尾的数据。

【示例】

1     vector<int> v;
2     v.push_back(1);
3     v.pop_back();
1     c.assign(beg,end)将[beg,end)一个左闭右开区间的数据赋值给c。
2     c.assign (n,elem)将n个elem的拷贝赋值给c。

【示例】

1      vector<int> v1,v2, v;
2      v1.push_back(10);
3      v1.push_back(20);
4      v2.push_back(30);
5      v2.assign(v1.begin(),v1.end());
6      v.assign(5, 10);
1      c.at(int index)传回索引为index的数据,如果index越界,抛出out_of_range异常。
2      c.begin()返回指向第一个数据的迭代器。
3      c.end()返回指向最后一个数据之后的迭代器。
4      c.rbegin()返回逆向队列的第一个数据,即c容器的最后一个数据。
5      c.rend()返回逆向队列的最后一个数据的下一个位置,即c容器的第一个数据再往前的一个位置。 

【示例】

 1 vector<int> v;
 2 v.push_back(1);
 3 v.push_back(2);
 4 v.push_back(3);
 5 cout << v.at(2) << endl;
 6 vector<int>::iterator it;
 7 for(it = v.begin();it!=v.end();it++){
 8     cout << *it << "	";
 9 }
10 cout << endl;
1 vector<int> v;
2 v.push_back(1);
3 v.push_back(2);
4 v.push_back(3);
5 vector<int>::reverse_iterator it;
6 for(it = v.rbegin();it!=v.rend();it++){
7     cout << *it << "	";
8 }
9 cout << endl;
1     c.clear()移除容器中的所有数据。
2     c.empty()判断容器是否为空。
3     c.front()返回第一个数据。
4     c.back()传回最后一个数据,不检查这个数据是否存在。

【示例】

1 vector<int> v;
2 v.push_back(1);
3 v.push_back(2);
4 v.push_back(3);
5 if(!vec.empty()){
6     cout << “the first number is:” << v.front() << endl;
7     cout << “the last number is:” << v.back() << endl;
8 }
1 c.insert(pos,elem) 在pos位置插入一个elem的拷贝,返回插入的值的迭代器。
2 c.insert(pos,n,elem)在pos位置插入n个elem的数据,无返回值。
3 c.insert(pos,beg,end)在pos位置插入在[beg,end)区间的数据,无返回值。

【示例】

1 vector<int> v;
2 v.insert(v.begin(),10);
3 v.insert(v.begin(),2,20);
4 v.insert(v.begin(),v1.begin(),v1.begin()+2);
1 c.erase(pos)删除pos位置的数据,传回下一个数据的位置。
2 c.erase(beg,end)删除[beg,end)区间的数据,传回下一个数据的位置。

【示例】

1 vector<int> v;
2 v.push_back(1);
3 v.push_back(2);
4 v.push_back(3);
5 v.erase(v.begin());
6 //v.erase(v.begin(), v.end());
1 c1.swap(c2)将c1和c2交换。
2 swap(c1,c2)同上。

【示例】

1 vector<int> v1,v2,v3;
2 v1.push_back(10);
3 v2.swap(v1);
4 swap(v3,v1);

【vector综合示例】

  1 #include <iostream>
  2 #include <vector>
  3 #include <algorithm>
  4 
  5 using namespace std;
  6 
  7 //add elements
  8 void add(vector<int> &vec)
  9 {
 10     int temp, N;
 11     cout << "Please input the size of vec:";
 12     cin >> N;
 13 
 14     cout << "请输入" << N << "个整数(空格分割,换行结束):";
 15     for(int i = 0; i < N; i++)
 16     {
 17         cin >> temp;
 18         vec.push_back(temp);
 19     }
 20 }
 21 
 22 //inset new elements
 23 void insertElements(vector<int> &vec)
 24 {
 25     int index, num;
 26     cout << "请输入要插入的位置:";
 27     cin >> index;
 28     cout << "请输入要插入的值:";
 29     cin >> num;
 30     vec.insert(vec.begin()+index, num);
 31 }
 32 
 33 //delete elements
 34 void deleteElements(vector<int> &vec)
 35 {
 36     int i, place, from, to;
 37 
 38     cout << "   ***** 1、删除1个元素  2、删除部分元素  3、删除所有元素 *****   " << endl;
 39     cout << "请输入菜单编号:" << endl;
 40 
 41     cin >> i;
 42     switch(i)
 43     {
 44     case 1:
 45         {
 46             cout << "请输入要删除的元素索引:" << endl;
 47             cin >> place;
 48             if(place < 0 || place+1 > vec.size())
 49                 cout << "error" << endl;
 50             else
 51                 vec.erase(vec.begin()+place);
 52         }
 53         break;
 54     case 2:
 55         {
 56             cout << "请输入要删除部分的起始位置(结束位置元素不删除):" << endl;
 57             cin >> from >> to;
 58             if(from < 0 || to+1 > vec.size())
 59                 cout << "error" << endl;
 60             else
 61                 vec.erase(vec.begin()+from, vec.begin()+to);//删除区间内,包含from不包含to
 62         }
 63         break;
 64     case 3:
 65         {
 66             vec.clear();
 67         }
 68         break;
 69     }
 70 }
 71 
 72 //sort all elements
 73 void Sort(vector<int> &vec)
 74 {
 75     int i;
 76     cout << "  *** 1、升序  2、降序 ***  " << endl;
 77     cin >> i;
 78     switch(i)
 79     {
 80         case 1:sort(vec.begin(), vec.end());break;
 81         case 2:reverse(vec.begin(), vec.end());break;
 82     }
 83 }
 84 
 85 //使用迭代器来遍历vector的元素
 86 void Display(vector<int> &vec)
 87 {
 88     vector<int>::iterator it;//vector 迭代器的使用
 89     for(it = vec.begin(); it != vec.end(); it++)
 90     {
 91         cout << *it << "  ";
 92     }
 93     cout << endl;
 94 }
 95 //修改某个位置的元素
 96 void Change(vector<int> &vec)
 97 {
 98     int place;
 99     cout << "请输入要修改元素的位置";
100     cin >> place;
101     cout << "请输入新的数据值:";
102     cin >> vec[place];
103 }
104 
105 //print all elements
106 void print(vector<int> &vec)
107 {
108     for(int i = 0; i < vec.size(); i++)
109     {
110         cout << vec[i] << "  ";
111     }
112     cout << endl;
113 }
114 
115 int main()
116 {
117     vector<int> vec;
118 
119     add(vec);
120     print(vec);
121 
122     insertElements(vec);
123     Display(vec);
124 
125     deleteElements(vec);
126     print(vec);
127 
128     Change(vec);
129     Display(vec);
130 
131     return 0;
132 }

三、Deque

  (1)deque是“double-ended queue的缩写

  (2)可以随机存取元素(用索引直接存取)

  (3)数组头部和尾部添加或移除元素都非常快,当在中部或头部安插元素比较费时。

  双端队列(deque),顾名思义,两端都可以操作,插入和删除。而且,还可以在中间进行操作。内部采用线性表顺序结构,与vector不同的是,deque采用分块的线性存储结构存储数据,每块大小512字节。
  所有的deque块使用一个Map块进行管理,每个Map数据项纪录各个deque块的首地址。当考虑容器内部的内存分配策略和操作性能时,deque相对于vector更有优势,同时,也可以用下标来访问。

1、构造函数

1   deque<Elem> c 创建一个空的deque
2   deque<Elem> c1(c2) 复制一个deque。
3   deque<Elem> c(n) 创建一个deque,含有n个数据,数据均已缺省构造产生。
4   deque<Elem> c(n, elem) 创建一个含有n个elem拷贝的deque。
5   deque<Elem> c(beg,end) 创建一个以[beg;end)区间的deque。
6   ~deque<Elem>() 销毁所有数据,释放内存。

2、成员函数

1 c.begin()返回指向第一个元素的迭代器
2 c.end()返回指向最后一个元素下一个位置的迭代器
3 c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素)
4 c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)

【示例】

 1 deque<int> d {1,2,3,4,5};
 2 deque<int>::iterator it;
 3 deque<int>::reverse_iterator rit;
 4 for(it=d.begin();it!=d.end();it++){
 5     cout << *it << " ";
 6 }
 7 cout << endl;
 8 for(rit=d.rbegin(); rit!=d.rend(); rit++){
 9     cout << *rit << " ";
10 }
11 cout << endl;
1 c.assign(n,num)将n个num拷贝复制到容器c
2 c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c

【示例】

 1     deque<int> d1 {1,2,3,4,5},d2;
 2     d2.assign(2, 8);
 3     deque<int>::iterator it;
 4     cout << "d2.assign(n,num):";
 5     for(it=d2.begin();it!=d2.end();it++){
 6         cout << *it << " ";
 7     }
 8     d2.assign(d1.begin(), d1.begin()+3);
 9     cout << "d2.assign(beg,end):";
10     for(it=d2.begin();it!=d2.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;
1 c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常
2 c.operator[]下标运算符重载

【示例】

1      deque<int> d {1,2,3,4,5};
2      cout << "d.at(pos):" << d.at(2);
3      cout << "d[2] : " << d[2];
4      return 0;
1 c.empty()判断c容器是否为空
2 c.front()返回c容器的第一个元素
3 c.back()返回c容器的最后一个元素

【示例】

1      deque<int> d {1,2,3,4,5};
2      if(!d.empty()){
3          cout << "d.front():" << d.front() << endl;
4          cout << "d.back(): " << d.back() << endl;
5      }
1 c.size()返回c容器中实际拥有的元素个数
2 c.max_size()返回c容器可能存放元素的最大数量

【示例】

1      deque<int> d {1,2,3,4,5};
2      cout << "d.size():" << d.size() << endl;
3      cout << "d.max_size():" << d.max_size() << endl;
4      return 0;
1 c.clear()清除c容器中拥有的所有元素
2 c.insert(pos,elem) 在pos位置插入一个elem的拷贝,返回插入的值的迭代器。
3 c.insert(pos,n,elem)在pos位置插入n个elem的数据,无返回值。
4 c.insert(pos,beg,end)在pos位置插入在[beg,end)区间的数据,无返回值。

【示例】

 1     deque<int> d {1,2,3,4,5};
 2     deque<int>::iterator it;
 3     cout << "insert before:" ;
 4     for(it=d.begin();it!=d.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     d.insert(d.end(),22);
 9     d.insert(d.end(), 3,88);
10     int a[5] = {1,2,3,4,5};
11     d.insert(d.begin(),a,a+3);
12     cout << "insert after:" ;
13     for(it=d.begin();it!=d.end();it++){
14         cout << *it << " ";
15     }
16     d.clear();
17     cout << endl;
1 c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素
2 c.erase(beg,end)删除区间为[beg,end)之间的元素

【示例】

 1     deque<int> d {1,2,3,4,5};
 2     d.erase(d.begin());
 3     deque<int>::iterator it;
 4     cout << "erase(pos) after:" ;
 5     for(it=d.begin();it!=d.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     d.erase(d.begin(), d.begin()+3);
10     cout << "erase(beg,end) after:" ;
11     for(it=d.begin();it!=d.end();it++){
12         cout << *it << " ";
13     }
14     cout << endl;
1     c.push_back(num)在末尾位置插入元素
2     c.pop_back()删除末尾位置的元素
3     c.push_front(num)在开头位置插入元素
4     c.pop_front()删除开头位置的元素

【示例】

 1     deque<int> d {1,2,3,4,5};
 2     d.push_back(10);
 3     deque<int>::iterator it;
 4     cout << "push_back(num):" ;
 5     for(it=d.begin();it!=d.end();it++){
 6             cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     d.pop_back();
11     cout << "pop_back(num):" ;
12     for(it=d.begin();it!=d.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
16     
17     d.push_front(10);
18     cout << "push_front(num):" ;
19     for(it=d.begin();it!=d.end();it++){
20         cout << *it << " ";
21     }
22     cout << endl;
23     
24     d.pop_front();
25     cout << "pop_front(num):" ;
26     for(it=d.begin();it!=d.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;
30     return 0;
1 c.resize(num)重新指定队列的长度。(往往用来增加vector的长度,小->大 ok 大->小 没用!)

【示例】

 1     deque<int> d {1,2,3,4,5};
 2     cout << "d.size():" << d.size() << endl;
 3     d.resize(d.size()+5);
 4     cout << "d.resize() after:" << d.size() <<endl;
 5     deque<int>::iterator it;
 6     cout << "resize() after:" ;
 7     for(it=d.begin();it!=d.end();it++){
 8     cout << *it << " ";
 9     }
10     cout << endl;
1     c1.swap(c2)将c1和c2交换。
2     swap(c1,c2)同上。

【示例】

 1     deque<int> d1 {1,2,3,4,5},d2,d3;
 2     d1.swap(d2);
 3     deque<int>::iterator it;
 4     cout << "d1 swap after:" ;
 5     for(it=d1.begin();it!=d1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     cout << "d2 swap after:" ;
10     for(it=d2.begin();it!=d2.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;
14     
15     swap(d3,d2);
16     cout << "d3 swap after:" ;
17     for(it=d3.begin();it!=d3.end();it++){
18         cout << *it << " ";
19     }
20     cout << endl;

【deque综合示例】

  1 #include <iostream>
  2 #include <deque>
  3 
  4 using namespace std;
  5 
  6 
  7 int Push(deque<int> &deq)
  8 {
  9     int i, j, place, n, N;
 10 
 11     cout << "1、前插 2、后插  3、中间插" << endl;
 12     cin >> j;
 13     switch(j)
 14     {
 15     case 1://前插,后移
 16         {
 17             cout << "请输入要插入的整数个数:" << endl;
 18             cin >> N;
 19             cout << "请输入" << N << "个整数:" << endl;
 20             for(i = 0; i < N; i++)
 21             {
 22                 cin >> n;
 23                 deq.push_front(n);
 24             }
 25         }
 26         break;
 27     case 2://后插,扩张队列
 28         {
 29             cout << "请输入要输入的整数个数:" << endl;
 30             cin >> N;
 31             cout << "请输入" << N << "个整数:" << endl;
 32             for(i = 0; i < N; i++)
 33             {
 34                 cin >> n;
 35                 deq.push_back(n);
 36             }
 37         }
 38         break;
 39     case 3://中间插入,后移
 40         {
 41             cout << "请输入要插入的位置:" << endl;
 42             cin >> place;
 43             cout << "请输入要插入的数值:" << endl;
 44             cin >> n;
 45             deq.insert(deq.begin()+place, n);
 46         }
 47         break;
 48     default:
 49         cout << "error" << endl;
 50     }
 51 }
 52 void POP(deque<int> &deq)
 53 {
 54     int i, j, place, N;
 55     cout << "1、前删  2、后删  3、中间删  4、清空" << endl;
 56     cin >> j;
 57     switch(j)
 58     {
 59     case 1://前删
 60         cout << "请输入要删除的整数的个数:" << endl;
 61         cin >> N;
 62         if(N > deq.size())
 63             cout << "error" << endl;
 64         else
 65         {
 66             for(i = 0; i < N; i++)
 67             {
 68                 deq.pop_front();
 69             }
 70         }
 71         break;
 72     case 2://后删
 73         cout << "请输入你要删除的整数个数:" << endl;
 74         cin >> N;
 75         if(N > deq.size())
 76             cout << "error" << endl;
 77         else
 78         {
 79             for(i = 0; i < N; i++)
 80                 deq.pop_back();
 81         }
 82         break;
 83     case 3://中删
 84         cout << "前输入你要删除的位置(首位置为0):" << endl;
 85         cin >> place;
 86         if(place < 0 || place > deq.size())
 87             cout << "位置越界" << endl;
 88         else
 89         {
 90             deq.erase(deq.begin()+place);
 91         }
 92         break;
 93     default:
 94         cout << "输入错误" << endl;
 95     }
 96 }
 97 
 98 void getFront(deque<int> &deq)
 99 {
100     if(deq.empty())
101         cout << "the deque is empty" << endl;
102     else
103         cout << "队列顶部元素为:" << deq.front() << endl;
104 }
105 
106 void getBack(deque<int> &deq)
107 {
108     if(deq.empty())
109         cout << "the deque is empty" << endl;
110     else
111         cout << "the back of the deuqe is :" << deq.back() << endl;
112 }
113 
114 void getSize(deque<int> &deq)
115 {
116     cout << "the size of the deque is : " << deq.size() << endl;
117 }
118 
119 void Display(deque<int> &deq)
120 {
121     for(int i = 0; i < deq.size(); i++)
122         cout << deq[i] << "  ";
123     cout << endl;
124 }
125 
126 int main()
127 {
128     deque<int> deq;
129 
130     Push(deq);
131     Display(deq);
132     getSize(deq);
133     getFront(deq);
134     getBack(deq);
135 
136     POP(deq);
137     Display(deq);
138 
139 
140     return 0;
141 }

四、List

  (1)双向链表

  (2)不提供随机存取(按顺序走到需要存取的元素,O(n))

  (3)在任何位置上执行插入和删除动作都非常块,内部只需要调整一下指针。

  list是双向链表,有vector,deque的特征,而且效率高。它有插入(前插,后插,中间插),删除(前删,后删,清空等),排序等功能。而且,可以剔除连续相同元素,保留一个。

1、构造函数

1    list<int> c0; //空链表
2   list<int> c1(3); //建一个含三个默认值是0的元素的链表
3   list<int> c2(5,2); //建一个含五个元素的链表,值都是2
4   list<int> c4(c2); //建一个c2的copy链表
5   list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。

2、成员函数

1 c.begin()      返回指向链表第一个元素的迭代器。
2 c.end()        返回指向链表最后一个元素之后的迭代器。
3 c.rbegin()     返回逆向链表的第一个元素,即c链表的最后一个数据。
4 c.rend()       返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。

【示例】

 1      list<int> a1{1,2,3,4,5};
 2      list<int>::iterator it;
 3      list<int>::reverse_iterator rit;
 4      for(it = a1.begin();it!=a1.end();it++){
 5          cout << *it << "	";
 6      }
 7      cout << endl;
 8      for(rit=a1.rbegin(); rit!=a1.rend(); rit++){
 9          cout << *rit  <<  "  ";
10      }
11      cout << endl;    
1 c.assign(n,num)        将n个num拷贝赋值给链表c。
2 c.assign(beg,end)      将[beg,end)区间的元素拷贝赋值给链表c。

【示例】

 1     int a[5] = {1,2,3,4,5};
 2     list<int> a1;
 3     list<int>::iterator it;
 4     a1.assign(2,10);
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     a1.assign(a,a+5);
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;
1     c.front()      返回链表c的第一个元素。
2     c.back()       返回链表c的最后一个元素。
3     c.empty()      判断链表是否为空。

【示例】

1      list<int> a1{1,2,3,4,5};
2      if(!a1.empty())
3          cout << "the first number is:" << a1.front() << endl;
4      else
5          cout << " the last number is:" << a1.back() << endl;
1     c.size()      返回链表c中实际元素的个数。
2     c.max_size()      返回链表c可能容纳的最大元素数量。
3     c.clear()      清除链表c中的所有元素。

【示例】

 1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << a1.size() << endl;
 4     cout << a1.max_size() << endl;
 5     cout << "clear before:";
 6     for(it = a1.begin();it!=a1.end();it++){
 7         cout << *it << "	";
 8     }
 9     cout << endl;
10     a1.clear();
11     cout << "clear after:";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << "	";
14     }
15     cout << endl;
1 c.insert(pos,num)      在pos位置插入元素num。
2 c.insert(pos,n,num)      在pos位置插入n个元素num。
3 c.insert(pos,beg,end)      在pos位置插入区间为[beg,end)的元素。

【示例】

 1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "insert before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     
 9     a1.insert(a1.begin(),0);
10     cout << "insert(pos,num) after:";
11     for(it = a1.begin();it!=a1.end();it++){
12         cout << *it << " ";
13     }
14     cout << endl;
15     
16     a1.insert(a1.begin(),2,88);
17     cout << "insert(pos,n,num) after:";
18     for(it = a1.begin();it!=a1.end();it++){
19         cout << *it << " ";
20     }
21     cout << endl;
22 
23     int arr[5] = {11,22,33,44,55};
24     a1.insert(a1.begin(),arr,arr+3);
25     cout << "insert(pos,beg,end) after:";
26     for(it = a1.begin();it!=a1.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;
1     c.erase(pos)    删除pos位置的元素。

【示例】

 1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "erase before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     a1.erase(a1.begin());
 9     cout << "erase after:";
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;
1 c.push_back(num)      在末尾增加一个元素。
2 c.pop_back()          删除末尾的元素。
3 c.push_front(num)     在开始位置增加一个元素。
4 c.pop_front()         删除第一个元素。

【示例】

 1     list<int> a1{1,2,3,4,5};
 2     a1.push_back(10);
 3     list<int>::iterator it;
 4     cout << "push_back:";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.pop_back();
11     cout << "pop_back:";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
16     
17     a1.push_front(20);
18     cout << "push_front:";
19     for(it = a1.begin();it!=a1.end();it++){
20         cout << *it << " ";
21     }
22     cout << endl;
23     
24     a1.pop_front();
25     cout << "pop_front:";
26     for(it = a1.begin();it!=a1.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;
1     resize(n)      从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
2     resize(n,num)            从新定义链表的长度,超出原始长度部分用num代替。

【示例】

 1     list<int> a1{1,2,3,4,5};
 2     a1.resize(8);
 3     list<int>::iterator it;
 4     cout << "resize(n):";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.resize(10, 10);
11     cout << "resize(n,num):";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
1     c1.swap(c2);      将c1和c2交换。
2     swap(c1,c2);      同上。

【示例】

 1     list<int> a1{1,2,3,4,5},a2,a3;
 2     a2.swap(a1);
 3     list<int>::iterator it;
 4     cout << "a2.swap(a1):";
 5     for(it = a2.begin();it!=a2.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     swap(a3,a2);
11     cout << "swap(a3,a2):";
12     for(it = a3.begin();it!=a3.end();it++){
13         cout << *it << " ";
14     }
15     return 0;
1     c1.merge(c2)      合并2个有序的链表并使之有序,从新放到c1里,释放c2。
2     c1.merge(c2,comp)      合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。

【示例】

 1     list<int> a1{1,2,3},a2{4,5,6};
 2     a1.merge(a2);
 3     list<int>::iterator it;
 4     cout << "a1.merge(a2):";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a2.merge(a1,[](int n1,int n2){return n1>n2;});
11     cout << "a2.merge(a1,comp):";
12     for(it = a2.begin();it!=a2.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
1 1     c1.splice(c1.beg,c2)      将c2连接在c1的beg位置,释放c2
2 2     c1.splice(c1.beg,c2,c2.beg)      将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素
3 3     c1.splice(c1.beg,c2,c2.beg,c2.end)      将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素

【示例】

1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(), a2);
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(), a2,++a2.begin());
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;
1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(),a2,a2.begin(),a2.end());
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;
1 remove(num)             删除链表中匹配num的元素。

【示例】

1     list<int> a1{1,2,3,4,5};
2     a1.remove(3);
3     list<int>::iterator it;
4     cout << "remove():";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
1 remove_if(comp)       删除条件满足的元素,参数为自定义的回调函数。

【示例】

1     list<int> a1{1,2,3,4,5};
2     a1.remove_if([](int n){return n<3;});
3     list<int>::iterator it;
4     cout << "remove_if():";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
1 reverse()       反转链表

【示例】

1     list<int> a1{1,2,3,4,5};
2     a1.reverse();
3     list<int>::iterator it;
4     cout << "reverse:";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
1 unique()       删除相邻的元素

【示例】

1     list<int> a1{1,1,3,3,5};
2     a1.unique();
3     list<int>::iterator it;
4     cout << "unique:";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;
1 c.sort()       将链表排序,默认升序
2 c.sort(comp)       自定义回调函数实现自定义排序

【示例】

 1     list<int> a1{1,3,2,5,4};
 2     a1.sort();
 3     list<int>::iterator it;
 4     cout << "sort():";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.sort([](int n1,int n2){return n1>n2;});
11     cout << "sort(function point):";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;

【综合示例】

  1 #include <iostream>
  2 #include <list>
  3 #include <algorithm>
  4 
  5 using namespace std;
  6 
  7 void Push(list<int> &mlist)
  8 {
  9     int j, place, n, N;
 10     list<int>::iterator it;
 11     cout << "  1、前插  2、后插  3、中间插入   " << endl;
 12 
 13     cin >> j;
 14     switch(j)
 15     {
 16     case 1://前插,向后扩张
 17         cout << "请输入你要输入的整数个数:";
 18         cin >> N;
 19         cout << "请输入" << N << "个整数:" << endl;
 20         for(int i = 0; i < N; i++)
 21         {
 22             cin >> n;
 23             mlist.push_front(n);
 24         }
 25         break;
 26     case 2:
 27         cout << "请输入你要输入的整数个数:";
 28         cin >> N;
 29         cout << "请输入" << N << "个整数:" << endl;
 30         for(int i = 0; i < N; i++)
 31         {
 32             cin >> n;
 33             mlist.push_back(n);
 34         }
 35         break;
 36     case 3:
 37         cout << "请输入你要插入的位置:";
 38         cin >> place;
 39         cout << "请输入你要插入的数值:" << endl;
 40         cin >> n;
 41 
 42         it = mlist.begin();
 43         while(place)
 44         {
 45             it++;
 46             place--;
 47         }
 48         mlist.insert(it, n);
 49     default:
 50         cout << "error" << endl;
 51     }
 52 }
 53 
 54 void Pop(list<int> &mlist)
 55 {
 56     int j, place, N;
 57     list<int>::iterator it;
 58     cout << "   1、前删  2、后删   3、中间删  4、清空   " << endl;
 59 
 60     cin >> j;
 61 
 62     switch(j)
 63     {
 64     case 1:
 65         cout << "请输入你要删除的整数个数:" << endl;
 66         cin >> N;
 67 
 68         if(N > mlist.size())
 69             cout << "越界" << endl;
 70         else
 71         {
 72             for(int i = 0; i < N; i++)
 73                 mlist.pop_front();
 74         }
 75         break;
 76     case 2:
 77         cout << "请输入你要删除的整数的个数:" << endl;
 78         cin >> N;
 79         if(N > mlist.size())
 80             cout << "越界" << endl;
 81         else
 82         {
 83             for(int i = 0; i < N; i++)
 84                 mlist.pop_back();
 85         }
 86         break;
 87     case 3:
 88         cout << "请输入你要删除的位置:" << endl;
 89         cin >> place;
 90         if(place < 0 || place > mlist.size())
 91             cout << "越界" << endl;
 92         else
 93         {
 94             it = mlist.begin();
 95             while(place)
 96             {
 97                 it++;
 98                 place--;
 99             }
100         }
101         break;
102     case 4:
103         mlist.clear();
104         break;
105     default:
106         cout << "error" << endl;
107     }
108 }
109 
110 void getFront(list<int> &mlist)
111 {
112     if(mlist.empty())
113         cout << "list is empty" << endl;
114     else
115         cout << "list front element is: " << mlist.front() << endl;
116 }
117 
118 void getBack(list<int> &mlist)
119 {
120     if(mlist.empty())
121         cout << "the list is empty" << endl;
122     else
123         cout << "list back element is: " << mlist.back() << endl;
124 }
125 
126 void getSize(list<int> &mlist)
127 {
128     cout << "the size of the list is: " << mlist.size() << endl;
129 }
130 
131 void Sort(list<int> &mlist)
132 {
133     mlist.sort();//升序
134     mlist.reverse();//升序后降序
135 }
136 
137 void Search(list<int> &mlist)
138 {
139     int n;
140     cout << "请输入你想要查找的数:" << endl;
141 
142     cin >> n;
143 
144     list<int>::iterator it;
145     it = find(mlist.begin(), mlist.end(), n);
146     if(it != mlist.end())
147     {
148         int sum = 0;
149         list<int>::iterator temp;
150         temp = mlist.begin();
151         while((*temp) != (*it))
152         {
153             sum++;
154             temp++;
155         }
156         cout << n << "是第" << sum << "个数。" << endl;
157     }
158     else
159         cout << n << "is not in the list" << endl;
160 }
161 //去重
162 void Unique(list<int> &mlist)
163 {
164     mlist.unique();
165 }
166 
167 //打印
168 void Display(list<int> &mlist)
169 {
170     int i;
171     list<int>::iterator it;//list的迭代器
172     list<int>::reverse_iterator rit;//list的反向迭代器
173     cout << "   1、前向遍历   2、反向遍历    " << endl;
174     cin >> i;
175     switch(i)
176     {
177     case 1:
178         for(it = mlist.begin(); it != mlist.end(); it++)
179         {
180             cout << *it << "  ";
181         }
182         cout << endl;
183         break;
184     case 2:
185         for(rit = mlist.rbegin(); rit != mlist.rend(); rit++)
186             cout << *rit << "  ";
187         cout << endl;
188         break;
189     default:
190         cout << "error" << endl;
191     }
192 }
193 
194 int main()
195 {
196     list<int> mlist;
197 
198     Push(mlist);
199     Display(mlist);
200 
201     Pop(mlist);
202     Display(mlist);
203 
204     getFront(mlist);
205     getBack(mlist);
206     Sort(mlist);
207     Display(mlist);
208 
209     Search(mlist);
210     Unique(mlist);
211     Display(mlist);
212 
213 
214 
215     return 0;
216 
217 }
原文地址:https://www.cnblogs.com/Long-w/p/9816107.html