博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL顺序容器之vector类型
阅读量:7059 次
发布时间:2019-06-28

本文共 9568 字,大约阅读时间需要 31 分钟。

  hot3.png

顺序容器:

将单一类型元素聚集起来成为容器,然后根据位置来存储和访问这些元素,这就是顺序容器。

顺序容器的元素排列与元素值无关,而是由元素添加到容器里的次序决定。

标准库中定义了三种顺序容器类型:vector、list和deque(double-ended queue,双端队列),它们的差别在于访问元素的方式,以及添加或删除元素相关操作的运行代价。

容器只定义了少量操作,大多数额外操作都是由库提供。

vector容器:

vector是顺序容器,是STL中最为常用的容器。它的容器以连续的方式存放。熟练掌握vector的使用是进入STL库学习的第一道门槛。

下面通过讲例子的方式慢慢深入vector容器的学习。

知识点预览:

vector<int> v;//定义一个空容器

vector<int> v1(v);//将v通过构造函数拷贝给v1

v.empty();//容器是否为空

vector<int>::iterator it = v.begin();//容器的首元素地址  [begin,end)

v.end();//容器的最后一个元素的下一个位置

v.rbegin();//逆序迭代器,指向容器的最后一个元素

v.rend();//第一个元素前面的位置

v.size();//容器v当前元素的个数

v.resize(n); //调整容器的长度大小,使其能容纳n个元素

v.resize(n,t);//调整大小的同时将新添加的元素的值都设为t

v.max_size();//返回容器可容纳的最多元素个数,返回类型为v::size_type

v.capacity(); //容器v的容量,会自动增长。通常大于size(),加倍分配存储空间。

v.reserve(n);//预留窗外的存储空间。

v.push_back(10);//将值10放入容器v 中

v.insert(p,t);//在迭代器p所指元素的前面插入值为t 的元素

v.insert(p,n,t);//在迭代器所指位置的前面插入n个值为t的元素

v.insert(p,b,e);//在迭代器p指向的元素前面插入迭代器b和e标记范围内的元素

v.at(1) = 20;// 相当于v[1] = 20;

v = v1;//删除v的所有元素,将v1的元素复制给v

v.assign(n,t);//将容器重新设置为存储n个值为 t 的元素

v.assign(b,e);//重新设置v的元素:将迭代器b,e范围内的所有元素复制到v中,b,e必须不是指向c中元素的迭代器

v.front() / v.back() //返回头元素,尾元素的引用

v.swap(v2);//交换两个容器,包括元素值,迭代器及大小等所有数据。比复制的操作快。

v.erase(p);//删除迭代器所指向的元素,它返回一个迭代器,指向被删除元素后面的位置。如果p指向元素的最后一个元素,则返回的迭代器指向容器的超出末端的下一位置。

v.erase(b,e);//删除迭代器b,e范围内的所有元素

v.clear();//删除容器中的所有元素

v.pop_back();//删除容器的最后一个元素

 

copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));//输出v中一块连续区间的元素的值

 fill(b,e,t);//在迭代器b,e范围内填充所有元素的值为t

 

 

[cpp]  

 

  1. template<typename T>  
  2.   
  3. class Print{  
  4.   
  5. public:  
  6.   
  7. void operator () (T& t){  
  8.   
  9. cout << t << " ";  
  10. }  
  11. }  
  12.   
  13. Print<int> print;  
  14.   
  15. for_each(v.begin(),v.end(),print); //自定义输出方式  

 

 

注:我用的编译环境是C Free + STLport

 

 

第一个例子:

使用下标方式和push_back()输入参数至v1中,vector<int> v2310,0),v3中共有10个元素,所有元素赋予初值0。

