查看: 4939|回复: 0

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

[复制链接]

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

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

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

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

×
1.map的构造函数
7 r. w3 l9 C" c$ {Map<int, string> mapStudent;% B0 S8 g9 @+ S( t7 b  s
2. 数据的插入5 T% y: }4 S/ D* I. p
在构造map容器后3 L) p" t0 x* P5 i5 r6 N* U" P! e
第一种:用insert函数插入pair数据
% ~, T4 p' u0 ~& L: y1 Q#pragma warning (disable:4786) )
# i! u5 _7 n* ]#include <map>
4 z: J: h# w7 t#include <string>
* D  Y- L5 ?- ~8 p: g: i9 V#include <iostream>
$ O- [( A. w# aUsing namespace std;  n& \  ~6 W' v& l
Int main(); P+ C: @/ W2 Q/ i, x3 R& r- X
{
* E( E# b% m; S$ I$ ~, A% I5 S       Map<int, string> mapStudent;
) D: r: P7 P, y8 j6 Z  t% ]       mapStudent.insert(pair<int, string>(1, “student_one”));
" c# N5 y, I  W+ u& k" R* [       mapStudent.insert(pair<int, string>(2, “student_two”));
% p/ j$ }1 _( ~       mapStudent.insert(pair<int, string>(3, “student_three”));
, _3 s, a9 k/ f% T4 B/ s       map<int, string>::iterator iter;
6 |4 `' a2 i! B5 \1 G$ J( g& }- A       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)6 h+ a  L! t, d* o% c
{
. q' ~% C& L; D- E5 z/ ~5 o       Cout<<iter->first<<”   ”<<iter->second<<end;
) L4 i' d4 o/ S; R3 N* V4 s0 x7 X( m}% |1 ?2 f: i# J% J6 g3 }; G/ K
}
0 m4 c* a/ R: h0 B5 |9 [  w0 V第二种:用insert函数插入value_type数据,下面举例说明
* z$ L3 Q4 S- C9 x: v6 d#include <map>" e# D# o( t; d/ H
#include <string>
- ?& ]/ h8 H6 v9 [& B#include <iostream>- ^" ~. j# |/ V" i- r# B
Using namespace std;- I6 L8 E) Q$ ~% h: ]% d& D( u( w
Int main()
) l( C9 W) Y4 q8 D( x3 E{
) L5 w- L: O$ b- s2 r       Map<int, string> mapStudent;
' A( d. B. z+ U- J2 Z1 u       mapStudent.insert(map<int, string>::value_type (1, “student_one”));" O; Z" A3 }: J$ H
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
$ W5 u& R$ h/ Y) m2 F# n+ T  b       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
4 }" v0 K8 v0 h8 b) z       map<int, string>::iterator iter;
8 u% D* q( ^3 r0 X. O" x9 C) N! ]* l+ [! `       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)$ \) C% ?, @3 c+ C1 R0 u/ M, W
{
4 Y" Q$ \# W2 ~       Cout<<iter->first<<”   ”<<iter->second<<end;% p+ H8 L/ [/ c' ?5 W
}$ H4 S) q# e% t( h4 a
}8 v& e9 r. c* c1 }+ v- Q" R) o; Y
第三种:用数组方式插入数据,下面举例说明3 H+ ^! f  h5 Y; Z9 d
#include <map>: L& j$ A: A/ p, Q7 A6 w
#include <string>! Y5 ^$ K3 L* k. B' G' [
#include <iostream>
5 H5 k. W- t" E) d: b# r6 P( n( zUsing namespace std;( P: X/ `3 k  Z2 {4 E
Int main()" J- h* y7 Y7 C/ I, T- i2 _
{
0 j" l2 @+ V$ p: W* Z6 i" [       Map<int, string> mapStudent;' U% B  k, Q2 ?0 i! h8 @# s
       mapStudent[1] =  “student_one”;
+ t( f( Q" p) R       mapStudent[2] = “student_two”;
6 C# F, ~0 L, {" S9 f       mapStudent[3] =  “student_three”;8 A: x8 R: I  X
       map<int, string>::iterator iter;
. ]+ a( G4 ^. U% N       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++). Z+ V; e' X0 S+ J8 Z2 L( f+ @
{9 A* Q7 o* g  |2 z! I7 t- v
       Cout<<iter->first<<”   ”<<iter->second<<end;
, W7 T3 J8 \  m}
- u/ \1 `0 g! l}
( p* w. F: q: D8 X# A: S8 U+ @# J以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
- d" p4 @" o; R! U' i( l# hmapStudent.insert(map<int, string>::value_type (1, “student_one”));0 n" r% ^7 Q' k( o+ S
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
" q- O+ [9 U8 k; r上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
9 f& K1 y2 ]- p5 s( fPair<map<int, string>::iterator, bool> Insert_Pair;& ?2 a; f* ^* l( w5 Z7 g( p
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));' H) o: l. ?5 d5 k
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
1 P7 K% v$ R; s) S下面给出完成代码,演示插入成功与否问题
* p  r$ f+ b8 V! U6 C7 e' E+ ^#include <map>
: ^- Y8 b+ f5 `! @4 _#include <string>
$ Y6 w# q( F  |7 n$ K8 ^5 l9 b#include <iostream>
6 ?0 i8 \$ j* T+ O* D& x) hUsing namespace std;
% a" j" V/ v, h7 U$ D+ L& GInt main()
6 Z" |1 ~" @3 i) q, X{1 v9 r  x/ m5 x  v. p, h
       Map<int, string> mapStudent;3 u0 z# I; |  Z( U
Pair<map<int, string>::iterator, bool> Insert_Pair;; o/ Y. ^9 Q! s+ j# V
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
% }& t/ ~" q" }/ h- z       If(Insert_Pair.second == true)
9 J6 s* \; p& \       {! m# t4 H' P2 E" n6 {$ C* O. M
              Cout<<”Insert Successfully”<<endl;
( o2 e% H4 H' X+ O       }1 W  m) W: ?1 H4 H6 L
       Else
  Z' l9 I( W! w+ {1 \8 B! g       {
$ b" u" w8 |( D+ x              Cout<<”Insert Failure”<<endl;
8 B5 c9 C& N7 d       }$ l% K6 r* f- w# w$ T
该会员没有填写今日想说内容.
回复

使用道具 举报

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

本版积分规则

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