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 4342 0

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
* `7 W! L2 H, _5 I4 f( IMap<int, string> mapStudent;0 A9 k* l: T; `9 s2 w! `% D* r; L- H$ y
2. 数据的插入
3 |4 e3 D0 d6 `在构造map容器后
( _7 K* W4 q/ t9 E6 n第一种:用insert函数插入pair数据4 j9 ?$ M" H$ _; f
#pragma warning (disable:4786) )
6 a% N" r% |4 V8 |#include <map>
7 Z4 Z" U, W: ^  m. _#include <string>
5 R' l) Q* m$ T: q6 H& o#include <iostream>/ u- Y: }1 o7 f  r
Using namespace std;- s5 v5 t9 U1 ~5 ?2 I
Int main()5 @( s. i6 K$ f; P1 |; A* F" G
{& V2 D9 f: Q% I  R+ o" {
       Map<int, string> mapStudent;% w" W0 F# ~" P
       mapStudent.insert(pair<int, string>(1, “student_one”));
: o  E4 ?0 Q2 y1 S! y# o       mapStudent.insert(pair<int, string>(2, “student_two”));
& C& P% _* n: f% z4 }       mapStudent.insert(pair<int, string>(3, “student_three”));( u5 L0 r! L: n
       map<int, string>::iterator iter;
4 r& t: N/ ]- |' L4 \. Z       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
5 Y! R) n8 ^' f3 Y# a7 f{
. ]# ?& z. R( y8 ], Q* _       Cout<<iter->first<<”   ”<<iter->second<<end;' [5 |! E+ I! F5 N( G
}3 w6 C3 f+ A  _
}
7 C% ^# g  k% E  B3 X第二种:用insert函数插入value_type数据,下面举例说明6 n$ m' t$ s4 ^( \4 A
#include <map>
6 M, G% ?& V# v4 A#include <string>
: ^- F' \  Y) i3 a#include <iostream>
$ S4 x, Y' K5 F7 m6 S4 wUsing namespace std;! C; O' F! [1 G
Int main()
# C6 L2 A' G& ?' `3 s2 ?0 F8 i{
4 h1 |" d* U  Y2 B% s& n       Map<int, string> mapStudent;
5 n- g( a! s2 c$ D  P       mapStudent.insert(map<int, string>::value_type (1, “student_one”));, _4 s$ {1 X$ c, w. q
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
  P4 b9 e2 D9 K5 x' a1 H( Z3 v       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
: D$ i5 g9 g8 C  c6 h" E" Q5 C" u- j# q       map<int, string>::iterator iter;, r; X# P% l. b
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)/ q$ `* p( B! I9 }" P: |+ n. {/ ~
{
! ^" E6 D5 W: V" a       Cout<<iter->first<<”   ”<<iter->second<<end;% k) e+ T2 T: ^" D7 j. @4 j4 z# s% d2 S
}
1 j$ {! ~" h, }}
# e- j0 h9 ]  f. k+ j& n: j第三种:用数组方式插入数据,下面举例说明8 C) X( z5 Y7 b) O. C& X6 I) d3 s& }
#include <map>
$ o: v9 P8 t, s- g( a+ K3 `( q1 y#include <string>/ L9 f( E: ], A% D
#include <iostream>; M, ~! ?2 R# @# Q/ n7 K/ f0 V; ?
Using namespace std;
5 `" R, g$ U# lInt main()
; j+ v/ E% B; r, x( t5 F5 z0 ?/ J{1 L" P  s+ o1 C4 N
       Map<int, string> mapStudent;
2 c% r* I. e( P8 m* A/ p       mapStudent[1] =  “student_one”;1 w% M  f  `# [( ^
       mapStudent[2] = “student_two”;! e3 a& l9 p, ?: b
       mapStudent[3] =  “student_three”;
$ d. ^% I. l5 R% d1 G6 y  U. b9 H       map<int, string>::iterator iter;
/ A: ^' Q/ q+ j* o* r# ^9 {- o: M4 e       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
! N# d7 w" V0 V) O: g4 i& e{' c# Y( B% s0 [- h" O
       Cout<<iter->first<<”   ”<<iter->second<<end;
+ O4 `" |6 |+ Q& Q) p2 u}
# |6 b3 g  x7 x8 w}
# x* V; g  T: e( S2 U8 I. d以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
9 Z7 [; Y; M: G. umapStudent.insert(map<int, string>::value_type (1, “student_one”));$ h/ s  B8 `& Q
mapStudent.insert(map<int, string>::value_type (1, “student_two”));/ s" C9 ^5 z5 \8 @2 \
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
" e' o# z9 m6 I- e# M; U0 ^( z. bPair<map<int, string>::iterator, bool> Insert_Pair;
& P9 T9 _; c1 z" r* l$ g/ `* |5 |Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));8 X/ f( V. m6 C% P7 L
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。4 t/ h: L$ H# U% Y! M4 }
下面给出完成代码,演示插入成功与否问题
! w) P" _6 a+ q" a# K#include <map>
0 t. S0 N' U+ x+ e  X#include <string>
6 p) Q' s- }( K, }#include <iostream>
- J  L4 P4 \. z; V, a1 y. OUsing namespace std;
6 t3 ?$ _' u) m8 ~; }# P* K6 h* \+ TInt main()
1 S4 x' b5 g: a0 I1 r6 n{! I0 h  z! ]1 d6 x
       Map<int, string> mapStudent;2 W* ?* v0 D% t9 u. _
Pair<map<int, string>::iterator, bool> Insert_Pair;
' D- q+ M% l; }& }( n8 D       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));4 W( u7 u5 ^0 @; |
       If(Insert_Pair.second == true)9 B; g! l6 w0 l* H
       {) F/ c% p) j/ g( m" L% |+ J" p
              Cout<<”Insert Successfully”<<endl;
4 ^; ~+ g% l8 R- D0 S- o$ T       }
7 W, E) {9 a/ p       Else
) M- D, R8 t4 Q: ~! p1 V1 z) `- A       {
* ^$ V, {; r0 \              Cout<<”Insert Failure”<<endl;8 U( k/ b, Y* o
       }7 v0 s" f/ A+ L6 s! A# D: h
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了