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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
3 c" |% |' f5 R7 c$ \# |) eMap<int, string> mapStudent;2 N6 v! t9 |1 ^
2. 数据的插入  K. W; D( e9 w% }7 C# o5 d
在构造map容器后
( h1 x6 B( M4 ^# p' l2 |7 k第一种:用insert函数插入pair数据
6 v# R9 n$ G6 |2 H6 r9 J$ I#pragma warning (disable:4786) )% n0 l. v+ W0 P- b
#include <map>
+ D# u: o5 O% G0 x4 W3 v#include <string>1 v$ s& L8 r4 @4 V4 ~! y, H' K
#include <iostream>" T3 ]- a6 l8 H
Using namespace std;
- G3 I4 i/ e. q0 ~* _Int main()
2 P6 n0 i4 E* D3 {{( u7 I6 u% T' o! Q/ I* Y+ b! ~5 P9 n
       Map<int, string> mapStudent;
3 Z5 _3 Y  _" ]$ u       mapStudent.insert(pair<int, string>(1, “student_one”));* f: L" R/ u8 {  J2 H; q
       mapStudent.insert(pair<int, string>(2, “student_two”));8 B' @2 r  H. j
       mapStudent.insert(pair<int, string>(3, “student_three”));/ n% h% q; y% t( |" D$ X
       map<int, string>::iterator iter;
  [2 ~, S& J) |# m& ~       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
; W, R8 t7 N  m/ {( F* |{
1 X& `+ H! w; W. M5 T       Cout<<iter->first<<”   ”<<iter->second<<end;
% e! O: o$ {( s/ M* c6 u8 l0 r}
* G3 B9 G* U! ^% M8 K1 ^; T% r: ]}! j% w: R1 @  U! s$ X  Z4 _8 K! E
第二种:用insert函数插入value_type数据,下面举例说明1 i, o$ m7 j$ K  A# Y, j: W4 u
#include <map>% P% S( F1 e2 i# S( U
#include <string>
& Z  o& w3 u2 B  R/ o3 x& E#include <iostream>
" p8 U/ S4 T" H/ F: W1 J1 SUsing namespace std;
. N' M* \5 k* r( u  M. U! J- R$ K! U0 H& ]Int main(), E9 c7 X2 f0 s) \
{3 R- R( |, ~% x9 g0 m  H* s& O' B
       Map<int, string> mapStudent;
: L# @" S- F+ [8 m8 n$ J       mapStudent.insert(map<int, string>::value_type (1, “student_one”));( \/ R' W, w/ l) j7 g( n
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
9 r0 W3 J) c( K% d       mapStudent.insert(map<int, string>::value_type (3, “student_three”));2 a1 J- c9 B, C# M$ Z) H. }. L
       map<int, string>::iterator iter;
4 y! Z7 O3 d8 q% ~" f       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
4 k! d+ F" N5 D5 V$ W{* e0 k" L4 X! F" i
       Cout<<iter->first<<”   ”<<iter->second<<end;
8 z3 \3 Z  m7 U" U: o9 x  P}: Y/ R3 L6 Z" q- s! r$ L) L
}0 d3 k* M4 m* c( v$ W; C$ z( i
第三种:用数组方式插入数据,下面举例说明
/ ?. F# e3 `* |$ b7 M# y#include <map>
9 C1 T1 [+ S  q0 z+ T#include <string>0 \' U" H+ Q1 H! F! ]: \" J
#include <iostream>
/ t8 p. B, p8 t- M/ ZUsing namespace std;
# |3 a  y: D1 y3 Q; Z$ E$ U7 ~  UInt main()
$ |* V6 d1 U# S, @{
( X* o$ Z3 y6 @5 D/ Y) H       Map<int, string> mapStudent;
1 `5 R) ^* o. ]1 u+ L       mapStudent[1] =  “student_one”;
7 Q" x, B2 x: B2 D0 q( F0 J! S) ?2 j       mapStudent[2] = “student_two”;
9 W/ [+ u% f6 a. ]* v       mapStudent[3] =  “student_three”;
6 G+ k3 d) q8 B1 a. x       map<int, string>::iterator iter;; v& K5 ~2 y. h7 `$ d, z7 ~
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)+ @2 n& i7 h9 r! d# V
{
" V$ p  L$ |6 X4 {& L       Cout<<iter->first<<”   ”<<iter->second<<end;
" V# K3 b0 a% q' V+ ]9 n( p7 {}2 X4 P8 ?" b1 w% }( a
}* j8 Q+ k# Z+ j5 D+ P
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
2 Q; S  j0 z  D0 R+ ZmapStudent.insert(map<int, string>::value_type (1, “student_one”));3 m# d. n. p6 n
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
$ w3 h5 W1 r0 m2 _5 v$ O上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下9 ]3 U  Q* B, t7 G
Pair<map<int, string>::iterator, bool> Insert_Pair;; e5 @! f8 m# k9 j, w. J$ M
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));& x  E! R. \+ z, d- O5 _# u; N& ?
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
: @% \# R  a0 G+ A1 Y* v下面给出完成代码,演示插入成功与否问题
! x; R: s  Q  E; ]# V#include <map>
2 {. Q* g% M: U, l#include <string>
; a, [" M6 H, V3 h0 D+ K#include <iostream>
- B- P+ l, b0 iUsing namespace std;9 s' ]0 m+ l- I7 y/ V: n
Int main()
8 Q; |  s3 t: W7 h! y) d2 e{2 r1 p+ @0 }- @' l
       Map<int, string> mapStudent;( ?. L2 D1 |/ {  @- v* K8 N
Pair<map<int, string>::iterator, bool> Insert_Pair;5 t! F) |1 d+ s9 \) |$ R
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));) j( j7 f1 {& r! @
       If(Insert_Pair.second == true)+ y, C0 Y' f. E/ ]$ g1 z
       {
. j7 d6 y* A6 L5 H- p' _              Cout<<”Insert Successfully”<<endl;5 R7 u/ r" I1 U- _; @
       }
' \  [7 R+ J* t4 f       Else' M3 B/ z6 s, ~" j2 Q' t3 ]
       {
) v. u# K% E/ B3 C4 u              Cout<<”Insert Failure”<<endl;4 B$ B# V( Y0 {- f+ ~1 A. O6 x
       }. y& @6 H( d6 a; B$ O% J; z
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了