|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
8 S3 u P7 ^& Q7 @1 @1 w. JMap<int, string> mapStudent;2 l9 G+ `- E' G5 H
2. 数据的插入" J! H H; K6 n
在构造map容器后1 ?5 ^3 g; O2 F7 l
第一种:用insert函数插入pair数据
5 |% y: ^8 U- L9 i- ^+ d4 e7 w#pragma warning (disable:4786) )7 n1 X3 d8 q2 }& {9 g" L
#include <map>
* c& G* f, n- A6 ^#include <string>
1 t f$ A; j" _" R2 F8 |3 g#include <iostream>
& D: g Y$ b% J/ Y0 kUsing namespace std;
# D; @1 @& ^! F$ R j- ?1 ]1 UInt main()
. F* m! k% a' Y2 V2 k+ h g6 E{% z6 y. X& \7 P$ J! ?0 @: A$ }, N; D
Map<int, string> mapStudent;+ E4 f3 V% z: D; c/ d# \; l* a
mapStudent.insert(pair<int, string>(1, “student_one”));" q- _: G+ ?5 T* X0 _( S) A
mapStudent.insert(pair<int, string>(2, “student_two”));9 I9 ^3 D0 ]+ z5 F
mapStudent.insert(pair<int, string>(3, “student_three”));
$ \8 D# W: P; j i; j map<int, string>::iterator iter;) G9 B2 o7 |/ S* n
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
& e/ Z" @- O- H- m2 W! j' a/ C# x{
: u& ]. o+ E3 \ Cout<<iter->first<<” ”<<iter->second<<end;
; I. a0 P# A9 B# b( r: ~+ o# a}& U: d# u0 A; l8 F+ u& M, S
}
2 U" n/ b! f1 ]! M第二种:用insert函数插入value_type数据,下面举例说明
: H8 V' ~* _2 D#include <map>9 e& S9 X5 v ]4 _( ?
#include <string>
) U! Z* Q1 }1 Y5 L#include <iostream>
8 U& n! `$ r! ~0 ?Using namespace std;$ U/ w% {4 I9 n% J# F* s" v7 U2 s
Int main(); ^3 }5 x8 V" C, Q, l+ C
{
+ Z1 \+ `, ^6 [' \7 Y- n+ Q Map<int, string> mapStudent;
+ T' b6 ?) y) Y1 J& n1 { u6 g5 t mapStudent.insert(map<int, string>::value_type (1, “student_one”));
. q. p K3 o' d) ~# \' i mapStudent.insert(map<int, string>::value_type (2, “student_two”));
1 J9 H- `" `; {/ W2 P mapStudent.insert(map<int, string>::value_type (3, “student_three”));
5 b) u7 g9 d. z% S* x! y" X( g map<int, string>::iterator iter;
) c% R1 ~% m6 S6 \; J8 ~0 N for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
+ |0 {" \1 F" q. ?0 v( x{( m: k. C4 {; \. B# l6 e E0 ^- i! m
Cout<<iter->first<<” ”<<iter->second<<end;$ V% X/ j* h0 G" Q5 d% u
}5 g8 K0 ~, E8 O3 m( i/ l
}
( P4 X( L% ]( V* D: i# q2 o y第三种:用数组方式插入数据,下面举例说明) ?* @- t! E% f d0 b8 `& L
#include <map>
; `: U* S; N" A& h, n#include <string>
7 y5 ^ C1 R5 h% ?#include <iostream>
+ X# w7 K7 K0 p5 n! h; s& n5 UUsing namespace std;
! V/ |- F6 O# {Int main()
, \! r! K: Q5 f; g0 j+ L" ?+ c{
! t" q6 b- J* V& s1 R Map<int, string> mapStudent;
$ A4 ^' ^, N2 f0 _ mapStudent[1] = “student_one”;% D: c7 \$ Z7 t! y
mapStudent[2] = “student_two”;+ `6 r: Y1 e. {/ Z
mapStudent[3] = “student_three”;
- c0 x4 ~2 j' K: ^% ?! S map<int, string>::iterator iter;
5 k$ [ y l2 Y( R) a/ @ for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)0 q% R" ^5 T0 ]; C2 W! Q
{3 u/ b9 ?" @9 g& B5 W& B
Cout<<iter->first<<” ”<<iter->second<<end;# [ V k# Q* b* G" Z
}
( s5 r2 D0 |+ [; k/ |9 V/ a# ?}5 Z! J8 X' X6 P
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明; F9 M- b0 q# \: D
mapStudent.insert(map<int, string>::value_type (1, “student_one”));5 O: K$ w/ u* Y- y: D
mapStudent.insert(map<int, string>::value_type (1, “student_two”));0 ?$ l) {6 Z% h1 k0 ^7 l
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
9 b4 l0 n! ^! k- X4 \; nPair<map<int, string>::iterator, bool> Insert_Pair;
/ v% O% c& Q- bInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));; w4 L" [1 M) n( L
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
; ~6 K+ @" N# p* V7 B+ C& U4 B下面给出完成代码,演示插入成功与否问题
' o# n0 R" _9 V/ x, [' O/ a* V! H#include <map>
2 j6 F& }% q+ ~* y+ T* m#include <string>
7 q* D7 H- p* |1 D9 E8 T$ x/ E( k) G#include <iostream>8 [ i8 s8 N |# A' h4 S* b7 ^$ r
Using namespace std;
2 k `. \7 ~5 ]. ?4 jInt main()
0 s& h1 }, C/ c$ m{) w9 T, {* V' G' p# _ M
Map<int, string> mapStudent;
: F8 r/ d5 z9 K" ?0 xPair<map<int, string>::iterator, bool> Insert_Pair;& d7 m* {6 b- Q$ w! ~
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
5 v7 l& r# ~. S) H8 A H( h% h* I# e2 @ If(Insert_Pair.second == true) t4 T7 P, K/ e" _( I
{- o2 U. w3 Y" m Q
Cout<<”Insert Successfully”<<endl;
: u# B- _; Y+ x8 |- D5 c }/ y0 k) k+ G2 o3 B7 @3 F2 y
Else) c- T: b' o& M/ i+ V! j
{: ]; J6 J; ~ X- d% `
Cout<<”Insert Failure”<<endl;2 o1 T7 G& _$ q/ M
}
j6 g" f6 W! ~5 Q& c |
|