|
一、vector基本概念: 1、功能: vector数据结构和数组非常相似,也称为单端数组。 2、vector和普通数组的区别: 不同之处在于数组是静态空间,而vector是可以动态扩展的。动态扩展它并不是在原空间之后持续新空间,而是找更大的内存空间,然后将原数据拷贝到新空间,释放原空间。
注:上图中的push_back()和pop_back()函数分别表示往容器里面插入数据和从容器中拿走数据;begin()和end()是我们稍后会用到的迭代器 3、vector容器的迭代器是支持随机访问的迭代器。 二、vector构造函数 1、功能: 创建vector容器 2、函数原型:
代码应用: #include <iostream>) j* ?6 V. y6 |4 M) X, a3 f#include <vector> using namespace std;0 b' R$ b. `- m* I/ V# c //输出打印- Z8 ~% n5 o2 c0 U. I void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)8 z! c# H# |* c2 \3 E { cout<<*it<<" ";$ f2 z# `- r1 i5 f, [# K }9 D: j1 t& _& g a9 _ cout<<endl;* l' N4 B4 d( K& u }4 H/ Y- O# O7 g //vector容器构造 void test()6 s, S* K5 U. y& X l: ~ { vector<int>v1;7 ]0 x2 ~/ W2 F: N3 W, t; L0 S' H- I for(int i =0; i<10;i++)- L* p4 v$ `1 e {3 V7 L1 p! j0 \4 T4 V v1.push_back(i);$ w# U3 g' c/ a2 I) U- q* r } print(v1); //通过区间方式进行构造 vector<int>v2(v1.begin(),v1.end());' t3 E: d5 I7 X' V print(v2);6 E3 @; ?1 E- b3 i* ] //n个elem方式构造 vector<int>v3(10,100); print(v3); //拷贝构造 vector<int>v4(v3); print(v4); & |9 Q' V! ?4 O: D! C7 X0 c+ w }, P+ d' x: }' Z- w int main() { test(); } : n# v! J/ Y/ o n( c& x 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 3 L; w$ H0 T1 p0 ?, a 100 100 100 100 100 100 100 100 100 100 $ l( }4 w& U7 w r( | 7 A. ^, O4 n# U3 B1 x3 a 三、vector赋值操作: 1、功能描述: 给vector容器进行赋值 2、函数原型:
代码应用: #include <iostream>' V0 y8 R* e. M0 o& }#include <vector> using namespace std; 1 Q7 }8 C' t4 r n* ]- N void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++) { cout<<*it<<" ";; S2 Q! u1 y- ^ k" E' y }$ o( |3 G& y- u% I3 J cout<<endl; } void test() { vector<int>v1;$ o s |' L: y, z; P& N5 y 5 h' h |' r9 e# c9 x) _ for(int i=0;i<10;i++) {: r5 P. x; n8 h! p v1.push_back(i);' c. b8 f$ ^0 \: P3 ]" ` }: a6 _; x$ H+ d8 Q i+ D print(v1); //赋值操作9 i4 d9 p* m6 d9 E L( s9 D, e$ m/ e2 X vector<int> v2;) p( B9 ]! |! }# a v2=v1;; L, H' I" W) { print(v2);$ o! _* ?$ }: @4 T4 S5 k //assign() vector<int>v3; v3.assign(v1.begin(),v1.end());2 T, c" w& V; k7 s) p8 |- M print(v3);& F; k ^2 {! R2 j; T {, ]* J //n个elem方式赋值 vector<int>v4; v4.assign(10,100);0 b; S0 j/ r& ~$ q print(v4); / H' N+ i5 g8 n3 a }, _! I6 O2 q- j- l8 X int main() {* w2 E8 _ Y) t F test();6 k0 D* n) Q, z4 w5 A } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out! W/ B$ n1 W2 Q' K. R" ? ]0 1 2 3 4 5 6 7 8 9 8 c! \9 o |8 o! M( _. X- M 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 + t% _& v- a* g5 U! O/ s " c# O' ^# u" T2 ]1 i8 O( n 四、vector容量和大小: 1、功能描述: 对vector容器的容量和大小进行操作 2、函数原型:
代码应用: #include <iostream>6 H2 e, I0 h5 k( d- {8 L#include <vector>$ s8 z3 C0 g! Z" l" g using namespace std; 7 H1 h# i$ q' I' K void print(vector<int>&v) {2 R% e( ?( _9 W for(vector<int>::iterator it=v.begin(); it !=v.end();it++) { cout<<*it<<" ";0 J% S; t& k% Z }& P F0 J* H1 h/ A3 i& B cout<<endl;5 A9 C0 a6 |3 B5 z2 ? } void test()5 L* c6 ]4 H3 p7 W; Q( Z {# o5 Y+ M! E- g( P& @! ]; {: X. o k vector<int>v1; - H& r% Q, I8 S for(int i=0;i<10;i++) {* V7 {; \9 n5 ^8 |$ D4 d% G$ f v1.push_back(i); }( m+ V- [4 j$ F m6 z print(v1);& q+ O$ c q5 p; Z //判断容器是否为空 if(v1.empty()). x* k% w% \1 [5 {/ }% [, S# n* B {# t) h3 B, D8 k: ]( J- }# [, _& j cout<< "v1 is empty"<<endl; } else {3 F6 Q Y! X h$ y' \ cout<<"v1 is not empty"<<endl;) l. V! ^' Q9 n cout<<"v1's capacity is : "<<v1.capacity()<<endl;# E! B7 A- i5 P cout<<"v1's size is : "<<v1.size()<<endl; } v1.resize(15); print(v1); 9 {* a0 t2 u3 E6 I6 Q% f$ Y v1.resize(15,100);//利用重载版本,可以指定默认填充,参数2,如果重新指定的比原来的长了,默认用0填充新的位置 print(v1); v1.resize(5);//如果重新指定的比原来短了,超出部分会被删除掉 print(v1); } int main() { test();1 \0 s$ F* b; n } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out! z- w. |& u# n( V ~% U& R& Z0 1 2 3 4 5 6 7 8 9 9 ^ H+ u, u2 H4 {5 v. w) X v1 is not empty v1's capacity is : 16 v1's size is : 10 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 # M/ q n. I' I z 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 ' a1 w. a# Q5 d2 M4 Q$ ^. ] 0 1 2 3 4 ( d/ u4 c( Q# C* m+ x 五、vector插入和删除: 1、功能描述:对vector容器进行插入和删除操作 2、函数原型:
#include <vector>, [4 b7 b( [/ K5 @ using namespace std; void print(vector<int>&v) {) F% \$ Z2 O- K6 o. `# z3 z for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {9 J+ C* C( { y {% X: ] cout<<*it<<" "; } cout<<endl;1 a& [; M; K& c$ R( T% Y z5 I4 {0 V } void test() { vector<int>v1;7 w2 B0 y$ t" U+ U# z8 K //尾插, ~" r, u8 B8 R9 h v1.push_back(10);4 Y6 D* x$ Y6 ?0 r# A- h8 H1 v' U v1.push_back(20); v1.push_back(30); v1.push_back(40); v1.push_back(50);8 Z6 T; ~. N; i# ?( t+ H/ R! r! P 8 {$ V3 N) {+ A4 ` print(v1); //尾删& U- j% k1 k- t& Z0 V v1.pop_back(); print(v1);+ v( `/ C. u8 f //插入,第一个参数是迭代器3 p' ^% R1 Z, P2 R, U v1.insert(v1.begin(),100);+ p" G: j/ w; t7 T* T0 p% P/ F print(v1);% ]* u! [4 Y8 a3 Y5 [ v1.insert(v1.begin(),2,1000); print(v1); //删除9 Z9 n3 g6 m+ s* u6 B' F7 Q; J v1.erase(v1.begin()); print(v1);6 l2 v! a. D! h- j, u9 E6 G //清空 v1 E5 |( u7 W v1.clear(); print(v1);* Z( M3 w0 M7 }: J+ @" Q/ T. A }4 R9 R4 R0 V8 e* W" ]8 ~+ b) Q. h int main()1 Y1 x% [4 c% c% R& F {" B0 o: |9 K. W G# Z( K test();& N5 [- _* Q& ^3 z* m0 R2 p+ ^/ d }2 S1 y6 ]0 ]* @ ]( G4 h 结果输出:) A& r c) D/ Z7 \ root@txp-virtual-machine:/home/txp/test2# ./a.out- g" W, y3 E/ _ 10 20 30 40 50 10 20 30 40 a( I; Q6 O( I' l0 D 100 10 20 30 40 # v! h& }; J Y 1000 1000 100 10 20 30 40 6 M+ P: l& `- Z, ?5 A' W' [* P 1000 100 10 20 30 40 1 P/ w6 N: A" H q$ o" ?( K 六、vector数据存取: 1、功能描述: 对vector中的数据进行存取操作 2、函数原型:
代码应用: #include <iostream>/ P& R2 s$ r, [! _5 u#include <vector>6 H1 R& ^8 F/ p using namespace std;/ X% i) ^5 B3 {. \5 c /*void print(vector<int>&v) l, ?* u: H6 }# n { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)5 g* D, |% \2 F3 W/ z: Y& e: X% _3 \ {: c! B$ [* w' U9 V+ l* _% x+ t9 } cout<<*it<<" "; }: o2 Z. f4 D1 d% A, A5 P cout<<endl; }*/ void test()5 U% r# ?6 j5 P7 e6 s: H y+ j { vector<int>v1; for(int i=0;i<10;i++)1 G& x6 A' A2 G* q9 C { v1.push_back(i);7 w- x0 s% w4 M( Z7 E" { }$ v: L" \$ _8 c5 C# O; | for(int i=0;i<10;i++) { cout<<v1<<" ";/ r! O. Z, n: u3 \ }1 L* U- R A. k2 j* J cout<<endl;* q7 E% v6 u2 [) j) A b for(int i=0;i<v1.size();i++)& S6 N; s1 ^' A$ `& e9 c) y& A {8 z0 \5 e* T- H. z0 {! Z! r- _" b cout<<v1.at(i)<<" ";( r" d# d% n- G9 u! _ } cout<<endl;9 k" Q4 w" R+ D4 a8 N% r cout<<"the first elemt is : "<<v1.front()<<endl;5 l3 y8 `; p* m! ~ cout<<"the last elemt is : "<<v1.back()<<endl; }' s7 U$ x' f$ c* d int main() { test(); } : ?6 Y; v. T ~; Y- x( H: e9 T 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 - c$ b9 v5 w! U" C' K the first elemt is : 0/ I) N" [: i4 ?! t# s: {) ?/ q% s the last elemt is : 9 七、vector互换容器: 1、功能描述:+ r9 Y1 U' [, P/ J- b' H 实现两个容器内元素进行互换$ [: C( h6 |. m7 B& I 2、函数原型:: m- @# K- z1 l7 y swap(vec);;//将vec与本身的元素互换% b+ r; [+ R+ ~4 T! H 代码应用: #include <iostream>( h( L+ j1 \0 k4 `: c2 N#include <vector>! t' |1 U/ X0 @5 X. F/ i using namespace std; void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)7 l/ e( \, o# x { cout<<*it<<" "; } cout<<endl; }, A, _! A" h* b( l3 b0 b- g- f+ M void test(). U: ~0 u6 v' i: Z5 L { vector<int>v1; for(int i=0;i<10;i++)" z3 o4 [" W$ n9 L% F: ?' p4 W5 z {7 f. k) Q: z) W2 h v1.push_back(i);- C+ g l" H! Q2 W }. u; U/ z' t: I! j print(v1);! p+ M3 y+ w1 k' n4 u" _ # P/ Q) D$ p. Z5 X; |1 a vector<int>v2; for(int i=10;i>0;i--) { v2.push_back(i); } print(v2); r6 x$ I, G0 K7 o+ q9 H cout<<" after swap() "<<endl;$ A" X9 U1 y6 H5 f) Q4 [' H v1.swap(v2);& [7 T H( F/ k, |6 L print(v1);- a) p& q# C$ [( }9 Q2 g print(v2);/ O7 l' @* ?0 W& P9 j } int main() { test();: W) t! ^' D) X5 b4 q% G }1 j) b* j& N5 c) { 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out0 1 2 3 4 5 6 7 8 9 ' C! w$ J+ F( Z6 n0 X$ W 10 9 8 7 6 5 4 3 2 1 + ~8 ~. u7 \3 l9 K; H after swap() * O2 J6 r$ m2 g. L( s6 Z V+ |9 h6 ] 10 9 8 7 6 5 4 3 2 1 ( E- ?. ]) x: S/ v, i 0 1 2 3 4 5 6 7 8 9 $ ^; i3 s2 r9 L6 o+ u) K 利用swap进行收缩内存空间 #include <iostream>! H2 ]6 i& d3 ?6 U4 T+ k5 J#include <vector>$ U# r" ^' D% U+ U using namespace std;- `# A8 W% B& f& v /*void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++) { cout<<*it<<" "; ]9 C/ k4 e8 w; Z } cout<<endl; }*/ void test() { vector<int>v1;$ [* g4 p0 b+ R for(int i=0;i<10000;i++) { v1.push_back(i); } [8 F5 c: W! k) ~$ ]+ ~) s cout<<"v capacity is : "<<v1.capacity()<<endl; cout<<"v size is : "<<v1.size()<<endl; " K+ q" \; s* G4 |, J v1.resize(3);7 }/ u0 q- [- _7 ? Z% x cout<<"v capacity is : "<<v1.capacity()<<endl;7 B8 \( H$ g% I! Z/ L _1 `# \ cout<<"v size is : "<<v1.size()<<endl; 2 ]: R9 i% n% S" H. k9 l vector<int>(v1).swap(v1);+ Q9 W& Q% {8 r$ M) g) j* F. V+ n cout<<"v capacity is : "<<v1.capacity()<<endl; cout<<"v size is : "<<v1.size()<<endl; 3 j; F' N" d; l2 Y5 S4 `& S- g }9 L3 \. o) A: L; h int main() {) ^& }; b1 ~8 U- h test();; S7 s0 S7 x% a }4 D; |& ?% W/ k4 P' }5 J* c 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outv capacity is : 163846 h4 |6 @/ T/ _/ k v size is : 10000 v capacity is : 16384 v size is : 3 v capacity is : 31 u8 G5 F9 b; [# a5 D v size is : 3" L, C n4 p1 a0 ?9 B 八、vector预留空间: 1、功能描述: 减少vector在动态扩展容量时的扩展次数 2、函数原型: reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问 代码应用: #include <iostream>#include <vector>0 Y3 U- v5 m/ B6 Z5 z0 o+ f6 }1 L using namespace std; /*void print(vector<int>&v)) M. ~7 z: _' l {$ J/ r% R L: J& H2 O for(vector<int>::iterator it=v.begin(); it !=v.end();it++)# ^. u7 y- z$ `7 m( d% B { cout<<*it<<" ";+ G! C$ @- x p }/ u' { [7 [* D8 {2 j i cout<<endl; }*/3 B3 A1 Z& c/ `6 K void test()- n+ k# S8 u u& S { vector<int>v1;# b- u5 m$ v" I int num =0;//统计开辟次数% l3 v' E- a* }' T' [# \ int *p = NULL; for(int i=0;i<10000;i++) { v1.push_back(i); if(p!=&v1[0]) {( e: h* v, V# i% n" W8 A3 W p = &v1[0]; num++; }" Q, Z: I% ^1 C [+ Q- A7 H } cout<<"num= "<<num<<endl; }1 s% R1 I" c0 t7 d( G* b9 P int main() { test();% Z0 O# ~/ a4 U3 A% w; v6 F& L$ L } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outnum= 151 f2 z( ^$ ?( c2 L2 ?' | @ ; B& N1 w; y8 n& j |
微信公众号
手机版