STL之vector

Posted bravewtz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL之vector相关的知识,希望对你有一定的参考价值。

  1 #include<iostream>
  2 #include<vector>
  3 #include<algorithm>
  4 using namespace std;
  5 
  6 //打印输出
  7 void PrintVector(vector<int>& v){
  8     for(vector<int>::iterator vec=v.begin(); vec!=v.end(); vec++ ){
  9         cout<<*vec<<" ";
 10     }
 11     cout<<endl;
 12 }
 13 
 14 
 15 //初始化
 16 void test01(){
 17     vector<int> v1;
 18     int arr[] = {10,20,30,40};
 19     vector<int> v2(arr,sizeof(arr)/sizeof(int)+arr);
 20     vector<int> v3(v2.begin(),v2.end());
 21     vector<int> v4(v3);
 22 
 23     PrintVector(v2);
 24     PrintVector(v3);
 25     PrintVector(v4);
 26 
 27 }
 28 
 29 //常用赋值操作
 30 void test02(){
 31     int arr[]={10,20,30,40};
 32     vector<int> v1(arr,arr+sizeof(arr)/sizeof(int));
 33     vector<int> v2;
 34     v2.assign(v1.begin(),v1.end());
 35 
 36     vector<int> v3;
 37     v3=v2;
 38 
 39     int arr_temp[]={100,200,300,400};
 40     vector<int> v4(arr_temp,arr_temp+sizeof(arr_temp)/sizeof(int));
 41     v4.swap(v1);
 42     PrintVector(v1);
 43     PrintVector(v2);
 44     PrintVector(v3);
 45     PrintVector(v4);
 46 }
 47 
 48 //大小操作
 49 void test03(){
 50     int arr_temp[]={100,200,300,400};
 51     vector<int> v4(arr_temp,arr_temp+sizeof(arr_temp)/sizeof(int));
 52 
 53     cout<<"size:"<<v4.size()<<endl;//元素的个数
 54     if(v4.empty()){
 55         cout<<"空!"<<endl;
 56     }
 57     else{
 58         cout<<"不空!"<<endl;
 59     }
 60     PrintVector(v4);
 61     //v4.reserve(8);//更改capacity但不更改size
 62     v4.resize(6,1);//同时更改capacity和size
 63     PrintVector(v4);
 64 
 65     cout<<v4.capacity()<<endl;//元素的容量
 66 }
 67 
 68 //存取数据
 69 void test04(){
 70     int arr_temp[]={100,200,300,400};
 71     vector<int> v4(arr_temp,arr_temp+sizeof(arr_temp)/sizeof(int));
 72     for( int i=0; i<v4.size(); i++ ){
 73         cout<<v4[i]<<" ";
 74     }
 75     cout<<endl;
 76     try{
 77         for( int i=0; i<v4.size()+5; i++ ){
 78             cout<<v4.at(i)<<" ";
 79         }
 80     }catch(...){
 81         cout<<"越界"<<endl;
 82     }
 83     //区别:at抛异常,[]不抛异常
 84 
 85     cout<<"front:"<<v4.front()<<endl;
 86     cout<<"back:"<<v4.back()<<endl;
 87 }
 88 
 89 //插入和删除
 90 void test05(){
 91    vector<int> v;
 92    v.push_back(10);
 93    v.push_back(20);
 94    //头插法
 95    v.insert(v.begin(),30);
 96    v.insert(v.end(),40);
 97    PrintVector(v);
 98    v.insert(v.begin()+2,100);//vector支持随机访问
 99    //支持数组下标的,一般都支持随机访问
100    PrintVector(v);
101 //删除操作
102    v.erase(v.begin());
103    PrintVector(v);
104    v.erase(v.begin()+1,v.end());
105    PrintVector(v);
106    v.clear();
107    cout<<"size:"<<v.size()<<endl;
108 
109 }
110 
111 //巧用swap缩减空间
112 void test06(){
113     vector<int> v;
114     for( int i=0; i<100000; i++ ){
115         v.push_back(i);
116     }
117     cout<<"size:"<<v.size()<<endl;
118     cout<<"capacity:"<<v.capacity()<<endl;
119 
120     v.resize(10);
121     cout<<"-----------------"<<endl;
122     cout<<"size:"<<v.size()<<endl;
123     cout<<"capacity:"<<v.capacity()<<endl;
124 
125     //收缩空间
126     vector<int>(v).swap(v);
127     cout<<"-----------------"<<endl;
128     cout<<"size:"<<v.size()<<endl;
129     cout<<"capacity:"<<v.capacity()<<endl;
130 }
131 
132 int main()
133 {
134 
135     test06();
136 
137     return 0;
138 }

 

以上是关于STL之vector的主要内容,如果未能解决你的问题,请参考以下文章

STL之vector4

STL之vector

STL之vector

超详细STL之基于源码剖析vector实现原理及注意事项

超详细STL之基于源码剖析vector实现原理及注意事项

STL之vector容器详解