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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
1 ~4 M8 u4 w8 O1 S. {* F& wMap<int, string> mapStudent;
5 X/ E; k) n# ?3 a/ o/ n2. 数据的插入0 n6 x. B# U7 i
在构造map容器后, o! d) u+ v8 ]9 M2 W
第一种:用insert函数插入pair数据* Y- w- D+ c/ E+ C
#pragma warning (disable:4786) ), }4 ?& ^8 n5 d5 u8 ~& w
#include <map>
; l! D7 n0 Y2 r. Y- z& T7 j#include <string>
8 n# {4 V5 E' R8 i. K3 P! I6 J#include <iostream>$ _: D# M6 L0 E' R
Using namespace std;2 D) z( u7 S3 B+ Q3 Q
Int main(). s% K7 G$ c4 c9 y) X% v! k0 x
{
8 t$ R+ Y. y  t5 l       Map<int, string> mapStudent;
9 G' T( i( R4 l/ S$ U0 e7 n& b       mapStudent.insert(pair<int, string>(1, “student_one”));/ x9 e0 x$ F6 K
       mapStudent.insert(pair<int, string>(2, “student_two”));" u/ U" a% F6 L6 w& g
       mapStudent.insert(pair<int, string>(3, “student_three”));3 [% o' f' _+ z4 ?( [# P
       map<int, string>::iterator iter;; I; u  n8 E/ s8 ]4 }+ M3 ^" f
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)) X" a. w" G$ k( v% B
{8 K: V6 R, f+ G) `) {- J8 x
       Cout<<iter->first<<”   ”<<iter->second<<end;$ s; d; g* A- B3 M0 S' M
}: X' U* p& s, ^) x2 a2 A( k* M) }; q" O
}
; v' z, M0 \! A8 F1 ^第二种:用insert函数插入value_type数据,下面举例说明
: p/ {( H; z% Z/ q, q  n) |#include <map>( X$ a, y# d; c7 N8 ^
#include <string>$ _8 M. L; R1 P5 p5 O% E6 A
#include <iostream>& H- ^  i$ N/ N* o# l3 V0 q
Using namespace std;. a8 [" f- F. W7 T) _- [; \
Int main()
3 S: L7 s! q: c0 L$ ?9 |2 K{
0 ?0 W; A& I/ U$ L# P       Map<int, string> mapStudent;0 i9 c) x/ D& z* A
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));, z( \. f4 x9 j& p) j0 T
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
! y2 x$ G4 _4 ]% ^1 I! Y; y5 w       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
2 f0 C; k/ A9 `3 d/ H       map<int, string>::iterator iter;
# Y. @- F# o) ~- M' D6 V       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++): g( A  O9 }9 H; W# D0 L5 ~# w
{: O& t$ ?3 Z; o4 `/ P# i& O: A
       Cout<<iter->first<<”   ”<<iter->second<<end;
% ]0 K$ X$ D) h1 r}
5 u8 W( h/ a0 f. b" {}
) Z- j8 |3 j" S0 I: F0 K# e第三种:用数组方式插入数据,下面举例说明: C6 B4 F- r2 k: D
#include <map>
3 _8 T$ s8 @5 U7 X1 ~8 L#include <string>
, k' S! B- ^9 S- s" {#include <iostream>
! @5 X8 t/ B  n) B; S/ ?Using namespace std;
) w- m4 @9 g9 m" ~* oInt main(), _  b0 {" O8 J- Z, k% p
{
3 O- @: }3 e$ H2 h* M       Map<int, string> mapStudent;1 E/ Y7 o& |: n8 E
       mapStudent[1] =  “student_one”;" X3 u' `& ~3 s1 N
       mapStudent[2] = “student_two”;- l" F3 Z: U% x8 l. [# ?
       mapStudent[3] =  “student_three”;
; I3 |- w( i* i; {, Z       map<int, string>::iterator iter;2 j- c( N( c% w4 m  E8 l% L6 i, E. [
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)/ t0 K# O9 O  b5 t
{2 s: l" B% m; [& B2 S
       Cout<<iter->first<<”   ”<<iter->second<<end;3 z; ]9 E* E) y/ q
}
/ ~3 b& T4 E8 B( Y9 {- Z}' p! U* s5 O( B2 s8 R( A1 x
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
  q+ g- K  t/ s& |; _mapStudent.insert(map<int, string>::value_type (1, “student_one”));
; z7 ~- A9 ]" k% ]3 ?% ]$ f: N* DmapStudent.insert(map<int, string>::value_type (1, “student_two”));
" K, A6 H& x4 |1 h* y$ U& w上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
9 K5 a6 s$ ^: r% M3 H8 }! h0 RPair<map<int, string>::iterator, bool> Insert_Pair;1 d2 L- F0 e; Y9 p8 U7 X
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));; {/ m8 @$ a. B* Z' c7 a8 f
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。2 {* t/ Z# q( e) p3 C; W" C
下面给出完成代码,演示插入成功与否问题4 U5 a3 R) f1 x+ f, \* t' Q+ J
#include <map>
5 [  b7 y' b% n0 Y# k$ O#include <string>8 J$ Z9 d. z/ n# T! P6 B% r
#include <iostream>" a5 y& V* D6 v: W% i
Using namespace std;
% \5 m  \! X! Q4 j. n0 iInt main()" Y# Z* }0 e7 c; ~  p
{: ]; A# x* s! c# N' s7 V
       Map<int, string> mapStudent;+ I  r- N, ^2 P# s# e
Pair<map<int, string>::iterator, bool> Insert_Pair;6 B* V) h/ t2 n0 G( d! l$ `" ~
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));0 h* F  t  n* y! T8 ^
       If(Insert_Pair.second == true)3 O7 L7 m9 j3 ~6 b6 ]8 r" t$ X- x
       {% }6 p6 e. Y3 _
              Cout<<”Insert Successfully”<<endl;+ ]% q0 v3 R0 Z+ P$ H
       }4 |4 ?  G" X, P8 V6 t  M6 i
       Else
! L4 W: Y. F4 B) m       {
2 [. w5 C0 {% d- Y4 N" H              Cout<<”Insert Failure”<<endl;+ `0 q/ t9 y# I2 }$ X7 y4 E
       }
) E" R$ Y0 _$ S' J$ U( ^! J* P) x
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了