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

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

[复制链接]

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

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

积分
9784
发表于 2014-1-3 19:37:45 | 显示全部楼层 |阅读模式

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

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

x
1.map的构造函数: b# ]$ _1 O7 a
Map<int, string> mapStudent;
: g2 Q- ], i) g' {7 W2. 数据的插入) o3 h7 j" O3 {. T4 _1 ^: K
在构造map容器后9 R: E; y( V" X; m% |  b+ K# Q
第一种:用insert函数插入pair数据
; F# l7 p! S' N#pragma warning (disable:4786) )7 C0 Q  t2 V/ N
#include <map>5 ^* [# a6 M* w- U- t
#include <string>  m9 L0 k! _, Z8 ^
#include <iostream>
8 i9 {1 h. E9 K$ A+ C" K- }Using namespace std;
1 H4 q  _6 X8 E! d! L' PInt main()# y. D5 X& ^! o0 z. ?! Q
{6 G; N' p$ g# ?1 ?
       Map<int, string> mapStudent;
' x: |+ K: G0 x4 i3 H" `9 A+ C3 @" P       mapStudent.insert(pair<int, string>(1, “student_one”));9 @. z9 {$ d8 V1 v0 I
       mapStudent.insert(pair<int, string>(2, “student_two”));
; z! E+ E. K! G- _6 `9 [       mapStudent.insert(pair<int, string>(3, “student_three”));
, f- A" O: y- v+ v% @5 X( Q6 \& I       map<int, string>::iterator iter;
4 w4 n3 ~9 a( B4 ~8 ?$ |- A       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
3 U& I0 n. H- k{3 U5 E. i  K6 J1 o" z3 Q
       Cout<<iter->first<<”   ”<<iter->second<<end;: m3 u4 T3 p1 B8 f. a, o- E
}
( _$ Z3 Q9 ]# |! p}8 Q+ d) s' i$ ~# e# i$ o
第二种:用insert函数插入value_type数据,下面举例说明8 U2 R7 G2 q' O% a8 I: A5 N
#include <map># F1 h3 X" R( ]- F( y
#include <string>4 Z: h0 Z2 v  |7 z
#include <iostream>
, ^5 `$ W$ W3 z/ G$ N0 OUsing namespace std;
( r2 F8 ^- S4 v( K" K6 aInt main()
! s% }/ |/ n( [; k1 y7 U{- o+ x$ y: O' b
       Map<int, string> mapStudent;
* D6 m! t% `6 d( B  d       mapStudent.insert(map<int, string>::value_type (1, “student_one”));8 t, _  P; @& Q: }" X7 k  v; X: a" C: }9 K
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
$ B! V! O2 F$ Q  l$ H+ ?7 Q9 @       mapStudent.insert(map<int, string>::value_type (3, “student_three”));" J! B0 U6 a! G
       map<int, string>::iterator iter;
# Z! y, J+ R5 w+ R0 R2 T       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++); H0 ^- |, z# f; ]% x
{" @0 @5 H- s  S+ G  _) `- [7 u; U
       Cout<<iter->first<<”   ”<<iter->second<<end;4 A' `0 F' h* M7 i, V* O
}
) f6 O7 M' [/ b1 j2 H' Y}. Y+ t* ~( \) r& `) E  m" B7 e* I
第三种:用数组方式插入数据,下面举例说明
- ^) [+ w1 A  x' s# _% m# I#include <map>' P8 x, T( f( _6 P: s- X5 G& f
#include <string>
; o4 D9 ?2 I6 W+ f, E, Q1 B#include <iostream>; O/ }, @1 l6 V; v" c. s6 v
Using namespace std;' T- w' }( W( \' }
Int main()
( e) L7 v5 o) r) K. D{
$ v1 _) ~, F+ e$ P! q       Map<int, string> mapStudent;+ _/ i8 F, l' q. A: _% @# f
       mapStudent[1] =  “student_one”;, ^! y0 o5 t* z: u& ]
       mapStudent[2] = “student_two”;. Z8 i& t) X- H1 Q* S" J& u( N
       mapStudent[3] =  “student_three”;& R1 W5 R& g: n5 W
       map<int, string>::iterator iter;7 o4 U4 Z& r, P& P* p& @- i
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)8 k  f3 e* V+ S' W: H) L2 }1 u: K
{8 @. ?5 l  c$ i3 L
       Cout<<iter->first<<”   ”<<iter->second<<end;
5 L+ i2 C" N: {9 j% d}
7 b0 a- T$ Q- n" u" w}
" v9 E5 L2 i' R以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
6 z* [6 z7 r; M3 n9 G2 fmapStudent.insert(map<int, string>::value_type (1, “student_one”));: V" B+ b/ [& \/ I
mapStudent.insert(map<int, string>::value_type (1, “student_two”));' o9 i# l3 v$ s0 K  _
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
& P. b% \! W/ F0 lPair<map<int, string>::iterator, bool> Insert_Pair;
' e0 U5 b2 Z0 Z9 E* c, r& u- wInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
8 d5 h+ F  ~2 ^: ^( o0 o" D我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
* E* @1 _" f% p* {& ?  Y+ _: P下面给出完成代码,演示插入成功与否问题
$ o. X: w0 j4 W& Y% i#include <map>3 ~4 |( o+ }% x+ }: D# b
#include <string>" R0 f! L0 P% r2 F, G
#include <iostream>
' H0 S1 c7 u9 C1 n" h. @) Z. k2 xUsing namespace std;
: r$ J4 }/ T; {8 VInt main()/ a  e8 o/ m( ]+ h" a
{
. ~" u- R+ E/ @8 S" ]) S7 N3 ?       Map<int, string> mapStudent;
/ v$ c( P6 A  T/ mPair<map<int, string>::iterator, bool> Insert_Pair;/ ^" w( O+ ^, G+ X, ^/ j* [, D
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
/ A  v1 C8 R4 a. C4 X1 U9 m       If(Insert_Pair.second == true)
4 B# Z2 d3 c5 y* ]0 k% a) t       {
9 Z+ U, p6 j8 m              Cout<<”Insert Successfully”<<endl;
1 d' [5 @2 f6 T8 n+ e* F       }
: V, h; P) t3 x) w, N  M       Else
8 r* `- Q' F7 A( I+ o& u       {
, `% B1 ?" J7 ?3 Y              Cout<<”Insert Failure”<<endl;+ |; J+ V. ?: b5 _) k
       }
2 Y/ p: H9 a1 `5 c2 {
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了