PLM之家精品课程培训,联系电话:18301858168 QQ: 939801026

  • NX二次开培训

    NX二次开培训

    适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术对于老鸟也值得借鉴!.

    NX CAM二次开发培训报名 NX二次开发基础培训报名
  • PLM之家Catia CAA二次开发培训

    Catia二次开发培训

    Catia二次开发的市场大,这方面开发人才少,难度大。所以只要你掌握了开发,那么潜力巨大,随着时间的积累,你必将有所用武之地!

  • PLM之Teamcenter最佳学习方案

    Teamcenter培训

    用户应用基础培训,管理员基础培训,管理员高级培训,二次开发培训应有尽有,只要你感兴趣肯学习,专业多年经验大师级打造!

  • PLM之Tecnomatix制造领域培训

    Tecnomatix培训

    想了解制造领域数字化吗?想了解工厂,生产线设计吗?数字化双胞胎,工业4.0吗?我们的课程虚位以待!

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

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
8 S  u% m2 r3 T1 W5 d* Q0 zMap<int, string> mapStudent;+ K5 k3 ^4 u: M
2. 数据的插入. A( N% ^. J+ I6 m/ [0 R
在构造map容器后3 J# D0 ?5 z9 O3 v* q+ o9 b
第一种:用insert函数插入pair数据
- h2 B8 g7 P4 j: g2 x- ^9 ?#pragma warning (disable:4786) )! V$ F! q& u/ f5 b4 P
#include <map>) S& k4 ~/ u) c* n- z0 ?
#include <string>
% Y, [6 p0 w+ I' R" C9 Z$ g#include <iostream>0 D1 s. S6 m- _9 ?
Using namespace std;3 l! E) x+ l7 G- B
Int main()5 I; @$ |3 K; t" [: [) ^
{9 O3 m$ L0 b- G2 {  V
       Map<int, string> mapStudent;
9 r) t, X8 V( N, v2 e: H% J       mapStudent.insert(pair<int, string>(1, “student_one”));
; y- B, @3 e) K: }7 s       mapStudent.insert(pair<int, string>(2, “student_two”));
: d* x5 {5 f' J+ A: s* g: J       mapStudent.insert(pair<int, string>(3, “student_three”));. j! n8 ]  v5 r8 [! X" i# z# B
       map<int, string>::iterator iter;
- f; U) B+ E/ _+ r% J/ k       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
6 B0 {; q: E7 a' I' ?{
; T4 Q7 G( G+ ?       Cout<<iter->first<<”   ”<<iter->second<<end;
6 J7 l. N! i0 }0 J5 g- }! Y}
- U1 }2 J/ @; K; _8 R}4 h) [, q/ x) b
第二种:用insert函数插入value_type数据,下面举例说明8 q: a9 T$ k* [+ h3 v+ x
#include <map>! Y+ \. \# @2 g" m; {" S) H, o
#include <string>1 j3 I6 e( h: P4 O
#include <iostream>9 M8 `. s+ o$ T; _' Z2 u
Using namespace std;
7 O4 [, v1 s5 k% K# A9 q1 X  ^Int main()
! }$ ^3 l  a" {8 x/ {{
, E+ b, p. r. A0 x$ l       Map<int, string> mapStudent;
1 e0 Y- f( d' t+ b/ Y3 r! t       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
; P4 h! ?9 x# _2 E: P       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
" D$ n9 w# l; @$ h5 L       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
6 }6 u$ z- _; j2 o/ m" m       map<int, string>::iterator iter;1 g6 @, N) A; X2 z
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
/ m% k' N6 k- _1 K$ b! _, N" J{
8 {  R. w& Q! p       Cout<<iter->first<<”   ”<<iter->second<<end;6 }" F( z* i, Q. W( N7 Q
}
! L5 `8 M- a' n6 S% F}
, ~: k# D0 Z* h6 e8 r第三种:用数组方式插入数据,下面举例说明1 @3 \2 C- y$ r2 g. l! d
#include <map>. y  ^2 v% [9 O* R- h9 i5 M- `
#include <string>9 M$ a, J- P! ?4 O4 e/ m8 X
#include <iostream>
! c" l/ x2 m1 k0 i2 Y/ W3 {' Y" JUsing namespace std;
- N2 g% C4 u' W. r7 QInt main()' z2 t2 r  ]  t5 f8 @
{
! d$ S( T6 k8 E& X# B       Map<int, string> mapStudent;
* Y! Z$ `+ v% z7 e& v5 Y       mapStudent[1] =  “student_one”;: S) m* C1 f0 T4 m) r
       mapStudent[2] = “student_two”;
7 }% M9 i1 B% n) |       mapStudent[3] =  “student_three”;) x5 Q; |) n/ R/ }0 Y4 f
       map<int, string>::iterator iter;
3 k3 L  }% ?% A- W/ s1 ?' z& I, g       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)( {9 }$ o, j' ?- [0 d* l
{
9 D, i( o+ K6 l( f* v  i% j       Cout<<iter->first<<”   ”<<iter->second<<end;1 r, Q" g$ M% t1 p
}
4 G) Q7 a7 D) `, w$ O  N. F}4 s$ m2 A4 m( t1 c" o6 v
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
# P- b% H! I% {% q$ CmapStudent.insert(map<int, string>::value_type (1, “student_one”));% c8 w: O1 ?5 U+ ^2 N) R/ v3 Q
mapStudent.insert(map<int, string>::value_type (1, “student_two”));0 @/ B7 c$ |% T4 X
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下: o( M5 [' C! T) j6 W
Pair<map<int, string>::iterator, bool> Insert_Pair;
8 ?1 J0 X1 d- V1 u; G; T1 bInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));& Y3 c& o, P2 }
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。' j+ m" x3 ~) K. G5 @
下面给出完成代码,演示插入成功与否问题
6 d2 ~" ~5 l4 Q& F5 f#include <map>) h1 u9 ^: F+ _' w+ W( t' c  r
#include <string>5 Z# Q2 m" a( H1 e4 P
#include <iostream>
4 `! T% B7 c* [0 t3 Q. zUsing namespace std;3 x! Z% w/ n1 O: Y1 q( E" W
Int main(); i! W% g* Z6 l3 X' y/ r& I9 k% {
{
- B+ d9 j0 W9 s9 C3 Y1 y       Map<int, string> mapStudent;; h; G( f4 M& c* k- w7 `/ [7 z
Pair<map<int, string>::iterator, bool> Insert_Pair;- ^: Q$ I$ t8 O! T
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
+ O1 p: X- t) s9 O3 F, B       If(Insert_Pair.second == true)
2 a3 ~5 K7 T0 d       {7 I2 H& W2 i0 @9 b" Q( p
              Cout<<”Insert Successfully”<<endl;7 B% ?5 y8 ~3 Z1 m3 x
       }
9 O5 t  A) F" d0 m       Else
/ i/ q8 p( t; `+ \       {$ P- {9 y! J/ ]  H% r1 O) T% ^
              Cout<<”Insert Failure”<<endl;
! o0 r1 |+ _/ S& o& R3 Q       }2 Q- x7 C8 z% O8 ?
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了