其他分享
首页 > 其他分享> > vector容器

vector容器

作者:互联网

 

  1 /*
  2 vec.begin()        //指向迭代器中第一个元素。 
  3 vec.end()            //指向迭代器中末端元素的下一个,指向一个不存在元素。        
  4 vec.push_back(elem)     //在尾部加入一个数据。
  5 vec.pop_back()         //删除最后一个数据。
  6 vec.capacity()         //vector可用空间的大小。
  7 vec.size()           //返回容器中数据个数。
  8 vec.empty()          //判断容器是否为空。
  9 vec.front()             //传回第一个数据。
 10 vec.back()             //传回最后一个数据,不检查这个数据是否存在。
 11 vec.at(index)        //传回索引idx所指的数据,如果idx越界,抛出out_of_range。
 12 vec.clear()             //移除容器中所有数据。
 13 vec.erase(iterator)  //删除pos位置的数据,传回下一个数据的位置。
 14 vec.erase(begin,end) //删除[beg,end)区间的数据,传回下一个数据的位置。注意:begin和end为iterator
 15 vec.insert(position,elem)    //在pos位置插入一个elem拷贝,传回新数据位置。
 16 vec.insert(position,n,elem)    //在pos位置插入n个elem数据,无返回值。
 17 vec.insert(position,begin,end)    //在pos位置插入在[beg,end)区间的数据,无返回值。*/
 18 #include<vector>
 19 #include<iostream>
 20  
 21 using namespace std;
 22  
 23  
 24 int main()
 25 {
 26     vector<int> vec(3,0);
 27     vector<int>::iterator iter;
 28     vector<int>::iterator begin=vec.begin();
 29     vector<int>::iterator end=vec.end();
 30     cout<<"vec:";
 31     for(iter=begin; iter!=end; iter++)
 32     {
 33         static std::size_t i=0;
 34         cout<<*iter<<",";
 35         i++;
 36     }
 37     cout<<endl;
 38     cout<<"size:"<<vec.size()<<endl;
 39     cout<<"capacity:"<<vec.capacity()<<endl;
 40 //////////////////////////////////////////////////
 41     cout<<endl;
 42     vec.push_back(1);
 43     vec.push_back(2);
 44  
 45     begin=vec.begin();
 46     end=vec.end();
 47     cout<<"push back 1 and 2 based on above;vec:";
 48     for(iter=begin; iter!=end; iter++)
 49     {
 50         static std::size_t j=0;
 51         cout<<*iter<<",";
 52         j++;
 53     }
 54     cout<<endl;
 55     cout<<"size:"<<vec.size()<<endl;
 56     cout<<"capacity:"<<vec.capacity()<<endl;
 57 //////////////////////////////////////////////////
 58     cout<<endl;
 59     vec.pop_back();
 60  
 61     begin=vec.begin();
 62     end=vec.end();
 63     cout<<"pop one element based on above;vec:";
 64     for(iter=begin; iter!=end; iter++)
 65     {
 66         static std::size_t k=0;
 67         cout<<*iter<<",";
 68         k++;
 69     }
 70     cout<<endl;
 71     cout<<"size:"<<vec.size()<<endl;
 72     cout<<"capacity:"<<vec.capacity()<<endl;
 73 /////////////////////////////////////////////////
 74     begin=vec.begin();
 75     end=vec.end();
 76     cout<<endl;
 77     if(vec.empty())
 78     {
 79         cout<<"vec is empty"<<endl;
 80     }
 81     else
 82     {
 83         cout<<"vec is not empty"<<endl;
 84     }
 85 /////////////////////////////////////////////////  
 86     cout<<endl;
 87     cout<<"based on the above:"<<endl;
 88     cout<<"     vec.front():"<<vec.front()<<endl;
 89     cout<<"     vec.back():"<<vec.back()<<endl;
 90     begin=vec.begin();
 91     end=vec.end();
 92     cout<<"     size:"<<vec.size()<<endl;
 93     cout<<"     capacity:"<<vec.capacity()<<endl;
 94     cout<<"vec:";
 95     for(iter=begin; iter!=end; iter++)
 96     {
 97         static std::size_t l=0;
 98         cout<<*iter<<",";
 99         l++;
100     }
101     cout<<endl;
102 ////////////////////////////////////////////////////
103     cout<<endl;
104     cout<<"call at(),based on the above:"<<endl;
105     cout<<"     vec.at():"<<vec.at(3)<<endl;
106     begin=vec.begin();
107     end=vec.end();
108     cout<<"     size:"<<vec.size()<<endl;
109     cout<<"     capacity:"<<vec.capacity()<<endl;
110     cout<<"vec:";
111     for(iter=begin; iter!=end; iter++)
112     {
113         static std::size_t m=0;
114         cout<<*iter<<",";
115         m++;
116     }
117     cout<<endl;
118 //////////////////////////////////////////////////
119     cout<<endl;
120     cout<<"call clear(),based on the above:"<<endl;
121     vec.clear();
122     begin=vec.begin();
123     end=vec.end();
124     cout<<"     size:"<<vec.size()<<endl;
125     cout<<"     capacity:"<<vec.capacity()<<endl;
126     cout<<"vec:";
127     for(iter=begin; iter!=end; iter++)
128     {
129         static std::size_t m=0;
130         cout<<*iter<<",";
131         m++;
132     }
133     cout<<endl;
134 //////////////////////////////////////////////////
135     cout<<endl;
136     for(int i=1;i<8;i++)
137     {
138         vec.push_back(i);
139     }
140     cout<<"push_back 1,2,3,4,5,6,7 based on above;vec:";
141     begin=vec.begin();
142     end=vec.end();
143     for(iter=begin; iter!=end; iter++)
144     {
145         static std::size_t m=0;
146         cout<<*iter<<",";
147         m++;
148     }
149 ////////////////////////////
150     cout<<endl;
151     vec.erase(vec.begin()+2);
152     cout<<"call vec.erase(3),vec:";
153     begin=vec.begin();
154     end=vec.end();
155     for(iter=begin; iter!=end; iter++)
156     {
157         static std::size_t m=0;
158         cout<<*iter<<",";
159         m++;
160     }
161     cout<<endl;
162     cout<<"     size:"<<vec.size()<<endl;
163     cout<<"     capacity:"<<vec.capacity()<<endl;
164 //////////////////////////
165     cout<<endl;
166     vec.erase(vec.begin()+1,vec.begin()+3);
167     cout<<"call vec.erase(1,3),vec:";
168     begin=vec.begin();
169     end=vec.end();
170     for(iter=begin; iter!=end; iter++)
171     {
172         static std::size_t m=0;
173         cout<<*iter<<",";
174         m++;
175     }
176     cout<<endl;
177     cout<<"     size:"<<vec.size()<<endl;
178     cout<<"     capacity:"<<vec.capacity()<<endl;
179     return 0;
180     }

 

 1 //assign函数原型及功能:
 2 //void assign(const_iterator first,const_iterator last); 
 3 //功能:将区间[first,last)的元素赋值到当前的vector中,当前vector会清除掉容器中之前的内容。
 4 //void assign(size_type n,const T& x = T()); 
 5 //功能:赋n个值为x的元素到当前vector中,当前vector会清除掉容器中之前的内容。
 6 #include <vector>
 7 #include <iostream>
 8 using namespace std;
 9 int main( )
