|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数9 H3 B2 x2 ? r7 x% B+ i
Map<int, string> mapStudent;# W; m8 _' x* y) o' j2 N( i
2. 数据的插入' w7 H( t; l* D0 H+ i
在构造map容器后
: {7 B* V/ S C% L第一种:用insert函数插入pair数据 V: I! [* Z$ m
#pragma warning (disable:4786) )
" Q8 { A& T5 ^$ Y0 Z4 ^#include <map>
( W3 C5 q5 G6 G% z; Y- x7 ]#include <string>% j0 L5 I- l* |
#include <iostream>
' v H" q" @" t3 b pUsing namespace std;
) H( C2 S6 r* Y& dInt main()
6 m2 j2 K r$ b+ Y{8 d! ~3 [# ?0 ]/ S
Map<int, string> mapStudent;: h( s. Q& j& d! E$ x
mapStudent.insert(pair<int, string>(1, “student_one”));
2 \- Q/ s1 R& {0 j: ]% E mapStudent.insert(pair<int, string>(2, “student_two”));9 _+ N0 v. x2 O
mapStudent.insert(pair<int, string>(3, “student_three”));
0 n/ V8 c: L6 K6 f* r map<int, string>::iterator iter;
+ @: A9 K$ g& w5 s# [: t* r for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
6 k# s7 \5 \6 O{% I8 t$ E" I9 ?& x5 l
Cout<<iter->first<<” ”<<iter->second<<end;
( f6 }* C+ M1 ]) k- o}3 M6 G2 }9 _+ L& p& O; w- H7 z z/ |
}
8 F/ }- d" a. K/ Q7 u$ a第二种:用insert函数插入value_type数据,下面举例说明
% `2 h% l Y* G& c$ P4 A" V* f#include <map>
% s# b1 {" f9 H [8 K* W#include <string>
. z# x! f8 A4 L* O#include <iostream>
% M2 p1 ?: i8 U0 h+ C6 LUsing namespace std;
$ G* M! q" a1 q. |. @% e) G; ~5 ZInt main()
: O2 K& E* I) G: \+ z! O{
. `; ]4 T- y; ?1 X# F Map<int, string> mapStudent;% k. T# U$ e( B
mapStudent.insert(map<int, string>::value_type (1, “student_one”));+ F9 h$ |6 T0 [4 M* I
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
7 Z4 C3 z- F! y mapStudent.insert(map<int, string>::value_type (3, “student_three”));
0 M2 ]; j! e2 ?( h' ? map<int, string>::iterator iter;
0 j% _* {, c9 Y% K6 K9 k3 G for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
! j+ O( R% S( r0 S" o1 Y{, U4 L% H9 E. j" E/ b
Cout<<iter->first<<” ”<<iter->second<<end;
% K# M. v+ W- H4 l; j}
! O' J& V1 e5 V h}
o6 ~2 s4 a& d8 K; Q b1 _7 d第三种:用数组方式插入数据,下面举例说明
% w. ^ c5 u' y6 n" q: ]3 r* X#include <map>
0 g) T! D0 W8 H$ [! l; R; Q#include <string>; Y. I% `$ n9 f
#include <iostream>
# X6 f: w" R0 f. ~# I# P7 y9 JUsing namespace std;& U, f) s7 t- b& Q# g. L
Int main()
( T7 K; ~8 y1 e, `7 D{; ~, a, Z4 N% o, |4 u, \0 s
Map<int, string> mapStudent;/ a0 D6 i8 x% S$ |8 G
mapStudent[1] = “student_one”;
( K$ n1 K6 @8 Q8 D, X/ I mapStudent[2] = “student_two”;
+ ~5 p* j9 _) `6 W mapStudent[3] = “student_three”;& Z: r" S a/ H+ n/ R6 w
map<int, string>::iterator iter;! m$ h5 t. A+ D6 f$ R3 R
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
4 i: y8 D5 i7 M6 a{
, ~$ ^2 C: E; J Cout<<iter->first<<” ”<<iter->second<<end;% }3 N* ]4 a7 X$ L4 i5 W. {* ^
}
& Y2 k \0 m2 |; u( ~! k; {8 U* \}
3 B. ^! y V: s2 P& R以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
' _ _1 H' B; smapStudent.insert(map<int, string>::value_type (1, “student_one”));
0 C, c2 d9 n% R! w8 p* D+ OmapStudent.insert(map<int, string>::value_type (1, “student_two”));
; B& D8 S+ L" w, i) _' |* `% A5 q2 `上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下6 J3 K& g; g0 f9 J
Pair<map<int, string>::iterator, bool> Insert_Pair;
) p+ t3 N/ _' U. h/ LInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
/ p0 Q% ^. T; j" Z( k我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
2 Q8 c" l' W/ h9 w下面给出完成代码,演示插入成功与否问题
x. s1 Y/ j' a. x#include <map>; t& z$ b3 V7 T6 A- j
#include <string>
9 C, t8 `7 E$ Z#include <iostream>
5 h1 h4 U# ?- K0 ` yUsing namespace std;2 W" A4 G5 j, k/ N! Q
Int main()
# F+ Y2 n8 r' f{7 x7 K8 l; I" ^5 D
Map<int, string> mapStudent;% S4 [9 H* r& m1 M! n, V' ^
Pair<map<int, string>::iterator, bool> Insert_Pair;1 Z/ E& }% { Y6 _ A/ v% p0 T/ c, V
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));1 j* q# Y8 S2 u
If(Insert_Pair.second == true)6 z* V1 U7 T' b) b* e, B
{
1 b4 _, ^; r! ? Cout<<”Insert Successfully”<<endl;0 ]0 K# ]/ D; L" }+ V& f
}
; v* f% N8 w$ r J Else, Z' F6 @5 t/ [! l
{
& k n- M+ u$ K# m1 H Cout<<”Insert Failure”<<endl;- |" M8 Y& D, ^5 I' J! \, z
}
, j5 D2 W0 A0 U% \) g |
|