PLM之家PLMHome-工业软件践行者

[转载电子书] C++ 中 map类的具体用法实例讲解

[复制链接]

2014-1-3 19:37:45 4621 0

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
+ z' q, [+ S; E- {! A5 bMap<int, string> mapStudent;9 }6 A4 v8 Z. u) m( O& n) }
2. 数据的插入, {5 o, B; l+ c7 v
在构造map容器后$ v# `/ z, b, A0 F* O
第一种:用insert函数插入pair数据0 u* p; x$ u, E! p+ I
#pragma warning (disable:4786) )
/ {8 ~/ Z5 A- H7 C2 r#include <map>, T" ]$ @1 J+ N0 ^8 z
#include <string>
- ~$ Y8 f( Z+ r$ L  u#include <iostream>
& M9 D+ x8 O! i  l) f9 fUsing namespace std;
, v+ Z! n, V7 L- r, m, VInt main()
* z% `8 Q3 ?: Q$ S% D9 p{
% Q) M! @7 C6 G0 l4 ?/ ?5 Y# K       Map<int, string> mapStudent;9 d' T. y, g1 q( K  O! _+ J
       mapStudent.insert(pair<int, string>(1, “student_one”));
4 P/ `& L( b8 `, D- H" u( P       mapStudent.insert(pair<int, string>(2, “student_two”));4 u: N: L, S( g4 ?# c
       mapStudent.insert(pair<int, string>(3, “student_three”));
5 T/ Z9 t( K7 x- {: {6 X       map<int, string>::iterator iter;$ r! D9 t  f: V1 Y1 o) F; z
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- T. E- y- w" Q5 v{7 ]# f6 J! f  J, a6 [5 k
       Cout<<iter->first<<”   ”<<iter->second<<end;* ^0 D7 M9 o0 k3 F5 R* t
}/ L6 S& X  R! k( T, }
}
5 A7 W  o/ p( l. m3 ~6 F第二种:用insert函数插入value_type数据,下面举例说明) \0 p4 B* @* \
#include <map>' P' \7 h+ _1 H. ^% D
#include <string>- i1 A5 N  n2 h! u' R" I
#include <iostream>
7 k4 u% [( W" L5 p1 tUsing namespace std;) v/ h* `' C& v; A4 u- d
Int main(). M9 \3 R. {6 m! m- V0 ^# H
{( a( T; T0 m, |: @) H
       Map<int, string> mapStudent;
2 L& i' `, e9 e+ C& S: R       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
& D( n3 S- f9 V       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
2 R' c4 t: l7 T4 ]  ^2 \1 [! N       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
, [* x( E1 m, E5 }       map<int, string>::iterator iter;+ ?; }8 ]7 L* ]3 l! [1 \7 p: c# S0 `
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
: V- @" {4 o, C& p{
3 P8 s+ d: y* `* A       Cout<<iter->first<<”   ”<<iter->second<<end;  n0 O: M7 q8 l7 Z( h' p; \
}1 R; b( k5 z! L% S, n1 Q+ S  B
}
( O" f  x' j$ k2 h: v! F第三种:用数组方式插入数据,下面举例说明
$ M) s& D! m2 F, q4 I" Y# |#include <map>9 L4 {! R; O5 {( W) Z
#include <string>7 o) i/ |2 Z$ x! S* F3 z; n
#include <iostream>
. V: g+ V! o0 S8 s' T3 {Using namespace std;
7 _$ R5 p: B$ e4 ]" |Int main()
" f% C* H4 \; Z6 W. o/ M{# g% u! N! m: W# z, O$ D
       Map<int, string> mapStudent;& _- ?) R7 S& c) r( C7 {
       mapStudent[1] =  “student_one”;8 T. \/ j( Z7 g: F0 L) b7 ^3 }7 F
       mapStudent[2] = “student_two”;
4 Y, e3 a: C8 O# }7 e+ C       mapStudent[3] =  “student_three”;( E8 {( b% ~, @) ]
       map<int, string>::iterator iter;, P8 B3 e5 B, o, F
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
" e1 b# m6 x8 Z0 v3 a7 B{
) r) I0 U: s9 U: f0 A       Cout<<iter->first<<”   ”<<iter->second<<end;# ^# S) t5 W% c' K  ^' h) D* z
}* f+ R7 \- H$ r6 L
}
6 G( {; i6 U  G) d( S以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明5 T8 U% c. A5 N7 `6 _: \
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
  z4 g# N! \# I( NmapStudent.insert(map<int, string>::value_type (1, “student_two”));$ N! V5 r" j, r. e$ B
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下" {% I8 V) t4 s) a- K9 z4 \' Q
Pair<map<int, string>::iterator, bool> Insert_Pair;8 ?" w: X; t' ]! N9 o% o1 B
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
; {4 S1 }3 h+ J  ?! f$ I( C我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。* K& }- o; D( k1 _: U
下面给出完成代码,演示插入成功与否问题
4 u4 p3 \  p1 d7 x$ ?( d  h#include <map>
) r/ s# u; X9 B* `4 ^; K#include <string>
  F/ _# Z* l- N4 S9 u2 W/ V#include <iostream>. E6 ]/ h$ P8 D& D( l# w7 e% Y
Using namespace std;* B4 ?5 S2 k8 W
Int main()
4 m3 Z, I5 h% U/ V; \3 O" z. r{' C) Y9 I) E8 y  d7 b: }% j0 k+ ~& k" L" C
       Map<int, string> mapStudent;
/ O/ q( {* C4 X* b; z9 HPair<map<int, string>::iterator, bool> Insert_Pair;
/ V6 P) [- v$ H' k/ h7 y       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
. B$ N$ S" I7 g2 Z) E; r# V- g       If(Insert_Pair.second == true)# e" v5 G5 C  P% A7 g; H9 Z" h
       {" |( Y9 p8 B( ]2 \' n6 y
              Cout<<”Insert Successfully”<<endl;1 D; Q) p! [6 z) z  V$ b8 j
       }
7 H  j: B- P5 }# k       Else
7 o2 P1 b( ?$ T       {% ^* L! y5 o3 z* J
              Cout<<”Insert Failure”<<endl;
! y$ ]: }1 q6 P" i9 Z! \       }/ ~, y0 x/ J8 j  u2 E# c3 p; T
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了