|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数( ~( W! ?+ z+ d* {7 t
Map<int, string> mapStudent;
: |7 E8 [" t" u% }7 P2. 数据的插入" L6 w2 j8 ~# I& L( _$ t6 q. q
在构造map容器后1 y. {3 Z$ w9 ~$ B, ^, f( P( D3 w% p
第一种:用insert函数插入pair数据- r1 A3 s- C1 B/ B R. D
#pragma warning (disable:4786) )
3 _/ [6 H! x8 N#include <map>* Y4 C4 p3 [' h- g
#include <string>
- v0 ^4 [$ R; h' T& D# o#include <iostream>* c2 R! u* w/ V1 h
Using namespace std;9 \4 x% J. G! E1 {2 {: f
Int main()
+ ?* [" \4 p" r: k2 h5 u4 R{9 `- X( L/ H5 m K9 S5 ]& X+ s6 B
Map<int, string> mapStudent;2 x6 k1 [/ J6 I, [
mapStudent.insert(pair<int, string>(1, “student_one”));& l% h* K3 O U5 g1 a3 K
mapStudent.insert(pair<int, string>(2, “student_two”));
! L! k7 x, `6 p$ W" k mapStudent.insert(pair<int, string>(3, “student_three”));) R1 I3 A4 k9 I: V3 h, X$ C/ ~. h
map<int, string>::iterator iter;, G! M% Q5 {+ K- O# l
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- d2 s" L, C8 ^/ R{
- q. t3 S5 y3 v: {/ h/ Y$ I Cout<<iter->first<<” ”<<iter->second<<end;0 o, y* ?7 S$ q( f" Z2 i* T0 A
}0 k: [$ o( r1 P% {6 _ | R
}
+ G& \- S. G" `; A5 Y第二种:用insert函数插入value_type数据,下面举例说明
6 l( _5 m3 C5 D#include <map>
6 W/ D& q% {. s2 ^#include <string>2 G, M! [" s+ P# ?: o
#include <iostream>, H1 V, M6 t0 D- H( }& h. M6 K
Using namespace std;. r# F8 T" `7 D2 V" ], y
Int main()
# Y9 P$ g8 j _9 V% k: G7 k: _{
) |' k# B6 K0 o; c Map<int, string> mapStudent;# G2 g1 X2 v# Y; f; |* w
mapStudent.insert(map<int, string>::value_type (1, “student_one”));. n3 S5 U! ` K9 b+ e8 s
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
# w q3 Y L7 a: J( _; v mapStudent.insert(map<int, string>::value_type (3, “student_three”));
/ y, ]. w E- c% w2 K map<int, string>::iterator iter;+ i1 e. U8 R. x8 u/ g* h5 K1 X
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
7 D5 p: e, S7 U7 p f{$ q: y, ]4 U6 q5 b
Cout<<iter->first<<” ”<<iter->second<<end;! \4 m7 h A3 ]7 u1 r/ P
}
7 l: D2 d9 M- n0 n' f# ^) O}
# i0 G- Z" y( D ~5 U# u第三种:用数组方式插入数据,下面举例说明
3 {0 h* V5 h6 s; `#include <map>; r% v( y, {: v1 ^1 }, Z
#include <string>$ A4 j2 Y8 F6 ^+ f: Y
#include <iostream>
8 i* G% A7 c, G* k$ a2 UUsing namespace std;! w3 K. B7 {3 d8 P/ k+ ~" P
Int main()- ^8 J# D5 a/ J b- W
{
9 V, |5 g9 }: v! M, d& x Map<int, string> mapStudent;
6 X3 n8 F: U. i9 y7 l& O9 C mapStudent[1] = “student_one”;
' t# y X- \: X; v \ E) r mapStudent[2] = “student_two”;
$ L' _. m7 u! v$ V mapStudent[3] = “student_three”;
W6 a3 E& o& N2 I0 U map<int, string>::iterator iter;! ?* H1 I" T! i- O t) I# Q. X
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)8 z% d9 r( o0 m+ \" v
{
: u3 m) h1 _7 E# c0 i/ o4 g Cout<<iter->first<<” ”<<iter->second<<end;
% B6 S0 [8 g& S}
) n3 N" G) I7 X9 Z6 D! `* F0 o}& ?" K8 O" V1 X, C
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明6 T/ e }) M( C" L% ]5 p
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
) R, i: L8 X6 zmapStudent.insert(map<int, string>::value_type (1, “student_two”));7 F8 K* Q- I0 P8 p& ?6 H
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
+ j% M% S, k8 ~; G' m8 RPair<map<int, string>::iterator, bool> Insert_Pair;% V1 C% B2 {. i3 u$ @- |+ ^" ~# Y
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( U( c+ q5 \/ \- Z: A9 k3 g7 v, Q我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。( `. ?8 }1 D# r1 @
下面给出完成代码,演示插入成功与否问题
( v7 |: [9 |: x/ d#include <map>9 S9 Y1 L( V" D# t' B
#include <string>( Y- H) S0 L6 L+ ]9 D/ `2 H
#include <iostream>: P3 Z% g' X( e: V
Using namespace std;/ g6 a( D; J; U( x
Int main()+ G( I5 x( J& r2 {7 a: @, t8 E/ N* u
{% d* w m% [0 T( h
Map<int, string> mapStudent;- z2 Y8 e+ U. h
Pair<map<int, string>::iterator, bool> Insert_Pair;
/ t% B5 ^2 [5 h3 a6 P4 U Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
+ k5 q$ @# f, ?* W( k. {! Z! n If(Insert_Pair.second == true)2 X {8 K6 |$ Q0 G) }; _
{) Y( h8 v t9 y! G ~+ d
Cout<<”Insert Successfully”<<endl;
) z& ]- y% o ^ R/ w" ?5 T' L }& [4 w, |0 Q8 m) \. z+ f
Else2 j' d! f; B0 G* c2 |, h
{
9 W5 v& i# \: G/ W Cout<<”Insert Failure”<<endl;
" _: c6 n/ l K4 T: [ }; a- O* [) E) X/ G- t. I- f
|
|