查看: 4877|回复: 0

[转载电子书] C++ 中 map类的具体用法实例讲解

[复制链接]

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

积分
9784
发表于 2014-1-3 19:37:45 | 显示全部楼层 |阅读模式

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

×
1.map的构造函数
) N6 Z  V: k! tMap<int, string> mapStudent;
9 x# j2 ?1 T! O4 J# D' [/ |9 ~6 B2. 数据的插入
8 G1 j1 f+ T1 X) x8 ^- K& W在构造map容器后* j. b- ~1 l, c: N, O
第一种:用insert函数插入pair数据( @, `- S6 x: p0 {
#pragma warning (disable:4786) )4 ], v8 _0 X5 D# y" _3 p0 w
#include <map>
' e( n9 y# _! E+ l4 Y& I#include <string>
+ j. f3 [( w! q# y- S- L#include <iostream>
$ X2 y" k# Y% O" U9 K5 c) t8 uUsing namespace std;, \, H6 K1 E+ h- S
Int main()
! n+ R/ b/ X- Y* y; u{9 D( k+ u+ E! C6 E
       Map<int, string> mapStudent;/ {: [) c; B$ n. z  k7 r
       mapStudent.insert(pair<int, string>(1, “student_one”));4 v+ a6 Z) d* K# j4 M0 `& `3 y
       mapStudent.insert(pair<int, string>(2, “student_two”));
3 c; I# @( f4 m3 H1 T% h$ x       mapStudent.insert(pair<int, string>(3, “student_three”));! v% E, ?, t% J; x
       map<int, string>::iterator iter;+ `) M5 k$ S# P7 Q1 w) A( B
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
, e) Y! k0 _* J7 a! w{
' N0 j" B! m) |- H% q8 b: j0 t       Cout<<iter->first<<”   ”<<iter->second<<end;
( G# f( X' N: N# s* a/ m+ p) B}
7 P2 F- Z/ F1 `# Z9 \6 J8 h: G}* {# a6 \, e/ d
第二种:用insert函数插入value_type数据,下面举例说明
" j+ d. L- S) b#include <map>
6 @* Z6 D" y5 u7 G#include <string>+ f, g$ H4 q. K
#include <iostream>9 j/ r3 ?& V- i3 \
Using namespace std;
6 H0 i' L' y) J# o. oInt main()
9 G3 O" t, B4 v9 t" z2 L  t+ G  D1 }{$ H1 P$ A2 }; W/ f5 G
       Map<int, string> mapStudent;
$ w1 r4 h) ]: e* a; ]1 _; i       mapStudent.insert(map<int, string>::value_type (1, “student_one”));6 W: _  c. O4 f& Q3 K' r: w
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
& M9 g. L0 A) l" p3 }       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
! [6 }$ t/ X/ E7 H4 {1 n0 M# N       map<int, string>::iterator iter;
3 U$ X+ P- e6 ^4 B       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
7 o& u4 n4 J; |+ i0 _' o{
! X/ [* _2 G" g9 I+ {8 t       Cout<<iter->first<<”   ”<<iter->second<<end;
# S. f: q. s$ j& o( a  Q1 r}0 G/ }. i& c  d! M2 c  R7 d
}
+ U! t) O6 O3 B第三种:用数组方式插入数据,下面举例说明
& b8 l! \: `* U" s8 D, [8 I9 d#include <map>- M! v0 S7 }! @7 y, \' u
#include <string>
0 e! E/ k% v& h2 d% r6 r$ M#include <iostream>
$ n+ }3 f7 S1 g. p3 V) V" mUsing namespace std;
9 S+ A2 T# n3 e$ w- NInt main()
% N% b) W" v' P% Z% I" W$ a{
2 P9 U$ x. E" |) K0 [$ L% Y$ Z       Map<int, string> mapStudent;# r' U  t" L5 ^. K+ L# o
       mapStudent[1] =  “student_one”;
; S# R% `8 a. ~9 M1 R8 X6 B' Z$ D5 Y       mapStudent[2] = “student_two”;
9 o: @* y! i9 g1 U( ?4 g& c: d# j( G       mapStudent[3] =  “student_three”;5 o1 {& [2 T4 u
       map<int, string>::iterator iter;
6 e: s- [. P! K( n       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
# p3 q8 F% E# `6 c  s9 d{7 R7 s: R0 C2 D' F8 o
       Cout<<iter->first<<”   ”<<iter->second<<end;
; f, X- W8 V: h# X/ w}1 ?4 Y8 e2 d. E
}
( s) M; T4 L$ U% m以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
* h8 Q1 \/ I: P1 @4 R# fmapStudent.insert(map<int, string>::value_type (1, “student_one”));
) P/ `+ m( v3 g, f; c; [% Q6 }mapStudent.insert(map<int, string>::value_type (1, “student_two”));2 l* @* l. H& a2 O$ [* v2 f
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
$ @; c) s, L1 p# f* FPair<map<int, string>::iterator, bool> Insert_Pair;
9 h' U0 P) ?* ^* j# U' Y8 A$ dInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
2 `; k& o  F; Z, c9 }我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。' o3 G/ M- i7 H) d: p, c6 Q% X
下面给出完成代码,演示插入成功与否问题5 M7 N: q! e5 L$ y/ S$ C
#include <map>6 m2 O% T( f* a; c& R
#include <string>
: `0 t8 N) f# q#include <iostream>+ q) a: @# I/ _7 P
Using namespace std;
. [) Y- L& o7 lInt main()
$ Z$ a9 T  w0 R{  h& a& ]$ |8 F2 g4 h8 m" @
       Map<int, string> mapStudent;
9 ~8 T& Z& V3 t8 a, q! e% T3 CPair<map<int, string>::iterator, bool> Insert_Pair;
( E0 A) C1 D, m2 D/ c       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
! k; Q7 F  n$ [6 m* Q0 ], v9 a       If(Insert_Pair.second == true)1 l# S8 a6 m6 t8 ^2 M/ m! @7 A, u
       {
! ^, W' i# |4 m              Cout<<”Insert Successfully”<<endl;0 L- K" S3 [8 X3 z, u2 `# S
       }* f4 B# Q3 |6 M/ o! n# m
       Else! ^( L& }# X6 n4 X% _/ g' t
       {4 [: D* m8 Y7 U2 x
              Cout<<”Insert Failure”<<endl;8 v: A: Y( Y: R" I  F. b7 K4 O
       }' y$ U4 k) a* [( \
该会员没有填写今日想说内容.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

在本版发帖
关注公众号
QQ客服返回顶部