
一、vector基本概念: 1、功能: vector数据结构和数组非常相似,也称为单端数组。 2、vector和普通数组的区别: 不同之处在于数组是静态空间,而vector是可以动态扩展的。动态扩展它并不是在原空间之后持续新空间,而是找更大的内存空间,然后将原数据拷贝到新空间,释放原空间。
注:上图中的push_back()和pop_back()函数分别表示往容器里面插入数据和从容器中拿走数据;begin()和end()是我们稍后会用到的迭代器 3、vector容器的迭代器是支持随机访问的迭代器。 二、vector构造函数 1、功能: 创建vector容器 2、函数原型:
代码应用: #include <iostream>#include <vector>6 K2 J0 G9 N% {% z# n3 g5 ?5 D using namespace std; //输出打印 void print(vector<int>&v)9 L$ t" h$ U' @, t) p { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)8 g4 K+ N: `0 l# l6 ~6 B {8 P3 r3 n) u; e+ [6 R/ w cout<<*it<<" "; }0 w! J5 w- a# N0 ~4 i. e0 D6 [$ ? cout<<endl;4 b- ]& F C) p/ B# B( s/ u }* S0 N' p( A, E9 S4 h2 |4 O& \ //vector容器构造1 Y8 l! \, N' G! s. O7 m void test() { vector<int>v1;. t7 I9 w/ d7 B# A" ^! G. ^) J for(int i =0; i<10;i++)6 k* h+ j9 a7 o( l6 k { v1.push_back(i);3 d. J" ?2 v2 l7 z0 M }% |0 h2 y0 ]: E. \ print(v1); //通过区间方式进行构造 vector<int>v2(v1.begin(),v1.end());6 p% L% S, f a2 {7 y5 K print(v2); //n个elem方式构造 vector<int>v3(10,100);5 I: [3 K ]$ J# ~% ~ print(v3);! }- w' A. [0 F9 z //拷贝构造 vector<int>v4(v3); print(v4); - G7 t$ O& ]- A0 \ }4 D: ?; l6 [" m" f7 l( b int main() { test();9 v; P5 o' b" z- c } " c7 p; s9 \) W0 t& D 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out/ W6 ~/ F/ |, u5 M1 \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 100 100 100 100 100 100 100 100 100 100 三、vector赋值操作: 1、功能描述: 给vector容器进行赋值 2、函数原型:
代码应用: #include <iostream>#include <vector> j7 d6 n2 [ p4 J$ x, v using namespace std;1 U- Z+ i$ c7 w$ E 0 @2 L* L2 d2 Y4 \; V1 g void print(vector<int>&v) {* r) q: L6 ~; i3 J for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {" I7 l3 b& s# x; c" M, [1 k3 N cout<<*it<<" ";! g) z5 k% ?; a8 Y0 r5 G' Q- A& l, O } cout<<endl; }1 S8 R! r2 c2 Z5 R6 ^7 A, m& P8 o void test()3 r( K! [& x4 V6 Y {2 `' \5 a B* s2 X! S2 F% O( @- Q vector<int>v1; " e8 K& U& c3 Q' f for(int i=0;i<10;i++) { v1.push_back(i);8 {, i" }, C" u8 G" {6 W7 }8 F } print(v1); R) n% `. q! O //赋值操作" F! V2 {1 _' q0 E! } vector<int> v2;3 \2 s- [# p4 @# n* l/ F v2=v1;5 }$ ]3 n1 k/ p print(v2); //assign() vector<int>v3;1 U3 v/ `6 U2 Z! p: y: I# b v3.assign(v1.begin(),v1.end());+ W! \7 L! {% H) m- k M. d% x% k print(v3); //n个elem方式赋值 vector<int>v4;* A$ l, T2 H& ]" a3 O- G/ v) U$ Y* x& x v4.assign(10,100); I7 G, I) e4 @0 z. w' v print(v4);+ d% t' D! M1 Y/ g 2 g8 [2 Q; s; I5 g2 m } int main()9 u2 `" t, T5 K6 q# {2 A" f' @ { test(); [- s* i$ L% v3 x+ e } . N- j$ Y) H4 h% K0 G) C6 s0 ]# y, \ 结果输出: 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 T. N S9 l( H6 y' r 0 1 2 3 4 5 6 7 8 9 100 100 100 100 100 100 100 100 100 100 5 w& _/ H; r' W, \7 t# L 四、vector容量和大小: 1、功能描述: 对vector容器的容量和大小进行操作 2、函数原型:
代码应用: #include <iostream>#include <vector> using namespace std;6 D4 b: X7 O( q- z+ K 8 O3 {% P$ w8 ^' R0 \ void print(vector<int>&v)- m2 u; ?0 w# f( \0 n {" l6 m* t' y- _ for(vector<int>::iterator it=v.begin(); it !=v.end();it++) { cout<<*it<<" ";* P9 [: y! a( z' Q' z) @ }! p9 z& v ~) h cout<<endl;1 j3 d, c3 B$ t' g } void test() { vector<int>v1;( U8 v. n: d& Y0 O! c1 z for(int i=0;i<10;i++)0 t* K, G6 C+ M' i: n8 D/ W2 o {# l% C Q) t8 N8 g9 r; g v1.push_back(i);4 y' v+ n" x; d ]' Y! F }2 {& y% \: G* _ u, ]" e. N print(v1); //判断容器是否为空6 X& B& v7 y% f% K2 }4 z if(v1.empty()) {/ f0 n% ]5 j! ~# c3 ?2 u! D+ g cout<< "v1 is empty"<<endl; }: y& g5 m) Z7 L else {# q' O* c! |# s- Y" q9 w cout<<"v1 is not empty"<<endl;$ B! P, l; J% o! z cout<<"v1's capacity is : "<<v1.capacity()<<endl;/ v, w3 R+ ^7 n0 g d2 \ cout<<"v1's size is : "<<v1.size()<<endl; }4 u$ _% A: t; z5 F& |9 S( j9 ]4 g v1.resize(15); print(v1); v1.resize(15,100);//利用重载版本,可以指定默认填充,参数2,如果重新指定的比原来的长了,默认用0填充新的位置2 \9 f1 J, e: n+ O% ~: T% R h print(v1);" f' ~' U: k, v& W v1.resize(5);//如果重新指定的比原来短了,超出部分会被删除掉8 A. S+ \2 O- s; k" ~2 J* b% v print(v1); }1 r) m4 A D% c+ n- x$ r+ J int main() {6 Q0 _) Q* e/ V9 b+ ` test();) t0 Q& U, o' N. n5 g } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out0 1 2 3 4 5 6 7 8 9 % I5 Y, L) c4 B% I" j v1 is not empty v1's capacity is : 16, Z$ f6 p! Q0 o7 r4 ]% g v1's size is : 10 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 6 |5 p+ k* }3 K9 i# K4 }* ] 0 1 2 3 4 6 z4 s ]" ]& g- P% X, i' c! M 五、vector插入和删除: 1、功能描述:对vector容器进行插入和删除操作 2、函数原型:
#include <vector>+ P% v% \2 S# q using namespace std; void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)3 ?1 n# c9 ^) S { cout<<*it<<" ";# V. b7 A- I8 V% K2 G0 F. J }$ e k# K+ i- r! p. ]) S4 \8 w, I+ c0 m cout<<endl;9 U0 Q. e9 ^; }& q }0 Y7 H: z5 e. d0 l9 \ void test() {/ c. D; k5 X; G# H vector<int>v1; //尾插 v1.push_back(10);! Q: Y$ \* T/ M1 M v1.push_back(20); v1.push_back(30); v1.push_back(40);' ]% Q0 V9 e5 Z v1.push_back(50); ]# B4 L* G0 a; a, p$ T- C6 | print(v1);7 M, P' \$ ~. P1 i( W //尾删9 X% ]+ X# ^. L v1.pop_back(); print(v1); //插入,第一个参数是迭代器" ~$ A/ _! f; P( w# a1 i v1.insert(v1.begin(),100); print(v1);+ C8 z4 M5 J8 k4 D2 ?7 Q v1.insert(v1.begin(),2,1000); print(v1); //删除 v1.erase(v1.begin());3 W; g; i9 a6 \! i print(v1); //清空 v1.clear(); print(v1);7 t1 D; ~! l( E- P }* k, D7 U7 i7 u8 M& C int main() B; i; b2 X$ l$ M4 }/ @% L O {% w% }% F4 d8 \ {2 N test();# p! q; X9 ]2 ~3 y }/ Q3 l* f% M' N& z- _/ H' J 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out 10 20 30 40 50 10 20 30 40 - B3 U9 B( T) u8 ` 100 10 20 30 40 " ]) [% G8 a. q. q' I" }" Z7 J 1000 1000 100 10 20 30 40 4 a2 L% P: k! A: s' a: ^ 1000 100 10 20 30 40 m5 v, R& b' B- L# e 2 D1 v9 H; P) G+ _) j ) X2 b9 u) p1 A% u: a- {" d 六、vector数据存取: 1、功能描述: 对vector中的数据进行存取操作 2、函数原型:
代码应用: #include <iostream>" l& {1 o+ d9 t3 n#include <vector> using namespace std; /*void print(vector<int>&v)& p0 ~9 G8 H0 g" v { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)" ]7 U$ U8 H! q { cout<<*it<<" "; } cout<<endl; }*/ N1 e' `* U/ ?7 P$ s void test()8 ]* `* a4 E* C3 }6 V5 d! u { vector<int>v1; for(int i=0;i<10;i++)6 Z+ f+ a4 G+ R {+ L% F6 @0 F- ^ t v1.push_back(i);; ~" \3 Y! ~8 Y7 U% m. O }# D$ W4 R; h+ r' U for(int i=0;i<10;i++) {2 n/ O* Z) V2 h5 { m% N6 W cout<<v1<<" ";4 R- V, c6 A% m( V) {% W8 V) u } cout<<endl;0 `$ N9 L, I3 X, g8 V0 [ for(int i=0;i<v1.size();i++)/ g& p( v% t6 m$ O2 N {% q# S U; v3 N* I cout<<v1.at(i)<<" ";( I+ m4 z6 \6 W* g: s } cout<<endl; cout<<"the first elemt is : "<<v1.front()<<endl; cout<<"the last elemt is : "<<v1.back()<<endl;* G2 W: g- W( z/ h. q } int main()! b, z# k9 E4 k6 H3 b/ K, [ { test();/ P! C; l9 i6 W8 ], W) E1 E- L$ ^ } ; a, I7 x" I6 }% } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out! E3 I8 Q7 F0 ]* R0 1 2 3 4 5 6 7 8 9 2 r r4 \' K$ S. U7 Q7 i& f 0 1 2 3 4 5 6 7 8 9 the first elemt is : 0 the last elemt is : 96 M' u* v: E9 h$ T, m ( ?0 G+ f& b: I9 { 七、vector互换容器:8 b) `9 a* s0 \! h4 g9 ~ 1、功能描述:* x4 l: _6 E# g 实现两个容器内元素进行互换 2、函数原型: swap(vec);;//将vec与本身的元素互换% m+ i% r# o% u" l 代码应用: #include <iostream>. P0 F- D7 D/ A( b# I/ |#include <vector>4 r9 A1 @+ |: ]8 V' L5 E5 \ using namespace std; void print(vector<int>&v)+ H2 g9 G# ]4 U. n { for(vector<int>::iterator it=v.begin(); it !=v.end();it++). x* v6 R/ P; a {* M/ H0 q) _& O1 a) ]! s7 G5 D% ~- A) M cout<<*it<<" ";+ k9 B$ @; R: K. p$ S7 ~8 z }8 j: i0 W6 ~ E9 u2 y8 e0 S cout<<endl;1 O. w0 u$ }6 A' [, t } void test() {- B7 \0 _1 C! d! F4 _+ Z vector<int>v1;7 }( ^9 ?7 i5 D for(int i=0;i<10;i++) { v1.push_back(i);9 j6 k6 U* S. y. Z }: | ~, K, [7 t print(v1); 8 u* I0 U& M5 C+ b" |+ y vector<int>v2; for(int i=10;i>0;i--)0 G2 q, J* H' d- u {: l) Q# A7 E# |$ B; t- ? v2.push_back(i);. _& ^0 P+ A. K" W# Y: e } print(v2); 2 u0 h" i/ ]) F& p( i- [ cout<<" after swap() "<<endl; v1.swap(v2);+ l$ B, E4 L! q+ a# i print(v1);6 {2 F2 r. I1 {% A* t print(v2); } int main()# n, o, `1 k$ X) R& g {' P5 L% d. M& R/ z' ]% Q" m- W; u test();1 }+ B0 l; e) B, c }2 F) g6 ?5 Z6 @/ h* b' d 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out7 p, \% Y# @/ U0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 after swap() 6 q/ |% `% ^$ o) R 10 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 利用swap进行收缩内存空间 #include <iostream>#include <vector>1 r( J7 b0 r4 q8 B6 ~- N3 h; p using namespace std;! h- Y. s, l! I3 c, T- d /*void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {+ {2 M4 G: h( S* S9 | cout<<*it<<" ";& g4 h6 w- d5 F6 \- y6 } } cout<<endl;# v$ d7 V& P# l2 G1 I0 d }*/! n a0 B9 w# V- k1 x; ^1 W* a, ^5 C void test()- S; r) ^% z3 z {5 K* r6 S9 n) P! U. P# I vector<int>v1;+ W/ v+ [% c) R8 j* _8 T# {( k for(int i=0;i<10000;i++). u3 w* r; P4 e( v+ s2 W) j {- i& c4 i$ }9 ]0 ?/ q( r3 S9 k v1.push_back(i); }3 G) p9 p5 h3 W" P5 o" \: E cout<<"v capacity is : "<<v1.capacity()<<endl;) M6 j" ^, \" R: A+ z* N cout<<"v size is : "<<v1.size()<<endl; 1 c! x. O8 s2 t! Y# s v1.resize(3); cout<<"v capacity is : "<<v1.capacity()<<endl; v' s' x4 y+ I cout<<"v size is : "<<v1.size()<<endl;$ J4 p& N9 X X! b9 k vector<int>(v1).swap(v1);( a+ C' [5 F' b: z7 ]; w4 z9 G cout<<"v capacity is : "<<v1.capacity()<<endl; cout<<"v size is : "<<v1.size()<<endl; }# a! i" |5 B& V; d int main()& h7 d& x) ?1 @8 A {% d! L3 K: w; [& ]% I k' q test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outv capacity is : 16384% }$ p; O( c4 w, N2 U8 Z& t8 a4 x v size is : 10000* I+ x5 Q- L. a/ l f' ~" l% H7 y v capacity is : 16384% S X' a! C: I d. f v size is : 3 v capacity is : 3 f7 B2 O E; b4 h& K v size is : 3( f" n9 r! R5 W, i+ x+ e/ P6 L 八、vector预留空间: 1、功能描述: 减少vector在动态扩展容量时的扩展次数 2、函数原型: reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问 代码应用: #include <iostream>#include <vector> using namespace std; /*void print(vector<int>&v)9 N4 {+ F& x& E" L { for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {% i) T7 ]1 O u) w. h4 I9 f cout<<*it<<" ";0 G2 |3 R+ T2 A: a) t# d4 }7 ^ } cout<<endl;, L' c5 X& @9 b0 q }*/0 N7 c9 v% W8 A7 A2 x8 ^' o c void test()/ \5 F$ l" o7 h9 ?- \/ a1 \9 T {9 ?. N& q" {" ~" O4 ~7 i1 G* o/ g vector<int>v1;* G6 V8 d5 T1 ~# F int num =0;//统计开辟次数9 v& e2 N+ a7 Q8 G* W0 |. f% t int *p = NULL;" n- d$ {1 Z( k( U4 g. q for(int i=0;i<10000;i++) { v1.push_back(i);( `" n- w$ D. K6 C if(p!=&v1[0]) {8 ~) D% K" r/ P p p = &v1[0]; num++; } }" e; p, P) k8 y( U" j; @ cout<<"num= "<<num<<endl;$ B3 j% e! I3 J0 O9 E$ Q } N/ y, R* J/ | h7 |5 c t- r int main() { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.outnum= 15 |