vector<string> v4(str+0, str+3); 将字符数组str[] 中的内容拷贝至v4中。vector<string>::iterator sIt = v4.begin(); 定义一个迭代器,遍历v4中的所有元素的值。

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. #include <algorithm>    
  4. using namespace std;    
  5. int main(){    
  6.   //empty vector object    
  7.   vector<int> v1;    
  8.   v1.push_back(3);    
  9.   v1[1] = 4;    
  10.   v1[2] = 5;    
  11.   cout << "v1:" << endl;    
  12.   for(int i = 0; i < 3; ++i)    
  13.   cout << v1[i] << " ";    
  14.   cout << endl;    
  15.   //creates vector with ten object elements    
  16.   vector<int> v2(10);    
  17.   cout << "v2:" << endl << v2[0] << endl;    
  18.       
  19.   //creates vector with ten object elements    
  20.   //and assigned value 0 for each    
  21.   vector<int> v3(10,0);    
  22.   cout << "v3:" << endl;    
  23.   for(int i = 0; i < 10; ++i)    
  24.   cout << v3[i] << " ";    
  25.   cout << endl;    
  26.       
  27.   string str[] = {"Alex","Ajioy","Zerian"};    
  28.   // copy str,str+1,str+2 to v4    
  29.   vector<string> v4(str+0, str+3);    
  30.   vector<string>::iterator sIt = v4.begin();    
  31.   cout << "v4:" << endl;    
  32.   while(sIt != v4.end())    
  33.   cout << *sIt++ << " ";    
  34.   cout << endl;    
  35.   vector<string> v5(v4);    
  36.   cout << "v5:" << endl;    
  37.   for(int i = 0; i < 3; ++i)    
  38.    cout << v5[i] << " ";    
  39.    cout << endl;    
  40.    return 0;        
  41. }    

运行结果:

 

v1:

3 4 5
v2:
0
v3:
0 0 0 0 0 0 0 0 0 0
v4:
Alex Ajioy Zerian
v5:
Alex Ajioy Zerian

 

第二个例子:

v.assign(3,100);是将当前v的所有元素清空,用3个值为100的元素替代。

v1.at(1) = 200;相当于v1[1] = 200;,也相当于v1.insert(v1.begin() + 1,200);

copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));是将vector容器中指定的一块连续的区域的元素的值以指定的方式输出,需要包含头文件#include <iterator>

 

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. #include <iterator>    
  4. using namespace std;    
  5. int main(){    
  6.    int arr[] = {1,2,3,4,5};    
  7.    vector<int> v;    
  8.    //assign to the "v",the contains of "arr"    
  9.    v.assign(arr,arr+5);    
  10.    copy(v.begin(),v.end(),    
  11.        ostream_iterator<int>(cout," "));    
  12.    cout << endl;    
  13.    //replace v for 3 copies of 100    
  14.    v.assign(3,100);    
  15.    copy(v.begin(),v.end(),    
  16.    ostream_iterator<int>(cout," "));    
  17.    cout << endl;    
  18.    vector<int> v1(3,0);    
  19.    v1[0] = 100;    
  20.    v1.at(1) = 200;    
  21.    v1.insert(v1.begin() + 2,300);    
  22.    for(int i = 0; i < 3; ++i)    
  23.    cout << v1.at(i) << " ";    
  24.    cout << endl;    
  25. }    

运行结果:

 

1 2 3 4 5

100 100 100

100 200 300

 

第三个例子:

 class Member的作用在于配合push_back。

v.push_back(M("Robert",60000)); 是把一个M类型(也即Member<string,double>类型)的临时对象压进容器v中,通过迭代器It访问M对象的成员函数print(),输出数据成员name和sal,如此情形下是不可以用v[0],v[1]直接访问v的元素。这种方式很巧妙,通过自定义一个包含多种数据类型的类来保存复杂的数据,既简化了输入输出等操作,又使得结构更加清晰。

M("Robert",60000);相当于

M tmp("Robert",60000);

v.push_back(tmp);

 M("Robert",60000);可以把它简单对待成诸如int,string的数据类型,只是它比int,string更复杂一点。

v.back().print()是将容器v中的最后一个元素的值打印出来。

同理可知v.front().print()是打印第一个元素的值。

 

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. #include <iterator>    
  4. #include <string>    
  5. using namespace std;    
  6. template <typename T,typename D>    
  7. class Member{    
  8.   public:    
  9.   Member(T t,D d):name(t),sal(d){}    
  10.   void print();    
  11.   private:    
  12.   T name;    
  13.   D sal;     
  14. };    
  15. template<typename T,typename D>    
  16. void Member<T,D>::print()    
  17. {    
  18.  cout << "name:" << name     
  19.  << " salary:" << sal << endl;    
  20. }    
  21. typedef Member<string,double> M;    
  22. int main(){    
  23.      
  24.  vector<M> v;    
  25.  v.push_back(M("Robert",60000));    
  26.  v.push_back(M("Linda",75000));    
  27.  vector<M>::iterator It = v.begin();    
  28.  cout << "Enter vector:" << endl;    
  29.  while(It != v.end())    
  30.  (It++)->print();    
  31.  cout << endl;    
  32.  cout << "return from back()" << endl;    
  33.  v.back().print();    
  34.  cout << "return from front()" << endl;    
  35.  v.front().print();    
  36. }    

 运行结果:

 

