|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
1.map的构造函数- }9 u F' N3 Y4 |- d; G
Map<int, string> mapStudent;% Y# a v6 q B: @/ I L
2. 数据的插入# y9 u* U$ d! I& I) d- W0 t
在构造map容器后
# `' b9 B! h! R1 z1 k第一种:用insert函数插入pair数据% @7 ?3 `" X' F, F7 o# i
#pragma warning (disable:4786) )& V: W3 X, ?6 K6 L0 o# B, T
#include <map>
; Q, u5 | g" `4 k/ G- Q% E) F8 e. h$ n" b#include <string>9 e% B, N4 t P
#include <iostream>$ R; g+ b$ [+ p2 K! z
Using namespace std;. E& p: A$ N/ @- ?1 Q
Int main(): I" y2 Q* `/ `5 [' o7 V
{2 s. }" k8 s8 Y7 _
Map<int, string> mapStudent;
3 y" [ {0 R" L s) [* d mapStudent.insert(pair<int, string>(1, “student_one”));
, R9 A! ?! b+ O; Q+ P0 X mapStudent.insert(pair<int, string>(2, “student_two”));, M* t4 ?* N; E
mapStudent.insert(pair<int, string>(3, “student_three”));/ ?; n' L- s+ e6 K4 `: T
map<int, string>::iterator iter;
1 R& ]' Y+ z; |8 s for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
# ?( n! r5 z# _8 q! u- l* s{: N9 T1 z4 S2 N1 t
Cout<<iter->first<<” ”<<iter->second<<end;
+ B* x) y# r& i' q% \1 [7 d}3 d+ a; E" ^9 s: c; k
}( n: m; S# C, T5 X% Z
第二种:用insert函数插入value_type数据,下面举例说明0 C. n9 ?0 g! B8 o! @
#include <map>
( m8 ]7 S4 L5 [; t3 s( ~#include <string>- c( y B4 `( {, v' F! u
#include <iostream>) ^* g! z5 z1 U. a
Using namespace std;
3 V7 p. L4 l+ m, G9 kInt main()8 C- L8 R4 I$ N7 B+ D. x
{
( B) H f7 z6 |3 D D6 @. w9 U! H5 y! C Map<int, string> mapStudent;
6 A0 x$ y* g. V4 s A/ W# F; k mapStudent.insert(map<int, string>::value_type (1, “student_one”));
- t' o6 M' G; |, A5 O mapStudent.insert(map<int, string>::value_type (2, “student_two”));* D! x5 K8 G6 s5 u
mapStudent.insert(map<int, string>::value_type (3, “student_three”));2 n9 {9 b' p1 Z
map<int, string>::iterator iter;" H6 [: \9 x: M9 _8 Q( w
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
) a, [# m% ?9 j8 C1 Y{9 V9 g* L" m) L: y8 l* t) u$ u
Cout<<iter->first<<” ”<<iter->second<<end;
0 `# @. ?. U6 h1 `! w}, t8 {8 {' U% V! T) P
}' _6 c4 W) s2 h& y0 ], C$ ^- f+ |
第三种:用数组方式插入数据,下面举例说明
/ l# C9 B* I3 h! ^! m$ N#include <map>
- \9 Z$ k1 e0 X8 @ Q4 G$ U#include <string>
. I, \' p' F- z" |5 H) t#include <iostream>
+ _1 |6 w4 `3 MUsing namespace std;
! H" A0 v3 ^7 k, V6 B FInt main()( j D/ I1 L0 m; `9 a$ y. V8 ]
{) I- }5 s/ y/ w$ {; s. U7 k
Map<int, string> mapStudent;4 `3 G( d! X2 ~/ {5 x
mapStudent[1] = “student_one”;% I6 R- F5 w9 g$ u
mapStudent[2] = “student_two”;1 g8 g9 ?) w+ e" P/ j7 n
mapStudent[3] = “student_three”;
! ]' K3 _; G( ?. }; \9 V" c map<int, string>::iterator iter;
* G9 _% P7 @4 w- q7 w9 A for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)) c& H/ L- v6 H6 M3 {# w1 `: |. I2 b
{
% U0 g* e& |1 o Cout<<iter->first<<” ”<<iter->second<<end;. d u0 Y/ X) @% F
}
! I; i2 P" h; A1 k; ]0 M}8 t, Y+ m/ ]" O2 D
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明6 w. ]$ y- o* z) H0 n# ]
mapStudent.insert(map<int, string>::value_type (1, “student_one”));7 w b0 M v( J& l$ D
mapStudent.insert(map<int, string>::value_type (1, “student_two”));! L- h% x6 Z4 Y* E
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
+ \6 f' \, ^% b. E5 c0 F7 `Pair<map<int, string>::iterator, bool> Insert_Pair;# T0 a7 n6 q, n& [2 s6 J7 Z/ v+ A
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));! i- ?/ M* J; C& _& |3 a
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
$ e+ S {! U O, Q* h) u下面给出完成代码,演示插入成功与否问题- j# S: j+ m$ @3 n9 L: [
#include <map>
9 }. o. j5 L7 G8 i# ~#include <string>% L3 G& e) P+ f2 ~
#include <iostream>
2 U+ e4 |4 n0 M# T) U! @: fUsing namespace std;, n o- o1 p$ c' f8 C8 T
Int main()
0 V0 B8 D! D: |9 o! t{. E1 Z# y2 Q$ e5 b
Map<int, string> mapStudent;. Z0 c% x8 G6 S Q3 A
Pair<map<int, string>::iterator, bool> Insert_Pair;
3 [+ m; T% k8 P; ?6 e4 H$ h* X Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
5 [+ r/ O! ~; @; l If(Insert_Pair.second == true)* Y# ~$ Q( K- l7 D% E
{+ s) I" w* W6 b7 h9 G' L
Cout<<”Insert Successfully”<<endl;
8 A! p2 y; N8 A7 v }
: E1 C1 q0 ^; D4 o: V7 {$ z Else
6 } e* ?- d* d+ C {5 V7 N& K7 p, ?! d+ S" Y
Cout<<”Insert Failure”<<endl;) ~5 h4 o- |3 r0 U2 Q- J! E& t, w
}
5 c$ V8 h; I, f |
|