|
一、vector基本概念: 1、功能: vector数据结构和数组非常相似,也称为单端数组。 2、vector和普通数组的区别: 不同之处在于数组是静态空间,而vector是可以动态扩展的。动态扩展它并不是在原空间之后持续新空间,而是找更大的内存空间,然后将原数据拷贝到新空间,释放原空间。
注:上图中的push_back()和pop_back()函数分别表示往容器里面插入数据和从容器中拿走数据;begin()和end()是我们稍后会用到的迭代器 3、vector容器的迭代器是支持随机访问的迭代器。 二、vector构造函数 1、功能: 创建vector容器 2、函数原型:
代码应用: #include <iostream>5 ]' m0 e" k4 n6 z0 p+ _" B#include <vector> using namespace std; //输出打印 void print(vector<int>&v) {! f+ b6 o" X" R# a" p for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {) g2 Z+ r0 Q- \ I9 \ cout<<*it<<" "; }; d/ E0 x- I: R! i6 j; Y cout<<endl; }9 \) Y4 Q/ j2 T4 ~. l7 ^ //vector容器构造) D2 p; }& C) Y. f4 v void test() {% J3 ]; M1 u1 I5 O! U vector<int>v1;: R% x0 m4 H! K2 H# ^, ] for(int i =0; i<10;i++) { v1.push_back(i);- }" P) L/ L" r& m1 ]9 v; m4 Q! q7 A }9 i. Q- `5 i( I$ k print(v1);; I" v* r( F9 V. K. `, g //通过区间方式进行构造: e6 m7 N) a' k" y vector<int>v2(v1.begin(),v1.end());% e" c' \6 `# r6 b print(v2);9 M- S2 s( z5 ]/ |: ~) s //n个elem方式构造. O, H; {) [& H7 P. I* u4 b vector<int>v3(10,100); print(v3); //拷贝构造 vector<int>v4(v3);9 Z, U; f4 ?# o5 r7 @ print(v4); ' H' s c r( l }: z( b- L8 s* R int main() {9 ~) B+ h Z, m- H A test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out' n% M% ~8 e! Z9 e0 A0 1 2 3 4 5 6 7 8 9 , G4 `! u j$ \0 f 0 1 2 3 4 5 6 7 8 9 , L! p8 F9 k% q$ B ~ 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 ; L/ g5 A$ D! q, c8 M$ N 三、vector赋值操作: 1、功能描述: 给vector容器进行赋值 2、函数原型:
代码应用: #include <iostream>#include <vector>3 }- J3 I4 O+ X+ g using namespace std;- X( \; k5 V9 q% g' d& e+ L5 S" w 5 E% c5 {7 B! j- G void print(vector<int>&v)0 C% s: Z+ x* F {, [" H" P2 i8 ^2 `- J, S) R for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {* w( }7 U, J6 E8 n cout<<*it<<" "; }7 n8 i& ^% v: A) M& |* {# ?: @5 I cout<<endl; }% u: A# t" |+ V void test()( V- K$ R2 l% A {4 w7 }& m% Y' G$ O) N vector<int>v1; 8 ]7 [, V! K6 F7 { for(int i=0;i<10;i++) {# N- _" E x- N) ^' x v1.push_back(i);% k$ e1 p; E) {( L9 |9 N2 } } print(v1);# d# N5 `! s3 y //赋值操作 vector<int> v2;/ `' w0 f1 d) E" l& f8 u# J I v2=v1; print(v2); //assign(). i* x ^" c6 g# x vector<int>v3; v3.assign(v1.begin(),v1.end());- g( Z2 O* d7 u( } print(v3);$ e" J4 l+ H- Z2 _/ f0 L# n! I! c6 H4 E //n个elem方式赋值 vector<int>v4;- o }- q& ]8 H! |# F+ y v4.assign(10,100); print(v4); * H: c* g% Y# [6 a2 { * H; B+ p" F7 d* \, y } int main() { test(); }$ e) s, c0 \9 B2 C: B 结果输出: 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 0 1 2 3 4 5 6 7 8 9 ; v) t% O/ r+ ?1 I0 r 100 100 100 100 100 100 100 100 100 100 6 p/ j& ~8 t+ [5 Z m$ ^$ m ) V3 J: s0 g1 a4 E9 ^ 四、vector容量和大小: 1、功能描述: 对vector容器的容量和大小进行操作 2、函数原型:
代码应用: #include <iostream>#include <vector> using namespace std;, ^. j5 f, q/ V) ?& o- a void print(vector<int>&v) {! a& v n6 H1 v9 A5 J+ F for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {$ U6 g( R6 Q+ K6 s, c2 d0 w2 x cout<<*it<<" "; } cout<<endl;- c4 q5 g Q/ m" E- ?. ?4 } } void test() {3 x5 d' \2 R. g. t6 N7 Y vector<int>v1; for(int i=0;i<10;i++) { v1.push_back(i);' R: B Y" R" ^9 C5 _) s8 ]" Q0 C8 ? } A- y0 g: b6 m print(v1);" r! _/ q5 f) W/ E/ d5 B //判断容器是否为空( M- J: T5 c4 U6 I* i9 K: G3 F if(v1.empty())9 I$ X4 S# r! q' ~9 f; u2 [ { cout<< "v1 is empty"<<endl; } else {6 C9 y7 o: H! ]7 b+ s& p cout<<"v1 is not empty"<<endl;+ i- e) B& m4 \ cout<<"v1's capacity is : "<<v1.capacity()<<endl;' J0 z6 q( q1 M( n cout<<"v1's size is : "<<v1.size()<<endl; k4 U2 m1 t' n }" ~# P3 \- s* u9 T5 g8 D v1.resize(15);6 B+ V' v$ k+ |0 j2 p1 ~ print(v1); v1.resize(15,100);//利用重载版本,可以指定默认填充,参数2,如果重新指定的比原来的长了,默认用0填充新的位置& |6 [3 ?7 M: i U print(v1); v1.resize(5);//如果重新指定的比原来短了,超出部分会被删除掉 print(v1); } int main()7 ^- w9 j# C: i& m { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out) y5 ?% m( U0 A1 F& p0 1 2 3 4 5 6 7 8 9 / T" d: E4 \, y7 y3 x$ `5 z- T v1 is not empty$ {9 s8 ^. Y: {; K D v v1's capacity is : 163 u* O/ u7 S( f$ ?" B) P v1's size is : 109 o8 Y& o; T L& M- s 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 & r) C2 @2 w4 b& ]- Y( l 0 1 2 3 4 5 p9 b& I! N# x- g4 l, a 五、vector插入和删除: 1、功能描述:对vector容器进行插入和删除操作 2、函数原型:
#include <vector> using namespace std; void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++)+ M% a' `- N6 c1 N { cout<<*it<<" "; } cout<<endl;$ D1 W3 V) `) R* W3 N2 H }4 ^# M2 `! I; G5 Y! J. }; w# g' t void test()+ _& ]/ Z7 z9 M1 |- w3 H { vector<int>v1; //尾插; e( B) l) a. h4 a v1.push_back(10); v1.push_back(20); v1.push_back(30); v1.push_back(40);% X! i: [% ]8 {6 Z5 Q' X% P( f v1.push_back(50); print(v1); K3 e# H& Q: H! ?' H //尾删 v1.pop_back();$ i4 g2 |& X1 n/ X- v: B% K5 u print(v1); //插入,第一个参数是迭代器0 U0 M* O T2 `" | v1.insert(v1.begin(),100); print(v1); v1.insert(v1.begin(),2,1000);' e& \) Y( X! V) @" K print(v1); //删除 v1.erase(v1.begin()); print(v1);+ Z: f" \. H8 {4 c& }$ q //清空6 Z; L5 g7 o4 G* M, ?! P v1.clear(); d5 L( k9 {8 n+ `! w0 f print(v1); } int main()+ _; p1 W& C- u/ Y' h; l { test();; b" V) f! a5 R9 D4 ]4 c) N0 l- D } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out+ q) R3 A% `6 A0 V: ?" l f: s 10 20 30 40 50 2 c5 U3 z% j6 Y! ^" W- Z+ m 10 20 30 40 100 10 20 30 40 * ^1 V# ^. D/ `/ z1 p* j 1000 1000 100 10 20 30 40 1000 100 10 20 30 40 5 E/ L' a1 h% A+ i! \$ O& [% F 六、vector数据存取: 1、功能描述: 对vector中的数据进行存取操作 2、函数原型:
代码应用: #include <iostream>) g* [/ q8 H o0 {! {: `/ F#include <vector>1 _7 L+ N3 ?4 O9 i using namespace std; /*void print(vector<int>&v)7 X; r# Z" M) ]+ q" n {/ K$ w- w2 I2 t5 ^ for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {& r8 m# Q% [4 S# q: e$ ]3 g2 s cout<<*it<<" "; k6 @$ J: \8 s" w9 [ M& K5 q+ c }9 G7 C7 O. _: y2 F7 N& f; H cout<<endl; }*/ void test()2 H$ R+ N6 ], L1 f. E {6 D, m& m6 }/ d: v; v8 Q vector<int>v1;4 M& g3 [$ [* {' a- H. e for(int i=0;i<10;i++) { v1.push_back(i);, w) o: T; \8 B0 H' D } for(int i=0;i<10;i++) {5 g1 H- ?4 Z+ M! W: S cout<<v1<<" "; }! @: u5 ^# z |% R7 o) a cout<<endl; for(int i=0;i<v1.size();i++) { cout<<v1.at(i)<<" "; }/ Y) x, e1 n# Q5 b cout<<endl; cout<<"the first elemt is : "<<v1.front()<<endl;4 _; e& j% W( J cout<<"the last elemt is : "<<v1.back()<<endl; }0 d' i$ X: g M3 X- N/ U; s! i- _+ e int main()% h- R' r3 C7 j: ~0 g+ @ { test();* k; a( E" E' Z7 {$ }/ U% v1 D, } }/ h- D5 E5 p2 C+ R . I, c" ^; G( K' [ 结果输出: 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 the first elemt is : 0 the last elemt is : 9" `3 p K: ]6 Y% k 七、vector互换容器:" [& {( m$ l7 {; `# v 1、功能描述: 实现两个容器内元素进行互换$ }! i( ^, |; _7 ] 2、函数原型:# l1 c3 h; K, T8 `# a7 c! r swap(vec);;//将vec与本身的元素互换 代码应用: #include <iostream>#include <vector> using namespace std;* I# W, g' Z0 k* j6 v0 E void print(vector<int>&v) { for(vector<int>::iterator it=v.begin(); it !=v.end();it++), ~8 _7 i6 g* {1 g$ `' ? { cout<<*it<<" ";. Q: W' D! P/ X' d }" A2 |" w' c4 f; `& U cout<<endl;$ K& M7 `" |$ ? }* O/ c% N6 }2 v( l/ T void test(); S8 Y0 ^+ L \0 M2 I {: Y2 l) p- Q0 X G; W. b vector<int>v1;; I! }+ S- b) m1 w0 B ` | for(int i=0;i<10;i++) {/ w6 {' f2 u6 X* [- L' s* A v1.push_back(i); } print(v1); $ K* N. t- q0 I- Q# x/ c4 w vector<int>v2;/ y9 G! Z9 I' I5 C2 I+ v0 i for(int i=10;i>0;i--)! G6 k% X* I2 E, K) R {- D2 ~- v4 i. K: L% F; Y& \7 c v2.push_back(i);2 M( M! g4 ^( N- z } print(v2);% U/ a3 w1 u; a; u+ w7 f3 ?0 b cout<<" after swap() "<<endl; v1.swap(v2);2 E4 N% G8 ~0 `. p, @+ { R5 ]( G print(v1);( v6 n. q B6 Q print(v2); } int main() { test();" r8 `$ B5 c/ N }% A/ W) m5 V. d) x9 A& p 2 P8 D3 t! I6 v, l 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out0 1 2 3 4 5 6 7 8 9 9 o* N0 \1 h. f V7 ?5 |& | 10 9 8 7 6 5 4 3 2 1 after swap() 10 9 8 7 6 5 4 3 2 1 - X# f* j4 _3 @* r) ] H7 H! Q6 O 0 1 2 3 4 5 6 7 8 9 利用swap进行收缩内存空间 #include <iostream>8 B5 u3 F* C6 m#include <vector> using namespace std; /*void print(vector<int>&v)) |( S/ N, U! W% s { for(vector<int>::iterator it=v.begin(); it !=v.end();it++) { cout<<*it<<" "; } cout<<endl;# b# D- Y3 g9 `9 v: Y4 R) j }*/* m( b9 u4 G/ G5 U. h1 t void test() { vector<int>v1; for(int i=0;i<10000;i++) {$ f ]9 Q5 O- U2 j* G5 c4 e v1.push_back(i); } cout<<"v capacity is : "<<v1.capacity()<<endl; cout<<"v size is : "<<v1.size()<<endl;) H% W7 [8 Z/ K! J , L0 ~+ _% I# v& i v1.resize(3); cout<<"v capacity is : "<<v1.capacity()<<endl;' r7 a* m8 X0 ^+ B cout<<"v size is : "<<v1.size()<<endl;+ k% ~( `7 j( J9 O5 [4 @ vector<int>(v1).swap(v1);2 g- R+ \& P$ G5 G) `* O cout<<"v capacity is : "<<v1.capacity()<<endl; cout<<"v size is : "<<v1.size()<<endl; 4 e3 h* r6 }0 E2 H } int main()4 N7 k/ q9 r+ Y1 M- d { test(); } 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out( c9 Q$ S* u# U" y) \v capacity is : 16384( U6 X7 X$ Y: h- B; m* y. i v size is : 100004 S- ]) ], b' m6 Z3 b" ^6 D v capacity is : 16384 v size is : 3 v capacity is : 3 v size is : 3 八、vector预留空间: 1、功能描述: 减少vector在动态扩展容量时的扩展次数 2、函数原型: reserve(int len);//容器预留len个元素长度,预留位置不初始化,元素不可访问 代码应用: #include <iostream>#include <vector> using namespace std;; j# a3 G+ ] b. ?" |7 h' @ /*void print(vector<int>&v)1 H- J% Z0 h: k/ Z1 A. Q* t) D {2 e0 @2 g4 I6 r+ X3 `9 \" m. h for(vector<int>::iterator it=v.begin(); it !=v.end();it++) {+ s# R, k# t5 I* g: t cout<<*it<<" ";( z/ X. E+ B: ?: {8 o# ~ }! e" y. G& T! b9 U cout<<endl; }*/5 ?/ W1 ]& s0 ? void test()) x% v& K" h# ]6 b8 r {. x- y9 p! O; v. S vector<int>v1; int num =0;//统计开辟次数 w/ m( o- w( B# L- ~ int *p = NULL; for(int i=0;i<10000;i++): v- V+ Z% K" m+ H# m6 O# I+ w { v1.push_back(i);9 a) B: w0 ?$ ^' g if(p!=&v1[0])6 U3 S- }5 z* G; J6 N h {8 M. u! ], \ b p = &v1[0];5 h, b% _+ \* }9 y! |" B/ Y$ b num++;! R7 a& u# A5 G/ c } } cout<<"num= "<<num<<endl;9 T! d" O7 B: N+ w1 U1 I( }& ~ } int main() {# Q! v @5 Z( q5 u* Y test(); }0 [, v. m* G3 N2 x; s! c 结果输出: root@txp-virtual-machine:/home/txp/test2# ./a.out6 ?# j6 `) x0 {num= 15 |
微信公众号
手机版