|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
* u- P# Q4 A" t; D7 Y7 ~Map<int, string> mapStudent;2 S' Y2 a7 o6 s" p+ `* J
2. 数据的插入
* g8 o7 @6 v5 K. a$ P在构造map容器后1 `. ~' S/ z2 \6 z) A9 P" |! D
第一种:用insert函数插入pair数据
, Z/ t4 b1 W8 g Z" _& @- c#pragma warning (disable:4786) ), K/ G* F% @3 M0 k& U& z
#include <map>
9 _8 P) U6 K* ]. @0 Y" K#include <string>& k3 v1 m7 t' }0 g6 Y' Z
#include <iostream>
2 s0 p- s% J' [4 ~5 `. G" @9 jUsing namespace std;% D! p; }5 K' G. A7 t
Int main()- ` D3 U, M7 v: B b: }# P
{1 e9 g3 ?& ~; h
Map<int, string> mapStudent;
: B) a1 a( J0 J8 I* ]( D* b0 ` mapStudent.insert(pair<int, string>(1, “student_one”));! M( @( i3 T1 D2 [6 k5 N
mapStudent.insert(pair<int, string>(2, “student_two”));% X8 o o% e* V- s9 ~
mapStudent.insert(pair<int, string>(3, “student_three”));
8 u5 `% d) T K map<int, string>::iterator iter;
/ z& _. p g e7 I for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
, }7 c; ^6 A% ^0 h{
7 d# s0 D F4 |3 F- E Cout<<iter->first<<” ”<<iter->second<<end;0 N/ B% `, `# W3 I/ C
}* U# i! u& D; `) u8 Y3 d: T5 {
}
% [2 ?' K1 _/ u% M8 W5 @, ]第二种:用insert函数插入value_type数据,下面举例说明, h$ s$ `: K* \* ^; R# Z: b9 E
#include <map>! ~. w* X! Y) i2 @# H" B/ I
#include <string>) {$ l; ?( y8 u- Y/ Z5 E1 d6 [
#include <iostream>1 V8 n. G( b% l- I0 I
Using namespace std;; M# t% X# M; ~) h+ V' i
Int main()* D( _5 O: a/ L3 D) T5 I7 Q5 L r, f4 }
{
* G1 A6 i0 m& U) W Map<int, string> mapStudent;
1 Q. i$ [3 ^. |7 @5 B mapStudent.insert(map<int, string>::value_type (1, “student_one”));
" @1 q I+ ?1 p# m6 {' ? mapStudent.insert(map<int, string>::value_type (2, “student_two”));# c3 l9 F6 w6 W( J+ J+ i) Y( e
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
, S% e4 d1 l& L% _% k9 Y map<int, string>::iterator iter;5 Z& O7 |/ | T& G) y d
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
+ B" |3 I6 n! I" a7 X3 s+ Q) _1 d{9 T) l p, V. Q% h3 @5 P" m' q
Cout<<iter->first<<” ”<<iter->second<<end;
6 K& X* i/ f( Z# v}8 B# W- S. a) f' R/ R/ ~& X
}- N3 C5 i" \/ N) K [7 G7 R& M
第三种:用数组方式插入数据,下面举例说明
5 Q3 R; r3 h/ ~; A0 p+ Y8 v#include <map>
- ?0 `& g2 b: G% w* p+ W' W#include <string>
6 u. [/ d4 ^3 O8 X#include <iostream>
; }7 [( A% v3 A; yUsing namespace std;
% Z: i. w0 Z4 LInt main()
7 k5 d/ H# g" ]5 p5 F{
& n( c5 y7 B+ a! N6 `* ]/ @1 r Map<int, string> mapStudent;' n" C4 g, w+ P( _6 Y4 e( o1 l$ Q
mapStudent[1] = “student_one”;
% ?6 B3 Z9 h; |# e% g" b% v mapStudent[2] = “student_two”;: m- R8 u @1 H
mapStudent[3] = “student_three”;
. P: a3 S, F' |5 c; L: V map<int, string>::iterator iter;
# m' \' U* [0 ?" G- r for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)& N4 P9 K2 i. ]4 G/ {3 p
{
) U; p! f; V9 w4 t4 ] Cout<<iter->first<<” ”<<iter->second<<end;
& ]/ J6 z$ o( }& B8 v8 ^" m}
: H/ I( r" a0 S, k" u2 l+ `# R}% L" x+ |: }) L( n; Z
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明# i% {2 P! u2 F2 T0 Z* o9 E$ s9 _
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
! G7 Y" F# I% |mapStudent.insert(map<int, string>::value_type (1, “student_two”));. T% h) P, k, R2 Q9 y
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
p {3 x* u2 U; d: nPair<map<int, string>::iterator, bool> Insert_Pair;4 c7 P! c5 J& N& Z8 X
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
" N. k: F) U8 o9 j我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
* X. |2 I% r; I1 E下面给出完成代码,演示插入成功与否问题; B. H9 \, P: C: J$ ?! U: V9 n
#include <map>
! L' P! P7 Y. m' k% w#include <string>- y- O4 V7 u! ~0 C# z' T' r+ y, `
#include <iostream>
0 P t; I! p5 u, VUsing namespace std;: Q3 V$ w, @5 d4 C: R( a
Int main()
* B* n$ N3 M) }7 A r{
r$ ]6 U, T% I7 o Map<int, string> mapStudent;
# z$ R/ S" d5 ]- q: D( @5 qPair<map<int, string>::iterator, bool> Insert_Pair;3 O" P. l8 a p- ~; q' k
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
! r$ m O3 m7 S$ v If(Insert_Pair.second == true)/ _7 D6 V' H( |% o3 T0 `2 U$ [6 A& |
{1 ?8 T: L F# J5 E: J
Cout<<”Insert Successfully”<<endl;: _/ c' x6 E* l/ B. @: C' y
}
9 R% ?1 [" Y4 f& l( H; ~ Else8 t0 I7 y+ d% y) @3 _
{+ [& F8 ~, c- ^ ] K7 y9 d+ @. o+ j
Cout<<”Insert Failure”<<endl;. l1 @4 S, z2 b
}
, k, N3 c3 O; j0 P0 I2 `7 D |
|