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

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
! t/ ^" `% F1 o8 F5 kMap<int, string> mapStudent;8 S0 O& Z4 [% v6 Z" {5 p7 J1 Q; l
2. 数据的插入
' Z* A  j" g" s/ h# A在构造map容器后* ], l) n8 ~) |4 V2 j
第一种:用insert函数插入pair数据
/ ]! Z+ m' x& C( }5 F: A) y; h#pragma warning (disable:4786) )
) O6 e) V& U0 F6 s) h#include <map>7 G- q6 Z0 ]  j8 f# y4 P/ a6 w. i$ d
#include <string>
# G1 J) F) s! t' K, Y( v! f#include <iostream>/ @* Q: n. z" ^7 f* H
Using namespace std;
& P5 a8 y0 ^$ R  m. \: l* {Int main()
( [+ a* c* `& u8 s5 d+ a{
' @7 \  L, S/ \0 U1 F9 {( c       Map<int, string> mapStudent;, W/ I/ \/ \. L! @6 k
       mapStudent.insert(pair<int, string>(1, “student_one”));" A% y, F: {, s
       mapStudent.insert(pair<int, string>(2, “student_two”));
  l8 x4 ^. g+ `8 h( r; V       mapStudent.insert(pair<int, string>(3, “student_three”));
- @; y4 R$ x) G: i7 u       map<int, string>::iterator iter;$ u9 d$ r* f" Y& y' p1 F  X, x2 _
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)- P% [  m4 M- u6 \
{/ W4 B" ^* k1 x, y0 v0 F
       Cout<<iter->first<<”   ”<<iter->second<<end;8 \% E  ]# t# U( Q. s9 p8 |
}% V1 H7 v+ R( I% W6 Z
}8 v/ j" J. e% a* B
第二种:用insert函数插入value_type数据,下面举例说明
" z+ g9 t2 o% W" A$ j#include <map>) j, s) u' G2 f+ b: B/ N3 Q
#include <string>4 D+ w! q! Z3 Y5 Y' n( V* C( {
#include <iostream>( x% g# s- ^' J1 M+ h& u* k, a) C2 C
Using namespace std;
! e" x" M9 g. h' f! ]( ]' EInt main()
/ O' g& T+ K8 ?! @{) Z6 \3 ]+ ?2 o5 Y: b1 h$ w
       Map<int, string> mapStudent;. [4 o3 @8 n" j0 \
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ S6 I6 G, O# W) j       mapStudent.insert(map<int, string>::value_type (2, “student_two”));( I# p4 ?' A0 T6 d
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
' b0 O0 |9 m( N       map<int, string>::iterator iter;
- g! u4 d% w' E* [+ X# r8 ^       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
; N2 i; ^. x1 {' _8 R9 X5 J{' G0 D0 ?+ U5 Z4 Z2 _
       Cout<<iter->first<<”   ”<<iter->second<<end;
& g5 D/ O- Q6 g, i}
% {$ x! g  ~- Q! \( x}' C9 \; g# q1 M7 C3 Z
第三种:用数组方式插入数据,下面举例说明
0 V4 f( b" A4 b  M4 L; }- P#include <map>
; d8 ^; T6 P0 l$ g/ }8 X; C#include <string>
7 u4 T+ M) x+ R( `#include <iostream>
" |  {4 ^4 L$ Z* k& y+ g( zUsing namespace std;
2 G3 p( ~; c% z, f1 z/ }) b) `Int main()
3 T, x7 f: A7 u{
: W5 T) C: L) G4 @: |       Map<int, string> mapStudent;& P4 l6 Q( E1 g
       mapStudent[1] =  “student_one”;
4 \, c, m$ |- B: h& l       mapStudent[2] = “student_two”;% [5 ?: X& J( l* f; y
       mapStudent[3] =  “student_three”;+ o+ _4 l' E/ ?$ v
       map<int, string>::iterator iter;6 D6 \/ g1 p  O1 [1 }( Y
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
9 g& P1 `; j/ e$ @{
) B* P0 p8 R, g4 |# c! y       Cout<<iter->first<<”   ”<<iter->second<<end;
  I& g- `6 m0 Z) j5 ?, S}. W) F( d5 M% @( i7 v7 F" S3 f: c
}
6 P. l0 V: q: `1 e1 \& u* ?以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
7 F2 T, U; |( a4 U4 ?' F9 BmapStudent.insert(map<int, string>::value_type (1, “student_one”));
, o4 h( U7 R% wmapStudent.insert(map<int, string>::value_type (1, “student_two”));
, s  ^  ]! e5 E上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
, c$ X6 c" q# K; G/ ZPair<map<int, string>::iterator, bool> Insert_Pair;- M: O! x& P1 [( N% N
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
% p" Y4 U( u$ I. J) Z8 f我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
- Q, j6 Z2 j2 p9 Q下面给出完成代码,演示插入成功与否问题
$ z7 Q  }3 H: i6 g2 j! j9 Y* w#include <map>
; X9 @$ n/ u2 C& f. }#include <string>0 ~# m7 D, F7 y# q2 z0 C' h
#include <iostream>- g, p8 ?0 \$ E+ z' N6 L, Y# i% B
Using namespace std;
4 p1 P. H6 ^, X+ n6 w' g; t# o2 |Int main()
# i/ _8 F' Q( Q( J{$ I/ |# T  e/ h: q. |
       Map<int, string> mapStudent;
" M4 E$ B% n2 U/ l! W' `Pair<map<int, string>::iterator, bool> Insert_Pair;
" \; B! ]- b. `2 l/ O9 a" [3 C       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
$ z& o2 B3 ~- i+ {- q       If(Insert_Pair.second == true)
8 }( P3 B+ I, K       {+ z  q5 d0 K' |7 E  e& n
              Cout<<”Insert Successfully”<<endl;8 f; U' T5 d% L5 i$ q3 k/ @1 H4 y: j2 B
       }
2 `! i5 x% {* S. d6 Q) }' i* M       Else$ h2 X1 P- s' P' q& A6 e9 B8 j
       {4 }8 `+ G! e2 y4 r5 y# k/ Z7 ^: m
              Cout<<”Insert Failure”<<endl;) i2 G3 m0 a+ E$ y2 K% z/ G/ S, X
       }
! `% k% N5 n- y/ a, K% n! U
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了