PLM之家PLMHome-国产软件践行者

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

[复制链接]

2014-1-3 19:37:45 4468 0

mildcat 发表于 2014-1-3 19:37:45 |阅读模式

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
5 s0 U+ f; M: v  ~Map<int, string> mapStudent;/ M$ h9 F( L( s) m9 {# o. B5 l0 n
2. 数据的插入" z; ~- Y! |, o) V. y1 P
在构造map容器后
' W6 G, O6 A) A+ X1 A第一种:用insert函数插入pair数据; P1 s0 r) z  K' B3 F! y6 Z- Y$ c1 r- V
#pragma warning (disable:4786) )) \0 ?7 B8 B$ y/ d8 g$ v( X
#include <map>3 U. P# M: U( p7 P* H0 w2 _+ f3 M, ]! |
#include <string>+ ?; B& b/ T6 T2 i5 k( o
#include <iostream>5 C' J, [# B  [$ J8 A
Using namespace std;
, f; t: n/ w/ D7 y( X  zInt main()  P( M/ {7 C' m  B8 |
{
) F) `- }. z/ W# ]       Map<int, string> mapStudent;
9 g1 m& C8 e2 H# h9 a       mapStudent.insert(pair<int, string>(1, “student_one”));
. A5 c0 v. g  U/ y/ q       mapStudent.insert(pair<int, string>(2, “student_two”));/ G8 p1 \. n3 l4 f9 Z4 A
       mapStudent.insert(pair<int, string>(3, “student_three”));
& O3 D: l* h+ I1 |2 C$ D       map<int, string>::iterator iter;
8 i5 ?. t( R$ J2 E* @  I1 j+ v       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)2 N- s. {; x) P! `: U' Z1 F0 @8 ~
{
( v# D/ X1 C/ D; E       Cout<<iter->first<<”   ”<<iter->second<<end;- `; S. w4 R+ I6 ~( u$ K+ C
}
- @* _4 C! @: f) k}
8 M6 P* L9 D# ~7 }* Q3 M* f, E第二种:用insert函数插入value_type数据,下面举例说明
% W6 e( s4 B  f, {#include <map>9 T9 ~0 M* u6 j
#include <string>4 m5 U/ T$ ~( f6 x( k  V) O4 t
#include <iostream>" G5 X4 Y9 O4 w( o. p+ M
Using namespace std;' ]+ w" [: k7 I1 H/ G
Int main()
2 y; X0 {/ V. R4 d. p{2 w+ I' x7 T. ^. @6 u- }
       Map<int, string> mapStudent;, i2 d7 ?1 i2 p" Z8 o
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));& \6 j8 h0 j! d  w& A! A) s
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));* y9 A6 J* i9 p- J0 @/ d/ a' G
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
( u  G: d/ b; Q       map<int, string>::iterator iter;0 I1 e$ Q& D& n  g" q; z# `" Y* O: X
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
5 i, a3 T  f/ @$ R" Q{" B0 D# N& J) a+ H# i
       Cout<<iter->first<<”   ”<<iter->second<<end;
& m% q+ O' x# m- |9 z% G}
" c2 ?2 Y# B0 \3 H8 k/ t}" y4 Q4 Z$ W, X# w7 c7 `( p
第三种:用数组方式插入数据,下面举例说明1 m6 P2 u8 F6 b" f+ d- u
#include <map>
. N  _0 U- Y2 |; {: C#include <string>
& i4 V. D6 Y+ g3 l1 x#include <iostream>, B! J1 N* @. s# ?7 d3 p
Using namespace std;3 b% T$ O8 d: ]- E1 D4 X/ h+ x
Int main()
" d( v1 e1 p4 c) f1 t& u{
( K1 I: k- ]: {6 c, [       Map<int, string> mapStudent;
% [& O3 a5 D) e. y       mapStudent[1] =  “student_one”;
' k( [+ B' z2 _! c       mapStudent[2] = “student_two”;- i/ I% L; Z' ^$ {
       mapStudent[3] =  “student_three”;
0 M2 M4 A2 t! ^8 i       map<int, string>::iterator iter;0 [8 Q( t) c6 {1 d/ A
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
+ D) D2 S& \# q; Q! N6 Q6 Y& ^{% p4 T+ r/ M! ^* ?( K1 v
       Cout<<iter->first<<”   ”<<iter->second<<end;
) p. W2 @; @9 a& h4 C) k( |}
# }6 V3 ^2 C  V+ q; Z}* \- \: }# m& R
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明6 {" u9 {; k( Z
mapStudent.insert(map<int, string>::value_type (1, “student_one”));+ M0 X. i# p1 |3 ?$ K
mapStudent.insert(map<int, string>::value_type (1, “student_two”));2 s1 C1 T7 ^1 y& I& \
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
+ |1 a6 `  h  e1 b* V: _. E: `! UPair<map<int, string>::iterator, bool> Insert_Pair;+ Q( B) z! A/ D# f
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));2 V3 p+ s6 k* F2 E$ H
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。) p! E2 k% T% P5 n0 R, t
下面给出完成代码,演示插入成功与否问题
9 u6 y' }5 n, ?! e, S$ x! R#include <map>6 h  H/ D# P5 B2 S# s
#include <string>
% s6 j# L2 W. |# R; u+ U8 U#include <iostream>
5 ^5 j; U& z' ?) r+ zUsing namespace std;. t2 T& W& ?+ p8 p; N/ N$ R' Z
Int main(), h. Z9 u/ [; c2 D; I) p
{) t& y2 _. @; K. O8 O7 y; I
       Map<int, string> mapStudent;
2 s2 a6 @. E4 c4 V. ^$ B4 i1 y9 \; CPair<map<int, string>::iterator, bool> Insert_Pair;
- F% @  V2 z, _2 k       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
' K: t' x" k5 |5 V1 M7 F       If(Insert_Pair.second == true)3 g/ V- s" |) i5 E$ z. z- I
       {* f- K4 e3 i8 @+ e$ |, B
              Cout<<”Insert Successfully”<<endl;, a, r8 M$ j, x- s8 P* s
       }
- K5 O3 K: d. m& u$ X       Else
1 f; K  }9 h# J' h       {0 t& e' @; L% I) S. R
              Cout<<”Insert Failure”<<endl;. G5 e) H' l. {9 R' U+ a- c7 ?* @
       }
# _- V6 s( m  S" O( o) B' N
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了