10 {
11     vector<int> v1, v2, v3;
12     vector<int>::iterator iter;
13     for(int i=10; i<60; i+=10)
14         v1.push_back(i);
15     v2.push_back(1);
16     v2.push_back(2);
17  
18     cout << "v1 = " ;
19     for (iter = v1.begin(); iter != v1.end(); iter++)
20         cout << *iter << " ";
21     cout << endl;
22  
23     cout << "v2 = ";
24     for (iter = v2.begin(); iter != v2.end(); iter++)
25         cout << *iter << " ";
26     cout << endl;
27  
28  
29     v1.assign(v2.begin(), v2.end());
30     cout << "v1 = ";
31     for (iter = v1.begin(); iter != v1.end(); iter++)
32         cout << *iter << " ";
33     cout << endl;
34     
35     v3=v1;
36     v3.assign(4,3) ;
37     cout << "v3 = ";
38     for (iter = v3.begin(); iter != v3.end(); iter++)
39         cout << *iter << " ";
40     cout << endl;
41     return 0;
42 }

 

 1 /*
 2 vec.rbegin()//传回一个vector的最后一个数据的指针。
 3 vec.rend()// 传回一个vector的第一个数据前一个位置的指针。
 4 
 5 若干种创建方式(vector的构造函数)
 6 vector<type> vec
 7 vector<type> vec(vec1)
 8 vector<type> vec=vec1
 9 vector<type> vec(n)
10 vector<type> vec(n,elem)
11 vector<type> vec(begin,end)
12 vector<type> vec{a,b,c,……}
13 vector<type> vec={a,b,c,……}
14 */ 
15 #include <vector>
16 #include <iostream>
17 using namespace std;
18 int main( )
19 {
20     vector<int> v1, v2, v3;
21     vector<int>::iterator iter;
22     for(int i=10; i<60; i+=10)
23         v1.push_back(i);
24     v2.push_back(1);
25     v2.push_back(2);
26  
27     cout << "v1 = " ;
28     for (iter = v1.begin(); iter != v1.end(); iter++)
29         cout << *iter << " ";
30     cout << endl;
31  
32     cout << "v2 = ";
33     for (iter = v2.begin(); iter != v2.end(); iter++)
34         cout << *iter << " ";
35     cout << endl;
36     
37     vector<int>::reverse_iterator r_iter;
38     cout << "v1 in reverse ";
39     for (r_iter = v1.rbegin(); r_iter != v1.rend(); r_iter++)
40         cout << *r_iter << " ";
41     cout << endl;
42     cout<<"v1.rbegin():"<<*v1.rbegin()<<endl;
43     return 0;
44 }

 

标签:容器,begin,end,iterator,vector,vec,传回
来源: https://www.cnblogs.com/yg02/p/12262751.html