|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
2 `% g1 {* ^- ~7 }& r0 \Map<int, string> mapStudent;
' f, g/ q6 V0 F* g1 N2. 数据的插入* d% W6 I9 M, `
在构造map容器后/ Z! Z8 d% T; X" Z
第一种:用insert函数插入pair数据' _& d! H6 _- i* |3 J
#pragma warning (disable:4786) )
2 P0 [0 c5 a% M4 E. [! m#include <map>) d& b* C+ b- p
#include <string>
I$ M' {- U$ M#include <iostream>
. f5 l4 U4 k8 `Using namespace std;3 f$ J+ x. C0 G. H+ D5 p
Int main()5 I( r0 C* e" `# a ^
{
" @. ^0 V* l' S( R( D g1 o1 ^. K Map<int, string> mapStudent;
3 g! @- w- R5 Y' b0 n, Z mapStudent.insert(pair<int, string>(1, “student_one”));
) r3 r: j' k% ~- I mapStudent.insert(pair<int, string>(2, “student_two”));
+ I* M; V% K: y, j mapStudent.insert(pair<int, string>(3, “student_three”));* c4 _# E4 f" h3 N) k' N+ _
map<int, string>::iterator iter;4 t9 P% s m6 i3 Q* t
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)" H0 y1 ?: c% w! M: v8 R. V: |
{
; B9 D! V7 `! s; G0 i6 L. r, [ Cout<<iter->first<<” ”<<iter->second<<end;2 l1 h l- ~6 W/ p* M
}
5 f+ V) h$ R' b4 D1 s}+ O8 g+ ~/ n* ? w
第二种:用insert函数插入value_type数据,下面举例说明2 d* Q. \, ^$ l8 z& X
#include <map>& E" [) ^9 g. p) r6 q+ v; u( e# x
#include <string>+ X: z8 K; `4 W# Z- Q
#include <iostream>
; q5 U$ M% z6 l+ h( SUsing namespace std;
& I' `' s! U! o4 C vInt main()* a# l7 O6 m G% u3 P( U w9 x9 \: o
{
w5 Z# l# h, Y& ^3 ^3 K Map<int, string> mapStudent;) n8 V* h! R6 }( @
mapStudent.insert(map<int, string>::value_type (1, “student_one”));6 ]' q: I( t! t" \1 N. ~- c
mapStudent.insert(map<int, string>::value_type (2, “student_two”));* X- v7 u& Z' s8 r: j
mapStudent.insert(map<int, string>::value_type (3, “student_three”));2 J: ^0 u4 T4 a9 y
map<int, string>::iterator iter;+ _: E% g( N% H( M: r1 g
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
8 Y: @/ ]) K" d6 m{
$ z* f, M) y6 i Cout<<iter->first<<” ”<<iter->second<<end;
+ i& J+ K3 V1 `0 I1 P' n% _3 F}1 u5 R! o! M" V7 x# W. O
}% {; h* B5 p: O* I
第三种:用数组方式插入数据,下面举例说明
" n( \" t9 t. z2 G$ T% E* P' y8 M#include <map>
+ x" b1 Q! N2 G* }5 W+ [3 p#include <string># W2 \4 O5 J2 h1 f- H
#include <iostream>2 ?1 _) Q5 I3 D2 f+ p- d
Using namespace std;
7 ^ P- J7 |/ Y. g" ?5 d! pInt main()
% C3 _6 a; r1 g% ?* s, M{8 h f1 I- E7 V; u& K4 P
Map<int, string> mapStudent;4 U" h" O2 l- T y4 G, @( J
mapStudent[1] = “student_one”;3 y" X8 L- [% G& ~# W' O3 i( H$ S
mapStudent[2] = “student_two”;, X/ M8 |( W- g. {0 t
mapStudent[3] = “student_three”;6 E/ Y; u( F3 }+ o
map<int, string>::iterator iter;4 a9 b0 Y& c) v
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
' W' {/ t, k1 Q6 j# R{$ F3 n& N6 |6 P j5 O
Cout<<iter->first<<” ”<<iter->second<<end;% ~: ?: \# L( s& G4 Y9 Y
}8 Y: ]7 Q1 f! T
}! f* {1 W! f9 F! w; U
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
# d/ G* F: t. ~& [$ c& pmapStudent.insert(map<int, string>::value_type (1, “student_one”));
+ R& G V, n- l# }8 m; _, GmapStudent.insert(map<int, string>::value_type (1, “student_two”));9 V0 K f) d3 N. B z7 w
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下, N2 f+ f4 M: [* H% e& K
Pair<map<int, string>::iterator, bool> Insert_Pair;0 U& r1 J6 v- u5 N1 k% T
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));5 D! Z$ T' K* u
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
2 ]) F: t# x* \下面给出完成代码,演示插入成功与否问题: L2 g4 h5 `4 l+ m
#include <map>
|, S9 l) k9 y8 `" R. W# P& |#include <string>6 Z Y) y$ L! i; v
#include <iostream>" v1 |! x9 T( F% I& Y9 H! e! J
Using namespace std;
- S3 Q+ Y6 I D: Q3 I9 RInt main()- _- Q0 P! V3 s2 l
{5 }' x6 g5 P/ [8 `' _5 K3 Y2 t
Map<int, string> mapStudent;/ d- @, s* z& i; i% x7 {- W& e5 Y
Pair<map<int, string>::iterator, bool> Insert_Pair;4 U# ~3 P' L0 Q$ X
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));! d' a2 b8 p5 j
If(Insert_Pair.second == true)
, k7 ^ D$ l3 Y5 @' @% v {
0 F9 L9 w- N* d. N Cout<<”Insert Successfully”<<endl;1 k* `; H2 K6 `/ P, Q
}
3 z: l+ M5 G+ x/ { |% y Else5 V3 N# P0 t* F" F; ^" R8 E" ]
{
5 K) q7 ^* x/ ? Cout<<”Insert Failure”<<endl;% v( h! Q- m7 Q( }
}
: _- c' y$ n( e; P% P3 @ |
|