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

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

[复制链接]

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

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

mildcat 楼主

2014-1-3 19:37:45

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

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

x
1.map的构造函数
1 s( D, X& ~% O% ~, D5 h% DMap<int, string> mapStudent;
0 n7 r' [, y- n0 U2 B& d. g2. 数据的插入
& n. |  a+ E  F8 I2 g) D在构造map容器后" T8 C' O4 h' q2 a- l, A
第一种:用insert函数插入pair数据& k, U/ a% o. \$ x. A# [* I& U/ \
#pragma warning (disable:4786) )# d5 f1 y# z0 R$ R- l
#include <map>
% m! X7 G+ Y, a9 q& z. a+ j! c#include <string>! Y; u3 y2 y9 g9 |2 ]
#include <iostream>
# N% S, k8 X* Y6 z/ H6 s( m" \Using namespace std;. }- k* m7 f$ A; N. V$ _4 K
Int main()# x1 f* }$ B* C; |8 M
{
0 `( K5 X# p* {       Map<int, string> mapStudent;: C- z  v6 p7 k6 Q( @& d9 G6 H3 h
       mapStudent.insert(pair<int, string>(1, “student_one”));- T7 P& G8 P. C2 r
       mapStudent.insert(pair<int, string>(2, “student_two”));( |7 Q. X& A% L$ |! u0 O  {
       mapStudent.insert(pair<int, string>(3, “student_three”));  ]$ S% `6 T3 c% l5 L1 C! U# w
       map<int, string>::iterator iter;, \  ^7 r3 u6 h3 ^, E
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)% w, T3 X% V* ^2 P
{( a/ g% I# N4 O: }5 M1 C6 R
       Cout<<iter->first<<”   ”<<iter->second<<end;
7 k- s) k) K% N; [7 C, O}. X5 F. z3 ?8 _5 ]
}" ^6 C( @$ c" u( n+ u
第二种:用insert函数插入value_type数据,下面举例说明
( ^" i, K7 l( ^) D3 s#include <map>
% Y5 o1 K" y+ M#include <string>: {+ c8 k: |! u  ?$ ]: \
#include <iostream>+ H0 \! v0 g9 Z0 m/ F. I
Using namespace std;
# t$ v4 j5 @6 @Int main()+ j; R- |* P+ _  o9 b
{+ E+ x. e: ~) Q/ T" F, n
       Map<int, string> mapStudent;
$ v6 K# @/ a3 z' F) q7 C       mapStudent.insert(map<int, string>::value_type (1, “student_one”));7 V+ J/ }  e5 n5 |. M! F
       mapStudent.insert(map<int, string>::value_type (2, “student_two”));' r5 w- l! `+ j( x/ [. `
       mapStudent.insert(map<int, string>::value_type (3, “student_three”));
; w" K- n3 H1 w! b9 Z, P& I       map<int, string>::iterator iter;4 A1 x8 j. F5 `$ g
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)/ K7 S' z+ Z+ g. ~; X% B2 {! m
{% D" b4 y: b0 Y1 g7 q4 K8 b
       Cout<<iter->first<<”   ”<<iter->second<<end;
2 ^8 m( y8 S* t4 ~& J: }: C2 y1 [# x}
" z& F0 Q, M% K# T+ L& J( c' [}
6 e' e) Z+ T' b. r第三种:用数组方式插入数据,下面举例说明
* @+ Q7 U# k1 |#include <map>, t) u/ B& R! y# `  R" D
#include <string>; N8 {5 V3 E9 }! b7 U0 r+ |
#include <iostream>
2 f" k7 d( ?/ T; WUsing namespace std;8 K1 P) c* `; ~
Int main()
6 M" U1 ^/ ], X' |) j{
& e4 K8 t! _  e) j& u. S) {       Map<int, string> mapStudent;! Y5 D" ^5 R1 ^0 v% Z
       mapStudent[1] =  “student_one”;# Y8 _& T0 A1 F+ K+ D6 \
       mapStudent[2] = “student_two”;: `* O2 ?8 `) a' m. J" I$ e
       mapStudent[3] =  “student_three”;) a6 v! t7 G$ V
       map<int, string>::iterator iter;7 |- W' P' v3 k- z5 [
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
2 j, T+ m" i. X  X& G6 h! F8 \{
3 b5 u8 q) R* i2 |# o       Cout<<iter->first<<”   ”<<iter->second<<end;
4 A0 \* x( d0 Y4 K3 j' }& y}0 `  z% D( F4 I! r* U
}
! G5 s- x7 g1 P! H$ V! t; F以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明8 n! U. Y7 Y& n: k
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ m+ }: A( J/ T- b! ?5 f. }4 _mapStudent.insert(map<int, string>::value_type (1, “student_two”));
7 N7 n7 k4 M; ]上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
: [1 t! }/ u. sPair<map<int, string>::iterator, bool> Insert_Pair;
  O, d2 ^9 u! O( d6 x) wInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));( G' }! S; X' z4 g4 Z4 E6 ^# f
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。$ g, K: W' K0 F- b1 T
下面给出完成代码,演示插入成功与否问题! y5 E+ d+ u- P, S
#include <map>. Y2 g$ z. d3 u0 h# O! U& a
#include <string>
$ j0 i' w3 u. O2 ^" b6 o) r#include <iostream>' G& B6 P1 L# [& |1 D8 U# q
Using namespace std;
9 _/ I) D7 q# \Int main()
( T: C1 ~- h# |{$ j5 c6 b8 Z& G( t0 R* {
       Map<int, string> mapStudent;. [: `% s& Y/ F' ~1 }
Pair<map<int, string>::iterator, bool> Insert_Pair;4 t1 R: Z' D4 R( a' g  k1 Q
       Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));7 c; v. f( q5 O2 t) Q" C
       If(Insert_Pair.second == true)7 T1 k3 \. P) V5 d% v/ o6 y
       {
& V( j( P. j2 S8 z$ ]$ T3 d              Cout<<”Insert Successfully”<<endl;0 `/ z3 g; {* T7 D; V7 A
       }; B" w5 F. x4 x" b; P% t
       Else/ b$ S5 ~: Z) _
       {: J/ k5 `+ U& ^) d* w2 N# u- I
              Cout<<”Insert Failure”<<endl;9 E* M! {& N9 O! n. |! S9 r. g
       }  M7 C& x4 ~% q: |7 v# z
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了