查看: 4896|回复: 0

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

[复制链接]

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

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

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

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

×
1.map的构造函数
2 Z, b" Z) w  {/ j( u/ v9 K- {Map<int, string> mapStudent;
) I$ j' I5 o' `. u2. 数据的插入& O- V4 X2 W7 {* B" z! }; t  T) I
在构造map容器后
( J# y- j% V' H1 [第一种:用insert函数插入pair数据
# N" a8 `; y* q. P  K1 H7 x+ Q#pragma warning (disable:4786) )7 m' U  G; h7 k+ [; P
#include <map>
4 [$ N# Z) M& L  m9 x9 A$ P  h#include <string>8 T, B0 @0 m) g5 t
#include <iostream>6 F2 W( O2 f5 E/ V! n% _
Using namespace std;
1 Z3 b: r/ s6 Z8 KInt main(): }+ _  |8 @/ |6 L# Q, J. R% c& x* |
{
8 x6 D. S2 Y1 I" w( k       Map<int, string> mapStudent;
5 h- i0 _. `) p+ H       mapStudent.insert(pair<int, string>(1, “student_one”));
3 F3 G; F: i! G7 w- x/ B9 {       mapStudent.insert(pair<int, string>(2, “student_two”));- f0 D& Z9 d% U# P/ W" I
       mapStudent.insert(pair<int, string>(3, “student_three”));; ^# s* \+ {( K! I; z! w. S5 F* {2 _
       map<int, string>::iterator iter;
0 g7 C: U! x: }* o       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)' H) u. r3 j! m: g9 M! m( U
{
5 @" m& u' v8 _9 U1 j       Cout<<iter->first<<”   ”<<iter->second<<end;; O! ^2 N0 r1 ~+ b2 e
}
8 j! W1 j; e/ P5 @0 N}' Y% _3 Y& k- w" v0 g* m. h) T) B
第二种:用insert函数插入value_type数据,下面举例说明
7 C" y$ P" K$ P: C#include <map>
% f  Q; L8 ]" I4 Q3 b#include <string>1 o, ~3 T: j* e
#include <iostream>( L; `7 `/ c5 N8 P
Using namespace std;
% j2 F7 t6 }0 ~& |  wInt main()
  L; N: b  y4 V2 {2 y& Y{* ~1 d" o+ B! `2 o+ W$ A
       Map<int, string> mapStudent;6 ~4 }& B& ^  w: D3 C; L+ ]( V
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
1 K% h+ b0 p6 `4 N6 N/ L% t! U       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
: K% Q6 v' f3 m' t       mapStudent.insert(map<int, string>::value_type (3, “student_three”));* B% R( W& Y2 n, |" R/ Q
       map<int, string>::iterator iter;4 v* ?, d6 o. q  s6 O
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)3 x7 Y6 q" E; G5 T, x
{: e2 O9 k' R+ \+ w) M# O5 l. p
       Cout<<iter->first<<”   ”<<iter->second<<end;' _2 x/ `/ x" u0 i9 f
}
7 ^, X! x( v0 {3 y}7 Z. f; Z7 ~) y2 {; C
第三种:用数组方式插入数据,下面举例说明
+ H+ `, p% _6 n( g( P. @#include <map>
& S, d6 s% v! D+ R" I: M* G#include <string>
  }% \9 O! X# Q) F#include <iostream>
0 i2 X9 W# B' u2 {% R: k" RUsing namespace std;' Q) O* i& V6 I) g) o: F
Int main()
) n) S% z0 w2 {% c) t. o{: ]/ W2 R8 z, W2 S+ i5 q: ]* V
       Map<int, string> mapStudent;
% N; q  C' u( W. c' y3 p/ l: N       mapStudent[1] =  “student_one”;5 K( ]2 B" `, y$ g2 j( Q
       mapStudent[2] = “student_two”;
1 k+ o# }) w6 T: V5 c       mapStudent[3] =  “student_three”;2 Z5 }: n! o# T: H) |
       map<int, string>::iterator iter;3 V$ ^7 f$ \; ~7 Q8 w
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
, }" u4 {4 J2 g& R" |0 J, J{
1 h& c' U" [2 c3 J7 ^  |0 s       Cout<<iter->first<<”   ”<<iter->second<<end;
0 D( w1 z2 e9 @8 P, m, K0 C9 H}
+ a) t0 F+ O. L}
- x+ K  q8 r- v" A# B  y+ }% L) K以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明9 ^, T' b3 P- t! Q  q2 r5 k
mapStudent.insert(map<int, string>::value_type (1, “student_one”));) n0 Y0 ~- w# V, A
mapStudent.insert(map<int, string>::value_type (1, “student_two”));+ z. x% _4 p$ g
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下" c& s0 d9 A' O) b) X) ~  v8 \
Pair<map<int, string>::iterator, bool> Insert_Pair;
( y7 D0 ]# a+ j8 x, sInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
. }( H( u$ S. S. _' Z我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
, @6 k# n: ^9 V2 @4 B" f6 d1 s, ]下面给出完成代码,演示插入成功与否问题& H4 u+ V$ [" p* @5 e, l; o
#include <map>4 Z; L1 u& p3 s( o9 d1 r  U/ x
#include <string>! K  r" Q" _5 t- J4 x! q
#include <iostream>7 i* Y% ?# P- r4 ^& }
Using namespace std;
, t. w- {6 E. Y, b* R' S( cInt main()& F  {1 e1 N: H1 O/ p7 Z
{3 ?' d& u$ o( \! o! L8 Q
       Map<int, string> mapStudent;# Q+ S' W: u7 z, c) {3 J" O. T1 M
Pair<map<int, string>::iterator, bool> Insert_Pair;, X- f+ A& `4 A1 E, U; j
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));+ E) @# P* S( o. S
       If(Insert_Pair.second == true)
6 Y+ A" x8 n0 P" B( E       {
- P6 y+ G  m! o, Y; D              Cout<<”Insert Successfully”<<endl;
# Q0 f& ]8 j9 z, s/ s2 |1 J) a) X       }- q' {  H4 q5 |8 A% X
       Else( ?9 {8 d- {" p; ~# Z, W" c  h3 u
       {& H0 Z% `; V( h# V! \! `9 x
              Cout<<”Insert Failure”<<endl;
' {8 a2 e# B  [8 h' h. D( a/ [       }
: o1 y* R& ~+ k' `7 i* R9 P
该会员没有填写今日想说内容.
回复

使用道具 举报

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

本版积分规则

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