Enter vector:

name:Robert salary:60000
name:Linda salary:75000

return from back()

name:Linda salary:75000
return from front()
name:Robert salary:60000

 

第四个例子:

 fill(v.begin(),v.end(),5);填充迭代器范围内所有元素的值为5

v.clear();删除所有元素

 

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. #include <algorithm>    
  4. using namespace std;    
  5. template <class T>    
  6. class Print{    
  7.    public:    
  8.    void operator() (T & t)    
  9.    {    
  10.     cout << t << " " ;    
  11.    }    
  12. };    
  13. int main()    
  14. {    
  15.     vector<int> v(10);    
  16.     Print<int> print;    
  17.     //set all elements as 5    
  18.     fill(v.begin(),v.end(),5);    
  19.     cout << "vector v:";    
  20.     for_each(v.begin(),v.end(),print);    
  21.     cout << endl;    
  22.     cout << "size of v = " << v.size() << endl;    
  23.     cout << "v.clear" << endl;    
  24.     //clear all elements of container    
  25.     v.clear();    
  26.     cout << "vector v:";    
  27.     for_each(v.begin(),v.end(),print);    
  28.     cout << endl;    
  29.     cout << "size of v = " << v.size() << endl;    
  30.     cout << "vector v is ";    
  31.     v.empty()? cout << "" : cout << "not ";    
  32.     cout << "empty" << endl;    
  33.         
  34.     v.push_back(100);    
  35.     cout << v[0] << endl;    
  36.     cout << "now v is ";    
  37.     v.empty() ? cout << "" : cout << "not ";    
  38.     cout << "empty" << endl;    
  39. }    

 

 

 运行结果:

vector v:5 5 5 5 5 5 5 5 5 5

size of v = 10
v.clear
vector v:
size of v = 0
vector v is empty
100
now v is not empty

 

第五个例子:

v.resize(7,20);将容器的长度大小在原有基础上调整为7,并且将新添加的元素的值设为20

fill(vc.begin(),vc.end(),'*');在迭代器范围内填充元素的值为*

vc.pop_back();删除最后一个元素

 

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. #include <iterator>    
  4. #include <algorithm>    
  5. using namespace std;    
  6. int main(){    
  7.     /*  resize  */    
  8.    vector<int> v(5);    
  9.    for(int i = 0; i < 5; ++i)    
  10.     v[i] = i * 2;    
  11.     //copy() function need include "iterator"    
  12.    copy(v.begin(), v.end(),     
  13.     ostream_iterator<int>(cout," "));    
  14.     cout << endl;    
  15.     //reset the size and fill with 20    
  16.     v.resize(7,20);    
  17.     copy(v.begin(),v.end(),    
  18.     ostream_iterator<int>(cout," "));    
  19.     cout << endl << endl;    
  20.         
  21.     /* size */    
  22.     vector<char> vc(5);    
  23.     cout << "size of vc = " << vc.size() << endl;    
  24.         fill(vc.begin(),vc.end(),'*');    
  25.     copy(vc.begin(),vc.end(),ostream_iterator<char>(cout," "));    
  26.     cout << endl;    
  27.     for(int i = 0; i < v.size(); ++i)    
  28.     cout << vc[i] << " ";    
  29.     cout << endl;    
  30.     for(int i = 0; i < 5; ++i){    
  31.       cout << "size of vc = " ;    
  32.       copy(vc.begin(),vc.end(),ostream_iterator<char>(cout," "));    
  33.       cout << endl;    
  34.       //pop up 1 element    
  35.       vc.pop_back();    
  36.     }    
  37.         
  38.         
  39. }    

运行结果:

 

0 2 4 6 8

0 2 4 6 8 20 20

size of vc = 5

* * * * *
* * * * *

size of vc = * * * * *

size of vc = * * * *
size of vc = * * *
size of vc = * *
size of vc = *

 

第六个例子:

