PLM之家PLMHome-工业软件与AI结合践行者

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
% K' w* u0 y$ t' ~9 A' j9 FMap<int, string> mapStudent;6 m% m( a* X2 k- m5 s9 ]
2. 数据的插入
  g1 \: Z& v& j# E" ~在构造map容器后
) h; C5 X  {  m: G5 Q+ w第一种:用insert函数插入pair数据6 T6 W% e& C8 r$ c9 L' p  _
#pragma warning (disable:4786) )
6 S* b  J& \% U) k  @#include <map>8 D' A$ B1 e4 A" b
#include <string>
0 p8 M  b3 C1 T#include <iostream>
: _8 C/ l4 y6 P" |1 }: JUsing namespace std;
4 B1 @( f9 i! s3 TInt main()
  g$ h# l+ w: x7 k( q2 ~* u6 k{3 x* _0 C5 |2 W) @$ ]* l) W9 w
       Map<int, string> mapStudent;
( P' C3 R% m" M1 Z' q. r       mapStudent.insert(pair<int, string>(1, “student_one”));. |, z& j) x% \* N5 o6 u
       mapStudent.insert(pair<int, string>(2, “student_two”));
: ?! ~: B4 f" e; Z0 o       mapStudent.insert(pair<int, string>(3, “student_three”));
  C4 e, L+ i; b: v' y       map<int, string>::iterator iter;
! X8 Q4 q4 {4 t; j3 z$ Q       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
  F: W( Z" l# J( x" P# ~* w6 H{7 [9 s% E3 ]* Q
       Cout<<iter->first<<”   ”<<iter->second<<end;( u. r  H/ z3 h
}
# ]2 J: u) u4 Q7 e}
+ S+ p8 Z9 z. Z, a. a) V6 n+ O! E* x第二种:用insert函数插入value_type数据,下面举例说明' T1 P% O+ m+ }, n6 D, S
#include <map>" m% [* j. E) ~: `- G+ Q
#include <string>3 O9 L1 @5 D, {
#include <iostream>" B6 ^) X; I- U6 v; r4 L1 a
Using namespace std;/ J4 R0 I) p6 i  a* X  P
Int main(): r' Z$ n2 V) ^# ?
{9 Y5 }- T$ e, b9 h  B
       Map<int, string> mapStudent;
$ z0 \9 {! E( O8 p9 X- o. Y       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
5 n. b/ V. E- q( s: o* {3 }       mapStudent.insert(map<int, string>::value_type (2, “student_two”));2 A/ U! |! S& Q: [8 W- n1 T
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));0 h& |& E; d8 |4 n& ~- L
       map<int, string>::iterator iter;
) ~& ]: ^) X1 J3 L# r9 ^       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)/ X& m5 J# j& k3 B% k
{+ e7 Q8 f( M+ w0 {
       Cout<<iter->first<<”   ”<<iter->second<<end;
- ^) S$ L% N$ g4 R$ y2 l}( K2 ~- ?( V8 z' _* B# X
}4 {8 {3 i$ `" O1 r( p5 }0 n5 v7 K
第三种:用数组方式插入数据,下面举例说明
) F- S" u( i$ t1 k; \#include <map>
& s, W+ z: d& s#include <string>5 l7 D" G0 I" S7 L3 J/ \. E
#include <iostream>
' u- H4 _2 I% i; V+ C' Z/ sUsing namespace std;7 i; }4 }2 k" N# Y
Int main()
( u4 ^% {$ b- r( o{1 ]: s6 n! u8 F6 m
       Map<int, string> mapStudent;8 l0 F. {5 `* f* ^% Z7 x
       mapStudent[1] =  “student_one”;5 n: s1 U3 `0 ^1 |3 Q" V
       mapStudent[2] = “student_two”;
0 w' u! i: F8 E5 A; f       mapStudent[3] =  “student_three”;
$ G3 s" g" r2 P2 z  ^; K) E       map<int, string>::iterator iter;1 x# l  r% V" ]- T! e
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)7 u) c) ?4 o& }
{
) X1 ]1 J; P. s* n' v/ ^# J       Cout<<iter->first<<”   ”<<iter->second<<end;
! K( X- w7 @& L( Z# C: h  `# F* o+ Q}2 g8 m6 U8 e0 U- N: V
}3 y0 A, p1 m3 M
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明; F$ }7 @) u6 C+ W' `
mapStudent.insert(map<int, string>::value_type (1, “student_one”));; \$ d# R* t$ w6 F" u
mapStudent.insert(map<int, string>::value_type (1, “student_two”));2 F5 i$ V1 k- l8 r; U& q
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下4 `9 G9 O. H1 i0 L6 @1 C
Pair<map<int, string>::iterator, bool> Insert_Pair;  T& X$ R+ ]0 n3 l) \. {
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));3 X# ~3 Z) k9 V- C% n* D
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。" |8 m) W$ w2 N9 t
下面给出完成代码,演示插入成功与否问题
9 t% `0 p/ I4 P0 M& ~7 m#include <map>
/ s6 z. r- U; w( e+ x#include <string>- G6 q+ c6 x. l6 Z9 Q" P
#include <iostream>
" u8 u8 W/ O" w9 }" [/ x3 r) T; fUsing namespace std;% `+ d4 x$ Q" i' d
Int main()6 F7 t9 i( o9 b" l+ t, [' i' v& N
{4 R0 C$ r" T  `9 p( f' n
       Map<int, string> mapStudent;
7 f& z  C& L% ?2 ?: rPair<map<int, string>::iterator, bool> Insert_Pair;
4 A( O1 h: b" ]- `6 f       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));4 k3 q1 y! A6 x$ G. e7 Z: ]
       If(Insert_Pair.second == true)8 H6 F+ {* b" {4 c
       {1 V4 C5 q1 \: s& f# z/ W3 }7 b4 v+ i
              Cout<<”Insert Successfully”<<endl;  K; B$ M4 W' K
       }
. A0 z! P; v1 V" D0 L       Else
: Z: b* Z7 g  @3 A$ N2 A       {; b9 a7 f, x7 b, z
              Cout<<”Insert Failure”<<endl;
) |$ W& I8 ?, {( n, H: s       }; x8 K2 a. r& H! j, z
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了