PLM之家PLMHome-国产软件践行者

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

[复制链接]

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

mildcat 发表于 2014-1-3 19:37:45 |阅读模式

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数7 O2 ~5 U, S! R
Map<int, string> mapStudent;
% [% U" A1 o  H' a/ a: h2. 数据的插入
9 h4 r0 @2 i6 R( g- ~: a在构造map容器后
2 {2 Z+ m- g# I, G7 w6 `第一种:用insert函数插入pair数据8 A  v) z. _9 ~6 N9 w4 C& s
#pragma warning (disable:4786) )3 Y  z& [- V" y
#include <map>( e  M) i$ L1 T3 h# G
#include <string>2 g9 V( C  A+ o
#include <iostream>
% C8 j) A& y1 U% i+ [Using namespace std;
9 t; u  H7 f* z! D% t8 _Int main(), E$ B& H: ]. ~7 M8 r, T- s: m9 o
{" F$ y1 c* M" h2 e" [" t  ^
       Map<int, string> mapStudent;
5 e% A4 g) H2 I  I       mapStudent.insert(pair<int, string>(1, “student_one”));  s! _3 R: {# m
       mapStudent.insert(pair<int, string>(2, “student_two”));
+ \, F" S+ ^' X0 s) f       mapStudent.insert(pair<int, string>(3, “student_three”));
( Y' h7 M. ]6 d$ A% }6 b       map<int, string>::iterator iter;1 H" ~5 U: h! V; C+ U' _
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  ~% d1 T: q" ~/ ?
{2 M/ I# I! K( f. i% y+ B3 m
       Cout<<iter->first<<”   ”<<iter->second<<end;& a% K5 D: y- V4 l( q; r& w
}0 ?( ~, N- A' d2 q9 }) |
}
+ ^1 Q  F/ ^0 R; r第二种:用insert函数插入value_type数据,下面举例说明
* w# W; o% ~! [% {- k#include <map>. i2 Q2 W. ?3 t3 K# L2 `/ b
#include <string>
6 p/ Y* I8 {8 B# X) c#include <iostream>* J- u3 K- a' S9 F1 Y; w
Using namespace std;
- k9 y, Z9 r! }' {Int main()$ r0 h4 R0 y3 r( E
{
1 A" y- ]% ^3 R& T0 l2 h! Q       Map<int, string> mapStudent;
/ x1 x3 m5 N3 @  {2 s1 J       mapStudent.insert(map<int, string>::value_type (1, “student_one”));4 l7 W( W3 V( w
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
: Q9 u& _( n6 n. [& y6 W' n! e       mapStudent.insert(map<int, string>::value_type (3, “student_three”));/ e2 [5 d% I4 e; b; }4 M
       map<int, string>::iterator iter;' ]9 m6 A. q. K2 ]1 h
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)7 P6 A; p8 y6 \4 s) T& i  t
{) b. ]9 y6 |2 M  c. m+ |4 y, p
       Cout<<iter->first<<”   ”<<iter->second<<end;
0 w. {$ d: n% J5 q0 V}, s" u5 D( T5 ~6 H# m
}
! X% O! l$ e9 R第三种:用数组方式插入数据,下面举例说明
- t6 B7 ]& r/ d4 v/ ?#include <map>( C+ ^! L# o2 V
#include <string>
1 c0 C' z; m- J6 h1 o7 Q#include <iostream>. B' k0 `& q1 n
Using namespace std;
. ~" h) P6 ]% QInt main()
+ b( ?* U# N% d! M, G3 v2 c{
5 W( \& A) K% a6 W       Map<int, string> mapStudent;" e5 q% o9 y7 D9 L3 _
       mapStudent[1] =  “student_one”;
5 I  Q, F% X  T& q+ y! M$ m       mapStudent[2] = “student_two”;- @  w1 q2 Z; x+ m7 p
       mapStudent[3] =  “student_three”;- q# d0 N) E9 Y
       map<int, string>::iterator iter;# [; h  {8 S4 {& T+ U
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
5 m7 g9 n9 @$ ^3 U; f1 n8 p- g{
# ]( P+ g- i* I# u8 y5 @5 l       Cout<<iter->first<<”   ”<<iter->second<<end;# Y2 l" b# ?  B$ s$ G
}
1 j  `7 ?- I7 }! r5 `}7 s3 E% I8 q, F, _! [: Y! p
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明2 e+ U1 ^: l5 B3 w/ {/ d+ ?; l5 H
mapStudent.insert(map<int, string>::value_type (1, “student_one”));- B  V7 f+ A/ I8 n
mapStudent.insert(map<int, string>::value_type (1, “student_two”));1 k' `5 ^, P' F% |  `* N
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
: p7 ]6 x+ V6 e+ pPair<map<int, string>::iterator, bool> Insert_Pair;( M5 g8 I* f" P/ r
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));$ K7 t2 T* s8 q& d* K. {9 ~4 Y
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
1 y+ Q0 ~/ S9 Q; l3 N( H8 |4 D( P下面给出完成代码,演示插入成功与否问题
0 _( \# ?* E4 \3 U" {# h#include <map>! ~  _. }- c/ G& d
#include <string>
- J1 W7 Q/ v6 w3 ~6 o#include <iostream>$ S' l( a& D7 P! l) ]+ N& p
Using namespace std;+ |6 X5 M+ _) ]( ~( ~. F
Int main()0 m5 \( s0 e( y! Z' ~: G
{
# {+ M( {% x$ s7 O* t       Map<int, string> mapStudent;. r4 i' Z. h3 M; f0 _/ Z
Pair<map<int, string>::iterator, bool> Insert_Pair;
) ]% P  h6 ?# b$ l! s8 u       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
0 _1 E3 a8 b, \/ Y+ ~       If(Insert_Pair.second == true)
$ ?0 j! {2 f/ e! o       {
# ]) o7 E0 N) u              Cout<<”Insert Successfully”<<endl;
+ y/ g) q4 S, p/ r- J5 t; k       }
7 i5 p. o6 C% w3 \7 K3 T/ l' o- L$ y       Else
4 }$ B$ m3 l. Z+ K       {
. ^- T  \  p6 L              Cout<<”Insert Failure”<<endl;; h. X% y4 I6 Q, S
       }
$ C, j- l* j0 Y/ d2 |% e* A
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了