1 # include<vector> 2 # include<iostream> 3 # include<cstdio> 4 # include<algorithm> 5 using namespace std; 6 int main(int argc, const char * argv[]) 7 { 8 //vector容器的定义三种 9 //(1) vector<int>v 10 //(2) vector<double>v(3)用来存储3的double型的容器 11 //(3) vector<int>(10,8.7)用来存储10个装有8.7的容器 12 13 //vector<int>v(3);//其中的3为元素的个数 14 //v.push_back(1);//给其中加入1,现在v中就有 0 0 0 1 即四个数 15 //v.push_back(2);//push_back的作用是给其中的v加入后缀 16 //v.push_back(3); 17 18 /*-----------------------------------------------------*/ 19 //vector<int>v(3); 20 //v[0] = 2; 21 //v[1] = 3; 22 //v[2] = 4; 23 //vector<int>::iterator it; 24 //for(it = v.begin(); it!=v.end(); it++) //遍历 25 //{ 26 // cout<<*it<<" "; 27 //} 28 //cout<<endl; 29 30 /*-------------------------------------------------------*/ 31 //insert 32 //vector<int>v(3); 33 //v[0] = 2; 34 //v[1] = 7; 35 //v[2] = 9; 36 //v.insert(v.begin(),8); 37 //v.insert(v.begin()+2,1); 38 //v.insert(v.end(),3); 39 //vector<int>::iterator it; 40 //for(it = v.begin(); it!=v.end(); it++) 41 //{ 42 // cout<<*it<<" "; 43 //} 44 //cout<<endl; 45 46 /*------------------------------------------------------*/ 47 //erase() 48 //vector<int>v(10); 49 //for(int i = 0;i < 10;i++) 50 //{ 51 // v[i] = i; 52 //} 53 //v.erase(v.begin()+2); 54 //vector<int>::iterator it; 55 //for(it = v.begin();it!=v.end();it++) 56 //{ 57 // cout<<*it<<" "; 58 //} 59 //cout<<endl; 60 //v.erase(v.begin()+1,v.begin()+5); 61 //for(it = v.begin();it!=v.end();it++) 62 //{ 63 // cout<<*it<<" "; 64 //} 65 //cout<<endl; 66 //v.clear(); 67 //cout<<v.size()<<endl; 68 69 /*---------------------------------------------------*/ 70 //reverse 71 //vector<int>v(10); 72 //for(int i = 0;i < 10;i++) 73 //{ 74 // v[i] = i; 75 //} 76 //reverse(v.begin(),v.end()); 77 //vector<int>::iterator it; 78 //for(it = v.begin();it!=v.end();it++) 79 //{ 80 // cout<<*it<<" "; 81 //} 82 //cout<<endl; 83 84 /*-------------------------------------------*/ 85 //sort //当然也可以写一个sort函数cmp 调用格式同sort(v.begin(),v.end(),cmp); 86 // vector<int>v; 87 //int i; 88 // for(i = 0;i < 10;i++) 89 // { 90 // v.push_back(9-i); 91 // } 92 //for(i = 0;i < 10;i++) 93 //{ 94 // cout<<v[i]<<" "; 95 //} 96 //cout<<endl; 97 //sort(v.begin(),v.end()); 98 //for(i = 0;i < 10;i++) 99 //{ 100 // cout<<v[i]<<" "; 101 //} 102 //cout<<endl; 103 104 /*-----------------------------------------------*/ 105 //size 106 vector<int>v(10); 107 for(int i = 0; i < 10; i++) 108 { 109 v[i] = i; 110 } 111 cout<<v.size()<<endl; 112 cout<<v.empty()<<endl; 113 v.clear(); 114 cout<<v.empty()<<endl; 115 116 return 0; 117 }