PLM之家PLMHome-工业软件与AI结合践行者

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数5 W* c; Y- M4 |" }) a
Map<int, string> mapStudent;  S9 w- s: x2 H4 F
2. 数据的插入9 R5 G# n% `9 q/ z
在构造map容器后
. ~7 X/ W# e( K! I& U' r6 }5 K第一种:用insert函数插入pair数据
( F  e# H9 R$ Z+ H! j#pragma warning (disable:4786) )
1 z; ^" [: R% c+ H( x& ~, v#include <map>
% w0 o2 V( R; s0 `( u1 _) H4 y5 X#include <string>
% C# c2 C8 X; K7 A: Q# y#include <iostream>
6 ]7 d7 C# Q/ a/ L9 vUsing namespace std;
& E* ~' i% |6 e5 |4 Q4 MInt main()3 F+ a" z' P' _9 D9 U3 g3 N
{
. o" X6 F9 X; u2 K# U       Map<int, string> mapStudent;- z, o" f6 `2 M+ p! \7 Y' f3 [
       mapStudent.insert(pair<int, string>(1, “student_one”));
& n* X( u5 I" b. V9 L& d9 b       mapStudent.insert(pair<int, string>(2, “student_two”));
! \3 d& F' P5 G9 \7 ~6 _8 p       mapStudent.insert(pair<int, string>(3, “student_three”));. A& t. C5 Y% I2 Z" T" W+ h' U
       map<int, string>::iterator iter;" O4 I; g, I! x+ L! v
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)! L. b/ ?  \, v0 a, n! N% E: @% g0 `
{
$ P4 g& d  P. r+ w0 k       Cout<<iter->first<<”   ”<<iter->second<<end;* a" q( V, i' B1 [. Q$ J
}, ]: M: W9 X, O/ z: \7 l
}/ z# J4 |  Y* R# ~
第二种:用insert函数插入value_type数据,下面举例说明
- {, x; w5 h5 ], ]#include <map>8 r8 N4 ?& Y3 M( ~& j7 v: c% X
#include <string>
4 ~; v9 F2 V1 @2 Z0 [#include <iostream>! c0 e; D, O1 J! ]
Using namespace std;
# _$ X. M+ L$ G& @) Q! uInt main(). |' S! T& d9 W9 ]) ~
{
; e* T! [5 {) |: W       Map<int, string> mapStudent;6 o' S- r8 I; v+ k6 \/ I
       mapStudent.insert(map<int, string>::value_type (1, “student_one”));
0 w  h9 i8 c- ~2 E- N, Y; e       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
9 i5 p  V  h. X7 \0 S- X       mapStudent.insert(map<int, string>::value_type (3, “student_three”));. t( @5 m. f* r' y, V3 w2 b) ^
       map<int, string>::iterator iter;1 A: U& r  h& J- g' Q7 u0 M4 [! \
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)9 C% W( o0 F9 U6 A% [" a! [
{
9 j: d; F) \) ]% e       Cout<<iter->first<<”   ”<<iter->second<<end;
" a# @' y8 E* f( ?}
; j$ b& W* B. w/ _}
* F. d4 \+ d/ {0 f' `0 `第三种:用数组方式插入数据,下面举例说明
, n1 C0 w: w7 d; w#include <map>
, A5 R3 W  Z* g#include <string>! g0 o$ ~' E$ O8 b% x, R; |( {
#include <iostream>
+ B; \- ]6 Y9 n' E' ~Using namespace std;+ G: J1 r% @% U
Int main()
4 [. M9 d, u, A9 D{/ a! p3 h1 _& D+ g; h7 R3 U
       Map<int, string> mapStudent;
8 [3 l( V# Q% S, y3 _       mapStudent[1] =  “student_one”;
4 l1 z+ n, C; K: q8 D; g0 B       mapStudent[2] = “student_two”;+ t/ E% `1 o( w0 n
       mapStudent[3] =  “student_three”;( P* v* u9 D" c+ l
       map<int, string>::iterator iter;, P$ v) F6 W4 n) R8 H! d0 n
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
8 n; y( j& a- \9 D3 l{& `/ S: b& ]: U& ~
       Cout<<iter->first<<”   ”<<iter->second<<end;1 R& x/ ^/ h& J6 _! y1 o/ |2 P
}
, a6 l0 x6 p. G2 [0 ~6 a+ {}
: }6 R9 Q! E. L5 e/ N7 M+ q以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
: V& c/ X/ Z$ ^" E4 omapStudent.insert(map<int, string>::value_type (1, “student_one”));
! S: ?  f" H8 L1 \0 c9 smapStudent.insert(map<int, string>::value_type (1, “student_two”));1 Y; \, Q0 K- _" i" H0 f+ P! K
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下1 F# S! T- l% w/ A; v) u
Pair<map<int, string>::iterator, bool> Insert_Pair;
: Q. W3 O' k; q& L- H% `' o8 a4 w5 gInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
. H$ |' d, A8 W8 U9 t+ \% _- M我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。+ R. o5 z/ a; K# y9 b% x  }9 V. m# N
下面给出完成代码,演示插入成功与否问题7 z9 V, }* B: Y7 K8 n. g
#include <map>7 w. z, ?0 O1 M+ j% }
#include <string>9 K1 c+ y; k# c& c' I
#include <iostream>* a0 H) Y4 t  Q* f7 `8 B9 @
Using namespace std;
9 q+ x9 O- H3 v" EInt main()+ D1 O& z8 A8 M) r/ @
{( V3 G% R; x4 a) D7 i3 H
       Map<int, string> mapStudent;
1 u% j* G3 @9 h" J3 q% d+ E  XPair<map<int, string>::iterator, bool> Insert_Pair;
6 X3 ?# S) q; ~7 `       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));! o) l! ]6 q0 B7 {  ]4 I
       If(Insert_Pair.second == true)
7 J. d! l0 ]+ m: N# ?* m; ?8 \' _       {4 X8 I* M6 Q- H- U! W2 Y
              Cout<<”Insert Successfully”<<endl;4 n0 o; i2 l& w0 i+ @# s
       }' x7 ^, T1 V! K- F) ?2 ]$ ?; }& _
       Else
, R0 }8 j2 Y  f* s$ l       {3 b4 H0 C( C- q- S9 x4 P) h# Y
              Cout<<”Insert Failure”<<endl;1 u' z8 T* N5 S2 s: L* }/ A% h
       }
# v  e# O/ }; Q8 ]" O! O
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了