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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数' j& F) W" d! g- F+ c( y
Map<int, string> mapStudent;
5 M% L/ C: f* O' p! g& U3 I2. 数据的插入' T+ u* K, @( S0 X
在构造map容器后. P) b1 k0 c% Z" \8 _+ z" x% H
第一种:用insert函数插入pair数据: c( W  }+ E" n: J/ D$ g% _
#pragma warning (disable:4786) )
  x* x4 o$ p3 E, F/ h7 q) T#include <map>0 F1 K+ f, k7 l9 l
#include <string>7 M4 R$ m& p, Y: x
#include <iostream>
( {# `% C' y8 d) z" P- |8 [! wUsing namespace std;% \+ ?) m/ [6 K# [" Y7 R
Int main()2 m' e( [5 U' a
{
( B! S, p8 U* y! Y: R: B5 [       Map<int, string> mapStudent;7 I& N7 r* h& N) m! P
       mapStudent.insert(pair<int, string>(1, “student_one”));
: c' E+ c& L  h/ h, G       mapStudent.insert(pair<int, string>(2, “student_two”));+ g. K& p8 K2 G# W/ {  K
       mapStudent.insert(pair<int, string>(3, “student_three”));, T6 K  _1 Z7 P8 P3 M
       map<int, string>::iterator iter;
5 C( b3 ?3 r; j       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
0 w: t8 _/ S  O: T5 \2 u{
0 y2 q& B- S3 O, B3 N1 g6 `       Cout<<iter->first<<”   ”<<iter->second<<end;, N- w- V# w6 P$ R
}
2 }0 L  z3 l2 j}- m) E, `4 K# C: _* q, s) E) ^0 L
第二种:用insert函数插入value_type数据,下面举例说明
& J$ m7 }* o: n; n#include <map>! D) T2 \: ?* x* l  F  I
#include <string>
7 s' P4 O' b4 S: {#include <iostream>: ^; Z* h* ~+ v  _4 J
Using namespace std;
7 w8 S5 D- M" L# c  R$ \4 p( J+ a$ UInt main()! m. F# l; b, ~- w4 l. d
{" @1 j3 U& O) X, c. \4 p) N+ T
       Map<int, string> mapStudent;
7 Z: h0 E( m+ Z! p+ T  F       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
3 F9 I% n% t3 ~, X       mapStudent.insert(map<int, string>::value_type (2, “student_two”));9 z( Y. Y& l# h; g: @1 S5 P% N
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
7 t; \3 [! x) X       map<int, string>::iterator iter;
4 Y7 T" {4 J7 I       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
1 A  [7 g5 \! \& R( f{) c5 ~6 y. l; U) p6 }
       Cout<<iter->first<<”   ”<<iter->second<<end;
3 k2 s# p; }& q}
( K$ F% \  E7 t+ W}' b5 |: _, D$ u$ a
第三种:用数组方式插入数据,下面举例说明
0 _6 H& i9 t0 G4 g#include <map>, N. b0 u8 |5 ?# y
#include <string>
) o2 Y% f% r5 D2 x- w- {, t#include <iostream>5 A! t) D6 d! K: J9 `' J5 M8 t
Using namespace std;
7 ]( z9 k- f. c9 YInt main()* b  Y% m5 X* H; \5 z
{
) L$ d( G6 Q+ @2 V0 o, B       Map<int, string> mapStudent;" k- {# Y8 q; F/ c. e8 P
       mapStudent[1] =  “student_one”;. f$ J& V1 _6 @" Q* e* B# |; _: U
       mapStudent[2] = “student_two”;
: ]+ q+ B- d7 @2 Q$ e  d. B- U       mapStudent[3] =  “student_three”;
2 D) E* y! j* B& F) s. K$ s; H6 e       map<int, string>::iterator iter;, T. j# t' p( C8 u$ a8 {
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
& R; Z9 ~( }8 ]7 O& |  A8 [1 V9 W0 l{/ q) u! w; ~1 u8 s# v4 @
       Cout<<iter->first<<”   ”<<iter->second<<end;
# r. T  |3 j3 v}; j5 [2 ~/ Z+ ~' t  K
}: e. O$ r4 S! R, R! |
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明* b5 i. W9 [9 h6 d& Y! J  w7 z
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
. |' s, S) Z3 tmapStudent.insert(map<int, string>::value_type (1, “student_two”));
$ w) R' d- A  E$ X0 d$ [上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
" S) }6 A+ e( A( @& a$ DPair<map<int, string>::iterator, bool> Insert_Pair;
8 H# Z& j- j  U( B% q( l: RInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
  R6 j" W1 D4 L- O# I( |9 ~我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
! k7 S, l  o" b7 P7 y下面给出完成代码,演示插入成功与否问题' z6 ?9 j9 u4 {; \: M; Z! v7 T7 }
#include <map># m, T1 E6 n1 q1 g0 T- R, x
#include <string>" F+ g) |, b  Q0 w3 C% z  X2 e
#include <iostream>2 E- X( R- g  [7 _" I
Using namespace std;9 ^* e" q8 T5 f: P: ^
Int main()
) k# m  M0 T2 b& R+ v( O. Q7 s% [{
& u. w/ @0 S( q- n  d/ Y* ]7 J       Map<int, string> mapStudent;
6 n* V2 I/ u; r6 EPair<map<int, string>::iterator, bool> Insert_Pair;/ j5 u  T% n, @- {; D
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));. R4 v! O; t, {! G: P/ Z8 j% H
       If(Insert_Pair.second == true)
3 @8 O5 _! r' s5 J/ p       {$ y' A) I1 w7 h; Z  f" J; d9 n
              Cout<<”Insert Successfully”<<endl;
$ h6 P7 s, b; S- j7 o1 ?       }: P4 W9 h0 {4 r# y
       Else
, T: G( U  e) k% S5 T8 \       {( C/ Y. r& r, c. C$ ]* s4 I7 q
              Cout<<”Insert Failure”<<endl;
5 L( p# R& _6 I8 ^/ X       }+ v% O# b+ f+ D. m$ `
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了