PLM之家PLMHome-工业软件践行者

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数2 C  K/ U& y0 N! D, w/ v' B& T; l
Map<int, string> mapStudent;5 z1 r2 g5 C, P1 [" _4 A, H
2. 数据的插入
1 \- `% S# v/ V在构造map容器后
! F' n* ~4 o) \第一种:用insert函数插入pair数据
; H' w" A! S1 }; K& _' D#pragma warning (disable:4786) )
+ _" s6 X; g; }#include <map>
9 H7 X5 c7 v( W) v- N4 e#include <string>
; [9 i' u& y1 N#include <iostream>6 e3 J4 M2 h$ q) l" s3 v7 r
Using namespace std;
0 O# y- E. L( ~; z9 y, jInt main()# L6 L2 O; @! P
{
5 j" ?  `0 U1 |' u7 Q7 b! z       Map<int, string> mapStudent;
0 k4 T9 {+ P: d0 W5 x5 e       mapStudent.insert(pair<int, string>(1, “student_one”));
3 s6 v3 d& ]9 m" A       mapStudent.insert(pair<int, string>(2, “student_two”));
5 A3 m; `# w1 ^- ?- ^       mapStudent.insert(pair<int, string>(3, “student_three”));6 E+ `) o4 f) A( I6 K
       map<int, string>::iterator iter;
' q* p$ a! E' t       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)& s7 I! D; q) y: S9 |! i
{
/ A) n' f3 N+ G; M  ^       Cout<<iter->first<<”   ”<<iter->second<<end;8 ~+ J' V6 b' m! |/ Y3 V$ X
}+ Y- Q: Z+ J  g- E; |# Q; k
}
% ?) Y. O9 E' o! r4 c8 V% ?5 s3 @第二种:用insert函数插入value_type数据,下面举例说明; T  `3 @4 t& c; p
#include <map>& p" K- Q" d0 Z6 Y
#include <string>
2 J- N. e9 g' s#include <iostream>
/ Z* ?) \# O+ s7 v  b. oUsing namespace std;
7 Q" s! `" E8 |, hInt main(). A/ B* Q( I2 X' e
{
+ `1 y, j% h& [  f! F       Map<int, string> mapStudent;
* q- \5 s: C7 p9 H       mapStudent.insert(map<int, string>::value_type (1, “student_one”));) e6 y( \4 ?) Y- ]( ]+ ^
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));
& f4 \! H7 d0 Q* W. V/ K- X       mapStudent.insert(map<int, string>::value_type (3, “student_three”));, W6 q6 Z; Z2 G5 x7 k
       map<int, string>::iterator iter;% j7 t& J2 Z- b
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
1 @' G! N% |% t{1 m" u- ?- P; x2 U
       Cout<<iter->first<<”   ”<<iter->second<<end;
- J: v' a0 r7 m; Q& I3 j* e}# A# M7 I1 X6 o, A! H$ G! s4 ?
}3 W/ Z& J2 j& O; p
第三种:用数组方式插入数据,下面举例说明
3 e7 y5 u% x0 m* `& h' _6 [% `#include <map>- c4 d5 ~! d! p( s
#include <string>( i+ s8 ?- k- H
#include <iostream>
! Q' @0 _( V1 r* H6 J. \) l" \Using namespace std;
# Q$ v) ~) X8 g: wInt main()
, x# ]) v0 e; i" b6 C. ?) k% T0 p8 e{: P& M" }8 k8 O1 u
       Map<int, string> mapStudent;) F7 Q  _$ d+ @
       mapStudent[1] =  “student_one”;% j/ X1 b4 Q$ I( s. @
       mapStudent[2] = “student_two”;
6 Q$ m, j9 V# x: `; f: l. _       mapStudent[3] =  “student_three”;  c$ G' j+ k' z, A) z
       map<int, string>::iterator iter;
5 m+ X: y  K$ v       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
$ A9 ^! [' q7 e: `# B8 V5 y{
# U0 N; |2 A% r) N       Cout<<iter->first<<”   ”<<iter->second<<end;  V: Z: B4 f/ ^% d" h
}) y/ o; T" O9 [; F+ x  M
}, E0 G4 b0 J4 e  b. w
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明, c+ O0 ^/ p  _
mapStudent.insert(map<int, string>::value_type (1, “student_one”));% K/ F6 j$ I& k+ p5 J9 S7 A' a$ z5 p
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
: [: x, S/ }5 L  n8 Z. P上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
: I7 i: n/ z. ~% o1 cPair<map<int, string>::iterator, bool> Insert_Pair;
/ Z: M6 E$ G, ^3 ~Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ ?9 a  T3 g% Y. h我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
' _9 P8 o! U0 Q( O9 X& `8 O下面给出完成代码,演示插入成功与否问题
$ d# q) u. Z. _( X#include <map>
0 [; C- [2 C2 X& U' S#include <string>% x8 ~& v4 T/ _( u7 J$ N/ l
#include <iostream>
2 \- R- F6 T% C8 {9 K& MUsing namespace std;" n  K; H3 B) f' K4 f, U  k
Int main()8 c8 B0 r" L: S8 H% C. U
{
/ X) E$ D4 t0 i       Map<int, string> mapStudent;
  C# P8 Q7 [2 Q* Q( ?5 tPair<map<int, string>::iterator, bool> Insert_Pair;$ K; b: `1 Z) U2 N7 |" ?
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
9 Q0 U% c$ x1 R: v0 s8 K, L       If(Insert_Pair.second == true)6 c: t) }( `  e7 D4 n
       {
5 S3 H8 t) o  B              Cout<<”Insert Successfully”<<endl;
6 o$ u* T4 P) t! b1 Z. M, v       }" C5 Y+ E* b3 c' o$ C" @( v
       Else
2 b* }* {2 `3 B( F( j       {
# K" F; n) ^( a4 j              Cout<<”Insert Failure”<<endl;
- G! }! q5 e  {# h2 f3 ?, c  ^& S       }( w# o% d2 E9 H( p  {- C0 \! q
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了