PLM之家精品课程培训

PLM之家精品课程培训

联系电话:18301858168   |   QQ咨询:939801026
NX二次开发培训

NX二次开发培训

UFUN/NXOpen C++和实战案例

适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术。
公众号二维码

关注公众号

点击扫描二维码免费在线高清教程

课程详情
Catia二次开发培训

Catia二次开发培训

市场需求大,掌握核心技术前景广阔

Catia二次开发的市场需求大,人才稀缺。掌握开发技能潜力巨大,随着经验积累将在汽车、航空等领域有所作为。
B站二维码

在线原创B站视频

点击关注工业软件传道士主页

课程详情
Teamcenter培训

Teamcenter培训

全方位培训,从基础应用到高级开发全覆盖

涵盖用户应用基础培训、管理员基础培训、管理员高级培训及二次开发培训等全方位内容,由多年经验讲师打造。
QQ群二维码

加入同行交流

点击扫描二维码加入QQ群

课程详情
×

PLM之家plmhome公众号

课程涵盖: PLM之家所有原创视频

×

关注B站视频

所有高清视频一览无余,全部在线播放学习

×

加入PLM之家QQ群

同行交流,疑问解答,更多互助

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

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
7 a8 e" a" @. A7 u/ n* s+ hMap<int, string> mapStudent;
5 ^! P; G1 T9 T, `: ^! C9 `+ P$ C+ `2. 数据的插入
  l; k. h! _' _; O在构造map容器后
: X$ |8 d+ k# I  Y/ F第一种:用insert函数插入pair数据: e/ Y8 F2 S3 C4 f
#pragma warning (disable:4786) )" j1 n4 u/ a+ L& _- `( U
#include <map>' d. B4 p7 ~; S6 H) M
#include <string>. ]3 q* s) b5 k7 P
#include <iostream>
. h$ |' v/ T, C$ k3 TUsing namespace std;8 P* K% s  C" L& _3 B) J
Int main()
( {) v: o% @8 ?: I  W{
: ^; u; x& ]& \1 j# F1 c' {& [  W       Map<int, string> mapStudent;- I) F2 g9 H0 q+ O- a
       mapStudent.insert(pair<int, string>(1, “student_one”));: X6 B$ }1 }$ W0 M( e' _4 k( G
       mapStudent.insert(pair<int, string>(2, “student_two”));
: F) d4 w0 R. `& [' e       mapStudent.insert(pair<int, string>(3, “student_three”));
$ n5 i: N* p3 ?' Y       map<int, string>::iterator iter;$ x, c: k+ \8 D; M
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
6 X4 S& K6 T6 m& _+ K{
7 q, O  k5 }) c! |* U       Cout<<iter->first<<”   ”<<iter->second<<end;
% t) [3 M2 N4 ~- Z/ a( y& }8 |}
; |/ b- A! f0 ]: r( n}# H2 N+ h+ V5 C, \) s8 d
第二种:用insert函数插入value_type数据,下面举例说明
7 U0 _/ h$ Z* ]% ^( }. V9 L+ g7 Y#include <map>
4 g: M/ P6 o3 z#include <string>5 ]- P' a9 }$ a( H; S: }
#include <iostream>$ n6 v2 H; c* i0 q+ j0 x, U% B
Using namespace std;
) e+ g) S# ^' m. b( F- S" DInt main()
( o+ [5 d' a& {7 o6 ~8 }% M{
, G1 G, Q5 x& j0 J       Map<int, string> mapStudent;
: D4 O1 W  `/ i       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( z2 N3 F4 w8 F3 k& O! C       mapStudent.insert(map<int, string>::value_type (2, “student_two”));% t( ^8 j' x- [4 ^, e! A
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
5 O" o6 Y; K  k3 R; Y8 x       map<int, string>::iterator iter;
8 K' l" g% z) u       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++). u9 {  {  Z& O6 B/ q
{
8 Q- [* v5 l+ K9 j       Cout<<iter->first<<”   ”<<iter->second<<end;7 t! P8 n! d7 R% M0 X
}; ?& J/ }" m- p. n7 y
}
2 H/ ^* v, X# a$ m第三种:用数组方式插入数据,下面举例说明% ^7 V# m7 b% ?4 N2 ?1 ]8 `2 s
#include <map>- \- l# q. s' T% F, a% [
#include <string>2 K, Y5 D# ?) ?& x
#include <iostream>
, g" r9 H* N' G* y2 eUsing namespace std;
2 b( a2 s. N8 Q% m4 N0 N0 o2 dInt main()1 v) o: [2 v' r2 S8 c" s/ B
{
, w9 F& B" W# a: i       Map<int, string> mapStudent;( N5 \4 C" m% \- U* \0 z+ Z+ \' g
       mapStudent[1] =  “student_one”;1 a% [7 `' H: d1 J- l
       mapStudent[2] = “student_two”;; ^% I/ u* C0 J6 z
       mapStudent[3] =  “student_three”;
! Q' c2 S! d( S( B4 y       map<int, string>::iterator iter;
8 v( m9 S8 p+ C: ~" i4 u       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)' O+ }2 J7 u% ^, {" a+ h4 v
{
  e6 E/ q$ T' m) m: p       Cout<<iter->first<<”   ”<<iter->second<<end;; Q& r- W; n" l
}
+ y( A# ^+ ?1 E8 e  U}
" l( ^, U0 S" Q' ~( [以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明/ x: {" `) Z$ [: U
mapStudent.insert(map<int, string>::value_type (1, “student_one”));) T0 w+ g4 v& V1 D/ {0 T6 l% |
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
% f; |% z& w; l' Y, o' a上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
, c& \) R. f2 sPair<map<int, string>::iterator, bool> Insert_Pair;" M, y5 [0 T. Q+ L
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));0 L: Y. v5 W$ N2 l: S
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。! \, Y: r9 _' z3 A$ b
下面给出完成代码,演示插入成功与否问题) O/ A  o9 ]2 P2 M
#include <map>
6 W, T: `  r' P# ]* I! X- b' Z#include <string>
, M3 @5 `% f2 B0 l, J0 T; a1 p#include <iostream>0 Z, o, @# k1 n
Using namespace std;( G  `3 M  G' ?* b* ^1 _
Int main()+ x) F) E. k  o) d1 F( b
{8 `1 o1 u) M! o  C' R  _: {7 c
       Map<int, string> mapStudent;. V, `; E3 q# C. U& `8 s) X
Pair<map<int, string>::iterator, bool> Insert_Pair;
  F, z. U$ D8 m. I( p2 H4 q       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
8 Q1 N* }. z3 ?$ V& G- |2 X$ R# L       If(Insert_Pair.second == true)# _% Q( h* K1 d4 {; v
       {
2 g: b$ T( h; z+ q# x+ O5 p              Cout<<”Insert Successfully”<<endl;7 g- p# \( n/ F) l! Q% C. X
       }/ Y  p+ Z' ^1 D5 B- d" @
       Else
- U9 \7 b' d9 K! ]2 p7 ~3 a% v1 H       {& A* b9 y/ }6 u. Y
              Cout<<”Insert Failure”<<endl;) ^8 T1 S- f) W4 S; Y
       }
# w; |( a2 a) c. U; a
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了