|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
7 a8 e" a" @. A7 u/ n* s+ hMap<int, string> mapStudent;
5 ^! P; G1 T9 T, `: ^! C9 `+ P$ C+ `2. 数据的插入
l; k. h! _' _; O在构造map容器后
: X$ |8 d+ k# I Y/ F第一种:用insert函数插入pair数据: e/ Y8 F2 S3 C4 f
#pragma warning (disable:4786) )" j1 n4 u/ a+ L& _- `( U
#include <map>' d. B4 p7 ~; S6 H) M
#include <string>. ]3 q* s) b5 k7 P
#include <iostream>
. h$ |' v/ T, C$ k3 TUsing namespace std;8 P* K% s C" L& _3 B) J
Int main()
( {) v: o% @8 ?: I W{
: ^; u; x& ]& \1 j# F1 c' {& [ W Map<int, string> mapStudent;- I) F2 g9 H0 q+ O- a
mapStudent.insert(pair<int, string>(1, “student_one”));: X6 B$ }1 }$ W0 M( e' _4 k( G
mapStudent.insert(pair<int, string>(2, “student_two”));
: F) d4 w0 R. `& [' e mapStudent.insert(pair<int, string>(3, “student_three”));
$ n5 i: N* p3 ?' Y map<int, string>::iterator iter;$ x, c: k+ \8 D; M
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
6 X4 S& K6 T6 m& _+ K{
7 q, O k5 }) c! |* U Cout<<iter->first<<” ”<<iter->second<<end;
% t) [3 M2 N4 ~- Z/ a( y& }8 |}
; |/ b- A! f0 ]: r( n}# H2 N+ h+ V5 C, \) s8 d
第二种:用insert函数插入value_type数据,下面举例说明
7 U0 _/ h$ Z* ]% ^( }. V9 L+ g7 Y#include <map>
4 g: M/ P6 o3 z#include <string>5 ]- P' a9 }$ a( H; S: }
#include <iostream>$ n6 v2 H; c* i0 q+ j0 x, U% B
Using namespace std;
) e+ g) S# ^' m. b( F- S" DInt main()
( o+ [5 d' a& {7 o6 ~8 }% M{
, G1 G, Q5 x& j0 J Map<int, string> mapStudent;
: D4 O1 W `/ i mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( z2 N3 F4 w8 F3 k& O! C mapStudent.insert(map<int, string>::value_type (2, “student_two”));% t( ^8 j' x- [4 ^, e! A
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
5 O" o6 Y; K k3 R; Y8 x map<int, string>::iterator iter;
8 K' l" g% z) u for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++). u9 { { Z& O6 B/ q
{
8 Q- [* v5 l+ K9 j Cout<<iter->first<<” ”<<iter->second<<end;7 t! P8 n! d7 R% M0 X
}; ?& J/ }" m- p. n7 y
}
2 H/ ^* v, X# a$ m第三种:用数组方式插入数据,下面举例说明% ^7 V# m7 b% ?4 N2 ?1 ]8 `2 s
#include <map>- \- l# q. s' T% F, a% [
#include <string>2 K, Y5 D# ?) ?& x
#include <iostream>
, g" r9 H* N' G* y2 eUsing namespace std;
2 b( a2 s. N8 Q% m4 N0 N0 o2 dInt main()1 v) o: [2 v' r2 S8 c" s/ B
{
, w9 F& B" W# a: i Map<int, string> mapStudent;( N5 \4 C" m% \- U* \0 z+ Z+ \' g
mapStudent[1] = “student_one”;1 a% [7 `' H: d1 J- l
mapStudent[2] = “student_two”;; ^% I/ u* C0 J6 z
mapStudent[3] = “student_three”;
! Q' c2 S! d( S( B4 y map<int, string>::iterator iter;
8 v( m9 S8 p+ C: ~" i4 u for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)' O+ }2 J7 u% ^, {" a+ h4 v
{
e6 E/ q$ T' m) m: p Cout<<iter->first<<” ”<<iter->second<<end;; Q& r- W; n" l
}
+ y( A# ^+ ?1 E8 e U}
" l( ^, U0 S" Q' ~( [以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明/ x: {" `) Z$ [: U
mapStudent.insert(map<int, string>::value_type (1, “student_one”));) T0 w+ g4 v& V1 D/ {0 T6 l% |
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
% f; |% z& w; l' Y, o' a上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
, c& \) R. f2 sPair<map<int, string>::iterator, bool> Insert_Pair;" M, y5 [0 T. Q+ L
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));0 L: Y. v5 W$ N2 l: S
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。! \, Y: r9 _' z3 A$ b
下面给出完成代码,演示插入成功与否问题) O/ A o9 ]2 P2 M
#include <map>
6 W, T: ` r' P# ]* I! X- b' Z#include <string>
, M3 @5 `% f2 B0 l, J0 T; a1 p#include <iostream>0 Z, o, @# k1 n
Using namespace std;( G `3 M G' ?* b* ^1 _
Int main()+ x) F) E. k o) d1 F( b
{8 `1 o1 u) M! o C' R _: {7 c
Map<int, string> mapStudent;. V, `; E3 q# C. U& `8 s) X
Pair<map<int, string>::iterator, bool> Insert_Pair;
F, z. U$ D8 m. I( p2 H4 q Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
8 Q1 N* }. z3 ?$ V& G- |2 X$ R# L If(Insert_Pair.second == true)# _% Q( h* K1 d4 {; v
{
2 g: b$ T( h; z+ q# x+ O5 p Cout<<”Insert Successfully”<<endl;7 g- p# \( n/ F) l! Q% C. X
}/ Y p+ Z' ^1 D5 B- d" @
Else
- U9 \7 b' d9 K! ]2 p7 ~3 a% v1 H {& A* b9 y/ }6 u. Y
Cout<<”Insert Failure”<<endl;) ^8 T1 S- f) W4 S; Y
}
# w; |( a2 a) c. U; a |
|