查看: 4756|回复: 0

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

[复制链接]

320

主题

226

回帖

9784

积分

管理员

PLM之家NX|TC专家

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

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

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

×
1.map的构造函数# n: |) S0 j9 g( L  [/ B' x
Map<int, string> mapStudent;
/ P$ d7 U4 Q9 e1 m2. 数据的插入5 s; O8 t+ L! k6 @
在构造map容器后& p0 S! C4 V2 j
第一种:用insert函数插入pair数据
: T; t: J3 h& J0 L! `  H/ k#pragma warning (disable:4786) )% L3 x, X3 y1 \2 ?& c' t. w' g
#include <map>
9 |  e$ P$ @- f* ]$ g#include <string>2 W6 t, W/ N# z+ P3 W
#include <iostream>) w$ L! k$ _# o" f# t9 {+ G4 Q
Using namespace std;& i) _. F! o1 z$ \0 I6 d" p
Int main()
, `, b) V# H) Q' ?& N  }{0 B8 f" X$ P+ [
       Map<int, string> mapStudent;  {( V5 q9 @6 j# D  f3 y
       mapStudent.insert(pair<int, string>(1, “student_one”));
! E' a" ]. l  b  a% A: {' A       mapStudent.insert(pair<int, string>(2, “student_two”));
$ v: C7 H; @( s* m       mapStudent.insert(pair<int, string>(3, “student_three”));
7 O( ]/ _1 `5 {: q       map<int, string>::iterator iter;
% o  ^2 d+ D7 r" s9 }. b       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)9 B2 q9 o9 d  L5 N: L/ A
{! C3 s: |( X7 J" N
       Cout<<iter->first<<”   ”<<iter->second<<end;
3 d. l+ S# V0 @# x9 k( F/ y}( c" n: }: ~4 V! r/ U% g
}
+ X% t+ D! g- E5 M" V第二种:用insert函数插入value_type数据,下面举例说明
6 q: K; I7 Z7 ^0 O0 X" k#include <map>
7 N( _+ w& a, k/ H2 T2 D- w$ ?# L#include <string>/ r5 {3 `; y* g8 t( S
#include <iostream>" @7 W! i: m2 i
Using namespace std;$ y3 r, d6 ~4 R5 P  Y' c' p
Int main()
; V( e/ J* E1 m3 q{
5 z' f. \; P! G       Map<int, string> mapStudent;
5 o" H+ y* V1 u       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( G6 j. E* b. Z: R       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
4 J) X0 Q$ X, T# t& d! G: P       mapStudent.insert(map<int, string>::value_type (3, “student_three”));, r* U9 A1 b  m0 O" H
       map<int, string>::iterator iter;) \3 x) m& p8 T4 d5 q; @' a
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)* a- @8 @$ l* [; f! i6 k+ W7 m
{7 R# F/ f* C$ K$ V$ w3 Y  l
       Cout<<iter->first<<”   ”<<iter->second<<end;
( u$ t/ D3 U: t% a. R2 o3 d2 V. g: C}  w# u" B% G7 Z
}
# q) {, m1 E" \/ p$ ?4 q9 [3 z第三种:用数组方式插入数据,下面举例说明
$ o! X( `! o0 ~2 G2 w. d5 [$ t#include <map>4 @! V% x' O7 R2 y$ N
#include <string>
' w- ^1 O6 n& L+ g1 ?5 M$ ?+ B3 b; ~#include <iostream>3 {0 }5 ^0 \1 F& z
Using namespace std;- h; @4 b) o  ~5 V1 l8 {# ^
Int main()4 h2 _  d2 Q; m3 [( m
{" \& F$ r4 l* v, ?2 X- M7 `
       Map<int, string> mapStudent;3 @6 {, W6 r* w, c" \
       mapStudent[1] =  “student_one”;! A2 `; j, W9 D2 ?5 e
       mapStudent[2] = “student_two”;. l* H7 S  Q9 g' C$ i. z# C
       mapStudent[3] =  “student_three”;9 O* ^3 y2 X& A; `$ J7 j
       map<int, string>::iterator iter;
$ B4 U: S( f8 F+ x3 Z       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)1 T. l4 m3 `% |0 R
{  ]$ ^6 D* ^+ f) s* X
       Cout<<iter->first<<”   ”<<iter->second<<end;
2 M  j4 l5 v( N}
: _# L5 F5 U$ Y; z' z' d( @}
/ n/ v8 ^$ W: g9 E# b以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明/ m3 |4 e4 E) o! _/ a  W) w
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
" w" j5 }1 J3 V3 J1 x" S% f7 R7 QmapStudent.insert(map<int, string>::value_type (1, “student_two”));
% B+ i7 _% o1 L9 C% ]7 ~# [4 k上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
( j( P. _6 i: F# `Pair<map<int, string>::iterator, bool> Insert_Pair;
" O. B0 t# w/ {9 H3 p* a' lInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
) O# r. L  \  @" N* v8 F我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。: {7 x: m4 I, }1 r8 [0 K, x
下面给出完成代码,演示插入成功与否问题$ U/ I, F& z( ~" Q
#include <map>; \. |! s% U0 A& ?0 p( W: ?0 O# b
#include <string>$ k% o" c2 K) d2 U- M0 c2 J7 i, J4 o- L6 @
#include <iostream>" i: G4 K! B3 W5 T+ |6 S# K
Using namespace std;
; S, z" q% A$ |2 J: M3 I) i# WInt main()( B' e- ]; I) J: r7 e
{( \( N7 X8 u6 T+ A
       Map<int, string> mapStudent;9 \9 k: Z; v$ J8 {& J5 l, Q
Pair<map<int, string>::iterator, bool> Insert_Pair;
" p: V6 p4 v- x5 C: g       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));" j; H5 N- W- N: g! |4 x. w9 L# T' f8 |
       If(Insert_Pair.second == true)& d. U. R0 _) I3 f" B0 y/ i
       {
- Q& ^- S( T# Z9 r$ m, g; H1 `5 x0 Y  @              Cout<<”Insert Successfully”<<endl;
; |, M4 O% W0 G+ G       }( M% n% c: x7 q$ Y
       Else9 O3 e& k+ r: C1 l5 G$ e
       {
, w4 k4 x8 C4 j& D# L, Q( [6 J  t" J              Cout<<”Insert Failure”<<endl;
/ l+ W; }) O, E& x       }! M2 k. c9 c; f2 W/ @; D
该会员没有填写今日想说内容.
回复

使用道具 举报

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

本版积分规则

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