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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数# t9 X, N2 q5 w$ s+ T3 q# S% ~6 n% S! n
Map<int, string> mapStudent;+ }8 C: W- _# N: n" y' G
2. 数据的插入
4 F* p2 c. R- L) F$ O0 @在构造map容器后
/ ^: V) c' @' r$ m第一种:用insert函数插入pair数据5 [- ~  c7 E& D( R( L
#pragma warning (disable:4786) )& e3 ~& ~+ `; w1 `2 u
#include <map>
% X/ o. {! ?& d. [3 i7 Q  _! m/ U#include <string>9 u5 t& `9 [/ v6 T
#include <iostream>
5 L) J" y8 j5 \. a6 F2 g, T& AUsing namespace std;
# F0 a$ \0 p8 J7 L6 kInt main()
9 \# O& o9 o7 V7 b  E0 Y{+ D& }# P* q% o
       Map<int, string> mapStudent;. M; G2 ]0 j% e2 a, W! ^* n: Y
       mapStudent.insert(pair<int, string>(1, “student_one”));
4 y, w  L# V% j7 p       mapStudent.insert(pair<int, string>(2, “student_two”));
5 ^0 X& W5 [5 F$ x) N! {       mapStudent.insert(pair<int, string>(3, “student_three”));
: v& n  m5 p# B4 r; u       map<int, string>::iterator iter;3 H$ K7 L5 Z0 }- W( o# ]( h
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
/ p7 c) K. w9 i; u+ z{
7 g( g7 I4 O# s; X1 h' ~       Cout<<iter->first<<”   ”<<iter->second<<end;# B6 O/ y3 Z9 G+ u0 S; b
}
6 F& |, Y/ F* |0 ^- l8 D" H}& i; b# Y7 p, E7 ?5 J- I' u; W# A+ R
第二种:用insert函数插入value_type数据,下面举例说明* @2 G! m6 m2 w4 T9 O8 \# b) }
#include <map>4 P( m" X( D1 y8 m( w8 S/ d
#include <string>' g4 W7 C0 D) q7 C4 Q. Y- w
#include <iostream>! r. ]: W# u7 C7 D( ]
Using namespace std;. e9 z; L. z1 \& K2 Y% P
Int main()
! G5 U9 q' ?. k) P$ d{
" U' z6 ?  \8 X  i" g; ]6 t& E       Map<int, string> mapStudent;- m4 W" P( B& a/ B' ], ^  b' @
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
; R/ {; d4 @! T: Y$ j; M! u       mapStudent.insert(map<int, string>::value_type (2, “student_two”));4 ^3 y: u8 b" r$ g% c
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));; O) h- Z3 P1 D
       map<int, string>::iterator iter;
/ r6 [2 ]0 H0 ]+ C/ D4 N       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)7 |. U- {; Z) A0 I& A# W7 k  b5 o
{
, E$ C! @8 m: P0 \       Cout<<iter->first<<”   ”<<iter->second<<end;6 a7 {2 Y; o  \) d2 t( C3 a" @( i
}
. M# J$ j, Y. L+ s4 R}, ?4 Q6 o- {) Q+ M( ~
第三种:用数组方式插入数据,下面举例说明$ c; a( ^( Z8 c! f. i) b" E
#include <map>
$ d" t' n9 j* M$ {: c#include <string>1 [4 |: c0 V$ i3 z; F
#include <iostream>
4 W9 G+ {8 Y+ E7 b/ o. @2 j1 JUsing namespace std;5 p9 i& i& C& ^- Z) |  ^
Int main()
, o4 L9 [% D. w+ c1 ^{6 G! u& k: F, S5 l& M+ J/ {8 F
       Map<int, string> mapStudent;
* X3 u7 c& s" \- O. {       mapStudent[1] =  “student_one”;0 a3 p+ w- T( O
       mapStudent[2] = “student_two”;
1 ^7 ^7 s0 H  b) C$ Q: e2 {0 \       mapStudent[3] =  “student_three”;( P; T3 e8 W( m- C
       map<int, string>::iterator iter;& m9 t: h5 M# w, _7 v
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
% g7 M& n1 b5 m8 I{2 v+ t8 ^: l; S3 j  P* f( w
       Cout<<iter->first<<”   ”<<iter->second<<end;( ^1 R. p0 U# S0 W
}5 ~+ N/ q7 n( V8 B+ E
}9 V5 o$ t/ [/ t) ~
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
3 X  P: m, D( T2 b* CmapStudent.insert(map<int, string>::value_type (1, “student_one”));; v& t: X+ J9 b8 {9 F! t
mapStudent.insert(map<int, string>::value_type (1, “student_two”));7 b. i7 F* y& n! a) p
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下$ S4 @/ R6 y. A6 w
Pair<map<int, string>::iterator, bool> Insert_Pair;. c" F+ Z6 r9 Q) y: c- O) ?
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
9 t' m! j# a  e7 b6 i3 [6 g! U我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。4 D, G' K' e" J6 |0 |1 j
下面给出完成代码,演示插入成功与否问题$ k5 ^5 G+ ~  w3 {! P4 A
#include <map>
$ t8 `& Y& C8 [! L7 m' q#include <string>
; K# a( ^( G& s7 ~; g#include <iostream>
7 L4 }3 `+ t) ^  F' pUsing namespace std;
; c4 v% x- i2 R- z2 ~Int main()3 {* T7 u" o  V; H% L8 ^
{7 B2 j2 }) e# E+ ^% S
       Map<int, string> mapStudent;( J8 f2 H# @5 T1 B
Pair<map<int, string>::iterator, bool> Insert_Pair;
( d9 ~; e7 Q2 ]" n7 o2 l       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
; s6 R( J; A' ?3 k& j       If(Insert_Pair.second == true)) J, _6 _) Z' j9 o) _
       {
: j- C% R0 R8 b9 g  \              Cout<<”Insert Successfully”<<endl;
: U9 {/ a3 h5 h5 q" \! ]       }. V" p0 U' n/ a5 o' o' }7 K* F
       Else2 \& Y: D; n3 k7 q/ L
       {2 \) E- H' U% ^* R
              Cout<<”Insert Failure”<<endl;
1 p4 t5 H" U9 |  y# l) G       }
* B$ l( a' q& Q% U& A+ j1 j- E
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了