v起初分配的存储长度为5,此时size()和capacity()都为5。随着v.push_back(3); v.push_back(9);这两条语句的增加,容器的长度已经不能满足。不得不分配空间时,它以成倍的分式来开辟空间。即capacity()从5变为10。v.reserve(100);为容器预留100个长度的存储空间。

 

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. using namespace std;    
  4. template<typename T>    
  5. class Print{    
  6.   public:    
  7.   void operator() (T& t){    
  8.   cout << t << " ";    
  9.   }    
  10. };    
  11. int main(){    
  12.    vector<int> v(5,1);    
  13.    cout << "size of v = " << v.size() << endl;    
  14.    cout << "capacity of v = " << v.capacity() << endl;    
  15.    cout << "value of each elements:" ;    
  16.    for(int i = 0; i < v.size(); ++i)    
  17.    cout << v[i] << " ";    
  18.    cout << endl;     
  19.    //modify the elements    
  20.    v[0] = 5;    
  21.    v[1] = 8;    
  22.    v.push_back(3);    
  23.    v.push_back(9);    
  24.    cout << endl;    
  25.    cout << "size of v = " << v.size() << endl;    
  26.    //capacity is typically larger than size    
  27.    cout << "capacity v = " << v.capacity() << endl;    
  28.    Print<int> print;    
  29.    cout << "value of each element is :";    
  30.    for_each(v.begin(),v.end(),print);    
  31.    cout << endl;    
  32.        
  33.    v.reserve(100); // increase capacity to 100    
  34.    cout << "size of v1_int:" << v.size() << endl;    
  35.    cout << "capacity of v1_int:" << v.capacity() << endl;    
  36.    // i don't know why the size of vector v is 12    
  37.    int size = sizeof(v);    
  38.    cout << "size of v = " << size << endl;    
  39.             
  40. }    

运行结果:

 

size of v = 5

capacity of v = 5
value of each elements:1 1 1 1 1

size of v = 7

capacity v = 10
value of each element is :5 8 1 1 1 3 9
size of v1_int:7
capacity of v1_int:100
size of v = 12

第七个例子:

 交换两个容器的所有元素

 

 

[cpp]  

 

  1. #include <iostream>    
  2. #include <vector>    
  3. #include <algorithm>    
  4. #include <ctime>    
  5. #include <iterator>    
  6. using namespace std;    
  7. int main(){    
  8.        srand(unsigned(time(0)));    
  9.        int arr[10];    
  10.        const int max = 100;    
  11.        const int min = 50;    
  12.        //generate 10 random numbers    
  13.        for(int i = 0; i < 10; ++i)    
  14.        arr[i] = max * rand() / (RAND_MAX + min) + 1;    
  15.        vector<int> v1(arr,arr+10);    
  16.        vector<int> v2(arr+2,arr+6);    
  17.        copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));    
  18.        cout << endl     
  19.             << "size of v1 = " << v1.size() << endl;    
  20.        copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));    
  21.        cout << endl    
  22.             << "size of v2 = " << v2.size() << endl;    
  23.                 
  24.        v1.swap(v2);    
  25.        cout << "after swapping..." << endl    
  26.             << "vector v1:" << endl;    
  27.        copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));    
  28.        cout << endl << "size of v1:" << v1.size() << endl;    
  29.            
  30.        cout << "vector v2:" << endl;    
  31.        copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));    
  32.        cout << endl    
  33.             << "size of vector v2:" << v2.size() << endl;    
  34. }    

运行结果:

 

62 13 35 27 73 24 82 74 54 36

size of v1 = 10
35 27 73 24
size of v2 = 4
after swapping...
vector v1:
35 27 73 24
size of v1:4
vector v2:
62 13 35 27 73 24 82 74 54 36
size of vector v2:10

转载于:https://my.oschina.net/lmoon/blog/872228

你可能感兴趣的文章
微软为 Chrome 和 Firefox 发布了 Windows Defender 扩展
查看>>
英特尔基于 LLVM 的 SYCL 开源编译器现已发布
查看>>
java B2B2C 多租户电子商城系统
查看>>
(十二)java springboot b2b2c shop 多用户商城系统源码- SSO单点登录之OAuth2.0 登出流程(3)...
查看>>
换个角度看GAN:另一种损失函数
查看>>
连接mysql报错Table ‘performance_schema.session_variables’ ...
查看>>
Linux基础命令---zipinfo
查看>>
关于移动互联网产品的指标分析初探
查看>>
4.2019Android多线程总结
查看>>
Java数据解析之JSON
查看>>
大师级直播程序源码,一次让您玩个够
查看>>
跟我学 K8S--代码: 调试 Kubernetes
查看>>
烟沙浮生 | 此间少年(2018-10-15 第五周记)
查看>>
使用Lambda表达式与回调函数简化缓存操作
查看>>
Java网络编程和NIO详解开篇:Java网络编程基础
查看>>
CCleaner v5.54.7088 发布,系统清理工具
查看>>
Python函数式编程map()、reduce()
查看>>
产品经理十一章:流程图制作
查看>>
unsafe包
查看>>
(5keras自带的模型之间的关系)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署...
查看>>