查看: 4859|回复: 0

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

[复制链接]

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

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

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

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

×
1.map的构造函数+ y9 o- I9 u) A% K3 [# ?- T! V; f  F
Map<int, string> mapStudent;
' l! w6 ^$ c, f& D* R2. 数据的插入' B% ?* I1 m* ?* k4 @" l
在构造map容器后
* l5 u  y8 A/ O- w# A; @$ Q第一种:用insert函数插入pair数据/ L+ t) h9 x# \6 @$ m
#pragma warning (disable:4786) ); e4 E  K- S. b' _; B' G/ ?
#include <map>
/ Q+ g1 ]- ?& m, {( z' L#include <string>6 E) R* y7 d9 x% G. W
#include <iostream>
, W0 b& `4 w2 h& `Using namespace std;
- q7 \6 h6 ^/ z8 ~+ `+ U3 PInt main()
( g$ H+ B( E6 `$ @( Y4 p# D{
; p" L/ Q# d2 w) }! K: i       Map<int, string> mapStudent;( E0 Y0 Y/ u' h% w( N
       mapStudent.insert(pair<int, string>(1, “student_one”));
2 r0 {5 g+ F- Z/ @  f% O$ h       mapStudent.insert(pair<int, string>(2, “student_two”));
7 S7 o, P  ~# |/ N: A8 {       mapStudent.insert(pair<int, string>(3, “student_three”));/ u# ^) `) Z: `- A1 ?3 q5 d8 C
       map<int, string>::iterator iter;" e1 \8 |4 A+ W- o
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- E" j1 p2 {8 u/ Z- ]{4 x, s+ o: ?' e' J/ h
       Cout<<iter->first<<”   ”<<iter->second<<end;
: Y" Y& t. [3 Y- u}
: R  j  s! D3 t* |( s! f$ J$ ?+ w}
* @9 }$ }8 Y. |- c第二种:用insert函数插入value_type数据,下面举例说明+ r6 v7 {5 E( r- a& ~3 X
#include <map>
- `3 g1 j* y/ [- V2 K, M$ [, g! k9 [#include <string>) f# F8 A7 g  k7 y: e- t6 w
#include <iostream>
% o5 _. q  ?3 a& u3 P  ZUsing namespace std;- B+ ?$ |8 M6 _1 B$ e
Int main()' u" [0 u2 L8 Z) A; f" {0 {2 S
{8 A8 U: q5 p4 Q! R2 K7 {
       Map<int, string> mapStudent;$ P* _  O$ d) b
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));! j- U% K$ F, K0 B6 ~- k8 j
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));8 p- r9 x* h6 H1 R( H0 Z0 z. A
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));9 ~2 Y1 n" a7 ^- x+ W( Z0 G0 s1 e, ~! n
       map<int, string>::iterator iter;
* j0 }' I( J( I4 q5 u3 |( J       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
0 Z# j/ l& @; y, Q0 S1 j; e9 O6 U{
% ]9 v; E' r! r7 [6 }       Cout<<iter->first<<”   ”<<iter->second<<end;! r- O0 {7 Q6 K# P  ^% R
}" b! u* `8 _/ H7 g
}
" j$ I4 r2 {9 G1 p' W, {) x第三种:用数组方式插入数据,下面举例说明
/ F2 m$ b& K4 I2 ^; E8 Z#include <map>
6 z" {5 J/ b$ H7 D4 |% `#include <string>5 B; Z# B3 J+ X7 L0 C" p
#include <iostream>
+ K1 ^/ ]2 m7 y, Q9 R: l& Z* IUsing namespace std;+ W* w$ m0 K. D
Int main()7 N9 b7 x2 q! r
{
5 X) d0 {  \! r* l1 c       Map<int, string> mapStudent;
; U( B; h2 C" B% F1 o& M       mapStudent[1] =  “student_one”;5 x6 U/ Y* C* W1 B( |2 H$ X" `
       mapStudent[2] = “student_two”;7 E  ?2 C1 E0 }5 a# m. O* L
       mapStudent[3] =  “student_three”;* j+ ~% @' E( n) O. u7 {' R
       map<int, string>::iterator iter;+ \. W, Z7 {- r# l6 M0 j
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
+ \" s1 j* F' m0 J+ R{
2 b! m1 X3 c% M       Cout<<iter->first<<”   ”<<iter->second<<end;
+ f5 ~% I' H2 V7 h9 A0 {* U* p- C7 g}) L  P) d; n! o5 \# c8 M
}  w7 S) B1 ]8 R
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明+ _" V! m# R& }" u+ D- G+ h
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ O( ~: Q  }6 ?( o9 f0 TmapStudent.insert(map<int, string>::value_type (1, “student_two”));7 E: w* F/ V3 N' [( q1 U
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下- Y8 F6 a) Q; C  J9 [; U0 }
Pair<map<int, string>::iterator, bool> Insert_Pair;  x' X( \( o! X* o
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));/ @! v  c6 n" B. R
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
8 F; m6 V) L7 ~* K下面给出完成代码,演示插入成功与否问题7 f+ D/ w" w& M' {% c  z# x/ o
#include <map>. Q. b/ T; h3 z/ ?6 q6 O
#include <string>+ f/ E. b9 J8 w& z3 f
#include <iostream>- p' L& |" M' D* p  S9 z) v
Using namespace std;+ \( ]1 W! y& a, [. t
Int main()
3 N, l' f8 o# T1 U% i{# U8 W% ]* D) d* K
       Map<int, string> mapStudent;. p- t0 y( c6 r2 p
Pair<map<int, string>::iterator, bool> Insert_Pair;
! \/ ~6 ^6 P' z3 s% ]       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
0 g, T/ m0 y! }1 E8 Q4 o       If(Insert_Pair.second == true)
* O# ^! m& K: T0 i& `       {
) N$ @* Y$ A. a1 X3 g* H              Cout<<”Insert Successfully”<<endl;" [7 r" M( H1 m8 C
       }
" M7 T, N" l2 ], g* C# B8 d8 ^       Else* P& K' c7 U. U/ P
       {
, P0 O5 z7 H! ~' K1 u              Cout<<”Insert Failure”<<endl;! r9 g" a: l& V. R
       }" @6 R' O. A8 r
该会员没有填写今日想说内容.
回复

使用道具 举报

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

本版积分规则

在本版发帖
关注公众号
QQ客服返回顶部