|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数, Z* Q5 a7 U* W j3 J2 Z
Map<int, string> mapStudent;9 k' \7 M3 E& ^% G7 w8 |
2. 数据的插入1 {+ @% a6 ]1 E; _% [
在构造map容器后
2 j9 R5 F: c. U3 V第一种:用insert函数插入pair数据# h, S" @3 M) o p {# @
#pragma warning (disable:4786) )
; v" ]8 ^8 Z( B#include <map>
4 L- s4 U4 G, f#include <string>
8 ~, e; K ^8 S#include <iostream>
7 |1 Z, t' @: A6 P( j0 iUsing namespace std;
8 g" v" _8 B8 K* zInt main()
) Q; x- m2 o8 k- `2 ~/ ~8 r1 n* F4 L{
9 a1 B& \, ~1 f- o Map<int, string> mapStudent;
1 o& J: Y, O, d2 I mapStudent.insert(pair<int, string>(1, “student_one”));
& O( n9 {& O8 l9 @ mapStudent.insert(pair<int, string>(2, “student_two”));
& a% Z" n5 G) w mapStudent.insert(pair<int, string>(3, “student_three”));
% _' Z4 X, }6 U) S, k$ ^ map<int, string>::iterator iter;
) I) p# R+ Z$ F" y for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)! \+ P: ^+ s+ T& ^0 J' G- B8 o
{
2 c% T3 Q8 ^8 C/ h: w2 Q' k Cout<<iter->first<<” ”<<iter->second<<end;" R# R) J8 R; ?/ G
}8 d, a; _2 P4 K
}5 p9 e! a3 }# j; e- J
第二种:用insert函数插入value_type数据,下面举例说明
. V# u5 t* H( f- K8 q( N, n3 J) c3 v0 f#include <map>: {2 q+ y9 f6 Y) {3 F
#include <string>! z% K4 q: H8 n/ z' N! }
#include <iostream>
0 J) {3 c- Z5 N( E7 z/ dUsing namespace std;7 c3 ~7 q! K* s! x5 w% x, x/ C
Int main(); @# Z) U( Y& H# Y+ Q
{
& a* W0 |3 r n" T5 f Map<int, string> mapStudent;
; A' C: G# n0 h" t" F6 z6 i3 g. z# y mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ E% a4 S/ y1 A% g- y; L mapStudent.insert(map<int, string>::value_type (2, “student_two”));
$ \) n8 w8 e- Q( ` mapStudent.insert(map<int, string>::value_type (3, “student_three”));) ^ K0 b) G1 T' E4 h- L2 r
map<int, string>::iterator iter;
- C: a8 G3 d7 k' q$ H$ d for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)* R+ X4 h4 b- @- [4 A% K, F
{2 b* H: D$ k2 l+ A9 w
Cout<<iter->first<<” ”<<iter->second<<end;
- @. c# H: } Z/ c) Q$ \1 |}& _& @1 f% S/ J
}
+ B9 N) f- |9 f( \; b第三种:用数组方式插入数据,下面举例说明
3 H z5 \ |7 B% e* W9 |1 }" C#include <map>
' ~' w: B+ F" M* z2 P8 q#include <string>
+ f7 I! q2 ^' c4 n* V3 o8 }* b#include <iostream>( p8 I* }4 P" b+ f) U
Using namespace std;
: R: i, t( B9 I `0 RInt main()0 ?5 {6 w' z3 |+ V: u: V+ a$ I- z! }% o; H( R
{ ?) w$ r. M" G- G5 d6 |
Map<int, string> mapStudent;! M" x8 D2 @1 i1 {$ E* S
mapStudent[1] = “student_one”;3 h# v( D5 S' T. X0 f5 a) K& |
mapStudent[2] = “student_two”;' L' w5 m; x- q& b+ J' c
mapStudent[3] = “student_three”;
* \. p+ I) W1 n: u3 s; _ map<int, string>::iterator iter;/ g @1 X; E$ _+ i. s
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
` _1 k! _( A# R) a X+ f' }5 w{( @: ?( P# ~, p% ~$ q/ x2 q1 O8 ^
Cout<<iter->first<<” ”<<iter->second<<end;; p8 c$ K8 ~* ]- q4 \$ f
}
8 L( U* D( w0 Z3 f, E8 r. I}
+ u) l, M4 w7 e# M, L+ X, E {; h以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
$ T6 b* z. t d- KmapStudent.insert(map<int, string>::value_type (1, “student_one”));: ]% e7 ?9 c, T. A9 T. Z
mapStudent.insert(map<int, string>::value_type (1, “student_two”));0 v1 D( G/ ^2 C: A4 _4 b, U2 B
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
/ V0 H7 l* W2 E1 P# APair<map<int, string>::iterator, bool> Insert_Pair;/ ?" j7 J8 `: A j& e) n
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
' v2 u3 ^; H- V我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。/ v* o h% y, E' o/ Q* m1 M' h
下面给出完成代码,演示插入成功与否问题
# w* r4 c" U" }& m5 q$ H#include <map>
+ O9 k# f9 y/ V$ x#include <string>4 Q( Q9 Q6 U+ A+ V
#include <iostream>5 [8 W# f Z, u- u
Using namespace std;- Z1 ~- S* B% Z) e8 e; b0 T% }
Int main(), B' r4 D9 ~/ Y
{
1 U; _9 y) J8 u, Y& Y9 M& ]0 X Map<int, string> mapStudent;
6 F& u- u; K9 d2 @* E; }( |Pair<map<int, string>::iterator, bool> Insert_Pair;
+ P7 D& E$ A& H" q s' r Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));( }1 ]6 x" n1 J: V7 ~
If(Insert_Pair.second == true)" L2 \9 z0 p4 F& j
{, o3 y" G2 T# q: A: g' r* }
Cout<<”Insert Successfully”<<endl;. W- }# t! g9 h* Q7 d1 b
}
/ `1 p4 p, n/ ]+ f, j2 w( H0 G) m Else
2 `5 L# O/ S5 U U {
" k9 l9 a6 W# f% u3 g Cout<<”Insert Failure”<<endl;
! j1 y+ E' c% ~( t' j( X+ N }; [' G; y" a( b+ J4 |5 R
|
|