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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数1 B# f5 h; u+ X# D- s/ j& d( Y
Map<int, string> mapStudent;
- e! v% P& y4 {1 Q8 g5 Q1 S3 X$ b1 r- [2. 数据的插入$ ], P: Y$ V2 u" F  ]+ O6 I, A9 f" i1 E
在构造map容器后1 M! R. U2 }' ]# F
第一种:用insert函数插入pair数据
5 `7 {" t+ V* q) p  p0 d#pragma warning (disable:4786) )- ]8 d6 C, R! ]$ s
#include <map>
4 b6 f7 }- O, I# W$ e& a$ w#include <string>* P( b) U; x, f! y# Q& U
#include <iostream>
" I$ h1 c  \5 A; \% N0 XUsing namespace std;  n3 j) Y+ I' O& g
Int main()- v2 u, g% ~8 i5 b; |7 |6 e
{$ k# P6 k$ {$ z+ y2 W0 P
       Map<int, string> mapStudent;
* E* }+ \7 P" P/ D: |9 F1 x       mapStudent.insert(pair<int, string>(1, “student_one”));* S: w: M9 I# a/ @$ r; F/ t3 c* O) Z7 d
       mapStudent.insert(pair<int, string>(2, “student_two”));- D+ q9 |( G* u
       mapStudent.insert(pair<int, string>(3, “student_three”));
2 Z  u6 F* H2 u5 U* z4 N       map<int, string>::iterator iter;0 e+ f1 U+ h$ ^- J1 n
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  a( V3 C! m1 Q- ]! Y, a
{
/ B/ L# o. B/ A* U       Cout<<iter->first<<”   ”<<iter->second<<end;
4 I! G1 l1 u& l" D" m+ z& O5 T}: s7 H% ^% b# X
}# |; Z" ]# k" r* O6 E
第二种:用insert函数插入value_type数据,下面举例说明/ Y9 H. e+ O2 Y! H/ y# O  k! G9 g: f
#include <map>
4 }. C+ p$ ~. i5 O  l/ ?3 G) t3 {#include <string>
2 z8 |$ t) [7 [( W#include <iostream>+ j; J- Q& a2 d4 `" K, i6 p
Using namespace std;
' q7 s& q% A3 W$ }8 f' fInt main()
6 H* i) w, S+ M8 v) l( p" E, _{" _* \- d: [7 o1 a
       Map<int, string> mapStudent;
* ?) Q2 I! [' x% G3 i       mapStudent.insert(map<int, string>::value_type (1, “student_one”));, F' D/ o8 V, T' h
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));$ c* M  @( k* r; A5 i+ S
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));! Z- G9 C7 t2 e2 b; z' N
       map<int, string>::iterator iter;
( i/ _& \3 H6 S, q2 v       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
  ~/ S! H/ o6 {% V+ M; t1 L{. H6 C& x" J% P6 Z! M
       Cout<<iter->first<<”   ”<<iter->second<<end;
: i0 ]% @; r$ ~/ A8 `  w}0 C( |- v( {% o3 {  h
}
' f# D* r8 p) W* p) P$ h第三种:用数组方式插入数据,下面举例说明
& u$ ^& h4 \8 @' d" a1 b# h#include <map># L" H0 _% \: K" W; i+ T
#include <string>
: T! Z9 H- H) ?: H: u1 ~% u#include <iostream>
" ?  Q2 E& L/ o6 L2 o+ jUsing namespace std;
. V7 W6 f! w. A" a, a6 hInt main()! {: R* m! ^5 c5 {
{' E) P5 g2 ~& I# Q( R! t
       Map<int, string> mapStudent;
) c9 y! y  E9 J0 I$ h  `       mapStudent[1] =  “student_one”;
5 p+ I# i7 A" H       mapStudent[2] = “student_two”;% ~# S7 u  Z, X! S; W$ y
       mapStudent[3] =  “student_three”;5 h* w$ g" _/ ]! Q. j: V$ O$ l) U
       map<int, string>::iterator iter;
2 l: R6 M/ f, U% M2 R       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)+ I# ~) \& C: J0 C/ r# C
{
6 Q0 Q. o# p3 m: I4 n! G       Cout<<iter->first<<”   ”<<iter->second<<end;: x/ S+ w0 |3 h1 b( d9 x
}
  f; ~' ]8 H; e1 ]0 u5 t}
& i) I, T: p1 O以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
! }' {  g( |8 n  S" O7 K9 w4 z" wmapStudent.insert(map<int, string>::value_type (1, “student_one”));
7 A0 V7 T7 ?/ B" E! p+ ?* amapStudent.insert(map<int, string>::value_type (1, “student_two”));
* N# m8 G4 D* A6 X' C! W1 k上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
  s' _- P( g/ n3 L8 \  x* i+ G5 VPair<map<int, string>::iterator, bool> Insert_Pair;
' k% v% J& L) Z' P3 lInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));% y, b3 w& @; p4 N5 V+ Y
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
$ v# W0 R- D, D8 o9 R+ r5 \) M下面给出完成代码,演示插入成功与否问题2 I8 ~4 @$ q' M: T
#include <map>
+ J% h1 |- P* B( P& j7 N8 R#include <string>
0 T4 ^$ R# q. |% U$ k( x0 d# Y1 b. K#include <iostream>
6 N- r; p0 q4 m) BUsing namespace std;
5 {9 j( b* b4 V, N; v1 [Int main()
: B2 s, K# n2 N6 y+ P3 ~3 X{
) L8 T  [# U& }( @" V* L, k       Map<int, string> mapStudent;
. d4 [2 O# `/ @6 T- RPair<map<int, string>::iterator, bool> Insert_Pair;9 |& F, g' S& d
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));' |; h3 Q/ }& K7 z# _7 v2 P* [9 O
       If(Insert_Pair.second == true)
3 F2 g( E$ a' u3 v6 }       {% O" b' h7 p# O0 |! F( D4 i
              Cout<<”Insert Successfully”<<endl;& F$ G9 H3 j* l. L1 x
       }
; e, d$ L8 A; G" F       Else
) e! K" P9 i2 |1 R! j       {; h, t3 D4 [1 S' t; _, F. ^. t: \
              Cout<<”Insert Failure”<<endl;
; c2 s3 J+ e& g- I( m       }) n! f- N: o) c# g
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了