查看: 4800|回复: 0

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

[复制链接]

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

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

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

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

×
1.map的构造函数2 V" c# \4 T/ }
Map<int, string> mapStudent;
) L( h/ F2 Z& |; l8 E. o2. 数据的插入6 K5 h4 _( P& B2 z: [
在构造map容器后4 ]9 l7 y8 M6 o8 z
第一种:用insert函数插入pair数据
2 S+ ?, E' \# j$ Y" U  ~, P' z#pragma warning (disable:4786) )8 h2 S: n$ \2 M8 G' B) x
#include <map>6 @/ K; V7 z7 G9 [; }$ d8 S
#include <string>6 G4 T4 }& U: z
#include <iostream>
! c9 Q; ~( j/ G5 ~Using namespace std;2 P0 _5 C7 |6 k8 C. X3 V  ]+ _
Int main()( Q9 h2 v+ R' [( `8 S/ P$ `
{
% o4 s+ w9 j- f% p       Map<int, string> mapStudent;
2 P6 O4 X3 t  t2 D$ i" Z       mapStudent.insert(pair<int, string>(1, “student_one”));  l) L. n5 P7 `5 x1 }+ J& n
       mapStudent.insert(pair<int, string>(2, “student_two”));
* W& J4 F  p" c1 b" }& T+ N4 i       mapStudent.insert(pair<int, string>(3, “student_three”));
) C2 X# a& V# M( Q       map<int, string>::iterator iter;
2 U0 l7 u  I% Z1 D$ \+ V4 m- |       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)" _# Y7 C. Z" m7 b+ J
{
- ~# _7 g6 X6 W4 w& B) G; X       Cout<<iter->first<<”   ”<<iter->second<<end;
' U' K! T, |! E6 f& ~5 w}7 J5 g3 z. p0 a. m4 R
}
* a& ~* {# U9 E6 l+ |第二种:用insert函数插入value_type数据,下面举例说明5 f5 ]' ~; }. |4 _4 F$ |6 V
#include <map>
+ T; ]3 o8 o$ R8 R4 M#include <string>! c  {# k: q& H4 O7 V
#include <iostream>
% {. H" m7 ?, G9 ?& d4 C$ j' ~8 ~Using namespace std;
% q  B7 x/ G% r6 h  |Int main()
; h3 `. u1 W! `5 e{
, z6 t" }9 V6 r$ T* \$ `7 Z       Map<int, string> mapStudent;
. |" Y! \7 n$ c7 I4 t& N       mapStudent.insert(map<int, string>::value_type (1, “student_one”));/ }; `. }$ N# R
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));& X2 f; ^0 d; K& |/ G$ R0 a$ Y
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
. g) S0 G$ C" Q  L/ t7 m       map<int, string>::iterator iter;# e8 ?( I. l. A& E# X2 Q% ^* x6 t
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
+ ~& ?3 \* S' h8 t8 ~& }5 A{
' ]! R5 X) c1 C3 g& u  k: @  o       Cout<<iter->first<<”   ”<<iter->second<<end;- t- c7 V( D3 M0 w8 B0 H" r
}7 J6 H8 h7 b- a$ \% n3 ]3 }
}' ?! g) k- B' I. B$ K# V8 t. }% t7 h& D. o
第三种:用数组方式插入数据,下面举例说明! s9 Y" @5 s0 s" |( J: H6 X
#include <map>
! N6 s) @) X+ x7 ~2 J/ _5 Q#include <string>( b5 o1 Y- W% f" ~9 S, v6 C
#include <iostream>9 K  F8 y4 a+ p1 o5 P/ R9 E
Using namespace std;8 o' n4 \9 ^  i$ x" M
Int main()
9 I+ ^, ?; |2 C& W( N{
8 [) Q9 |7 {# d       Map<int, string> mapStudent;
9 _  {: R+ v3 E/ a: t       mapStudent[1] =  “student_one”;3 _' r/ ^( o& z: y% W2 y7 d% R
       mapStudent[2] = “student_two”;
7 j, W) A7 [- a9 t4 O0 h4 u6 Z       mapStudent[3] =  “student_three”;
/ p& V% O5 h9 t  I3 b       map<int, string>::iterator iter;* j4 k* ?1 ?0 x4 x4 P' h  F
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
4 D% A2 ]* D( ]% [7 ?: l{
# S9 A8 K% U9 e6 ^- \6 g       Cout<<iter->first<<”   ”<<iter->second<<end;
0 V8 h, X! k, E1 p+ }8 F2 v) y  W}& c5 |+ v2 A" B1 ^1 ]% g
}
+ z8 A$ M( U7 A4 F9 l以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
6 t, D/ A, G" |- X5 h. AmapStudent.insert(map<int, string>::value_type (1, “student_one”));* ~9 e3 D* t3 J8 V8 R! ?
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
/ n4 Z, m+ Q8 f% C# v7 [% w上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
: A8 Q/ O1 Q. l" V6 d7 PPair<map<int, string>::iterator, bool> Insert_Pair;2 m+ m8 k' e8 y2 i& T
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));  j1 p/ F) o+ y8 n
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。/ n2 q$ \$ s2 `; D7 S
下面给出完成代码,演示插入成功与否问题$ ^* K5 m( r, B+ L
#include <map>4 S7 d4 t6 x  m4 h( o' k: l
#include <string>
$ a" _6 K$ H" y* q#include <iostream>
2 X3 R- `1 M" KUsing namespace std;
2 h& }. E" y# M/ ]" F" r  f+ qInt main()6 G7 k" J1 v  H& h# W1 t
{4 g5 q8 O- w9 l, J
       Map<int, string> mapStudent;2 Y( z' w7 x) F. ?* y; I/ K3 W$ Z
Pair<map<int, string>::iterator, bool> Insert_Pair;
, y; f3 B  H; O5 j       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));4 T- s% d% l- R- j* r7 e3 v
       If(Insert_Pair.second == true)2 d! ], Q* X/ y5 C) `0 V6 J8 m- g
       {
9 p$ R- j  D5 l0 a              Cout<<”Insert Successfully”<<endl;6 P! R4 @( h0 @- G; `
       }4 E* e  u; M" L5 l
       Else* }( _! U9 E! i: [$ e0 N
       {
' ]& k/ j  V' x+ a              Cout<<”Insert Failure”<<endl;
8 F8 o3 h% S" g' f! w) h       }* w+ C# @4 C; x% f, U5 Q+ J
该会员没有填写今日想说内容.
回复

使用道具 举报

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

本版积分规则

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