PLM之家PLMHome-工业软件践行者

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

[复制链接]

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

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

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

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

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

x
1.map的构造函数
6 A" o2 x# y0 M. EMap<int, string> mapStudent;
* u0 a5 Q; g* }- p2. 数据的插入  E2 D2 F0 \+ ^  |7 N) S0 \- y
在构造map容器后
5 W" a' O) W3 d2 e第一种:用insert函数插入pair数据' T) G8 q! x, B- P* i
#pragma warning (disable:4786) )- ]4 S. x) r  Z3 p9 o% O2 F
#include <map>
' `* K: ?3 d+ V#include <string>& e. O  t% V" o0 s8 O6 f4 s! N
#include <iostream>
: J  U# B- C  ]" s: kUsing namespace std;8 G; o9 m6 U1 K% S
Int main()# k* V* w' y% O/ d1 ~& d
{
( }3 n2 J: Z2 T2 v$ `       Map<int, string> mapStudent;2 P: J- ?! Z; d7 `4 X7 ~' g  I: z
       mapStudent.insert(pair<int, string>(1, “student_one”));
- {9 B6 t; j. ~7 G, ?       mapStudent.insert(pair<int, string>(2, “student_two”));0 {/ _& A% @8 P- L4 w
       mapStudent.insert(pair<int, string>(3, “student_three”));! }7 S9 Y' M4 H( C4 S2 f
       map<int, string>::iterator iter;$ J, w( f) p6 c3 Y2 N, {$ U4 m
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
7 H5 t4 S( P! _) @- [3 c: Q! O/ m{
" N6 J* I0 L2 X( s0 G: H       Cout<<iter->first<<”   ”<<iter->second<<end;
5 D2 d( y# h1 f4 x}. ~! H: S% |( b6 s; M# G- y& i
}
* V" [( m7 ]; |8 x4 u( R第二种:用insert函数插入value_type数据,下面举例说明$ f# P6 M) o2 ?, U6 L
#include <map>6 V+ F  X7 G; e1 F8 U: j
#include <string>
7 L# q  p) t& r( m7 p1 ]; M( `& n: ]2 p#include <iostream>+ v$ K$ G7 X  ^( E4 k  A9 c
Using namespace std;
* V7 j0 I0 b5 b1 w' e* s* dInt main()* z; J/ Z& I6 V1 z8 D! ^4 |. X
{" |$ E: @* I! s" v2 O3 u
       Map<int, string> mapStudent;: L( F" w0 h; ^# {% Z0 O
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));- L* G% a! l% k. M
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));" I7 ?" k' f7 K! s" {+ @7 F" T- m
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
, E  x: \0 ^1 o& e5 O       map<int, string>::iterator iter;: s) j$ Q3 f9 \5 m
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
: Q; v- N+ z: k  n8 D; ]{
/ o( \: `. T5 R3 H9 P       Cout<<iter->first<<”   ”<<iter->second<<end;% w+ f* n9 k. [- n
}
9 x: R+ |; d8 f4 W6 \% n}- L8 w; P- E+ D! m' F
第三种:用数组方式插入数据,下面举例说明$ |- M0 d3 [- Q( [, y5 e: ~# O' p" ?
#include <map>% B& p" X. D+ v% Q3 B
#include <string>0 H1 B) p" y6 G+ y1 d0 V8 D
#include <iostream>
, O% o- O; `2 x2 b+ bUsing namespace std;
9 M' d; h. R/ C" r& o: `Int main()8 H7 ]- w9 m6 Q+ G: }& @! u9 i
{
9 H1 X+ Q8 O3 _; a, C  U       Map<int, string> mapStudent;
5 G4 @. W; d7 P4 m. \9 a$ I       mapStudent[1] =  “student_one”;, `4 A* ], |) r! l, J. ~" e1 F
       mapStudent[2] = “student_two”;
' {9 L8 U  u& h0 ^  S       mapStudent[3] =  “student_three”;
8 e2 s" \7 M) x( J       map<int, string>::iterator iter;
2 r) [" l+ \4 E' _, O5 ]       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)* Z3 Z5 R! |  X2 L3 N
{' x& W* C- s# d% `$ m. f
       Cout<<iter->first<<”   ”<<iter->second<<end;5 O) U2 L' T# U! f! m4 L2 r3 r: S
}
. W7 r' v- u; G- q) \}
2 B6 D9 @: @+ e以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
) r' b& K7 f% i* g" HmapStudent.insert(map<int, string>::value_type (1, “student_one”));
  g( c$ `/ s! hmapStudent.insert(map<int, string>::value_type (1, “student_two”));
" d3 P# a: a, ~上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下* X+ u* t4 M" v  g/ i6 t# z( v
Pair<map<int, string>::iterator, bool> Insert_Pair;
* u( I+ |) j4 b1 Q% ~5 _Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));+ _7 V8 p- n+ @+ q
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
) l; i1 M) g; e( V) e$ r下面给出完成代码,演示插入成功与否问题4 k! w4 Y- S! X' B, M# u; d4 [) K" @9 K
#include <map>7 q7 v/ P+ y, R* ?! s$ g6 }" @
#include <string>3 z  O9 s( w7 ^7 T8 i2 ]
#include <iostream>
7 r2 X- c) s" T& F( JUsing namespace std;3 `; \& Y# j/ `: x3 H8 K  ?
Int main()9 U* @, R# e  Y+ o8 Y
{
: |7 s/ r2 o5 v# x$ }  c       Map<int, string> mapStudent;- S- j1 O: W8 U9 o# |/ o( o! A
Pair<map<int, string>::iterator, bool> Insert_Pair;6 x$ v# v4 d9 J9 N/ z
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
: Q/ Z5 }+ X9 f5 h2 a3 f( l3 Y+ ?       If(Insert_Pair.second == true); ^9 c3 T% n- q7 N9 T, {2 [& i+ R5 z
       {
: [& s, _6 H% Y. d3 `( m! U  s# P) b              Cout<<”Insert Successfully”<<endl;
$ S0 I* R! _) K( z% k' U5 f       }. D0 I1 `; k  f: Y7 I) J- P' B
       Else' @: W, M# l; ~# k0 P5 v
       {
  S; ^! x% s; K8 F" T  L$ ~! U- M              Cout<<”Insert Failure”<<endl;
$ D8 H4 x9 L' U) ?7 q' C       }
  f/ O# _3 R; L+ W) P2 _+ }: F! U
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了