|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数% L" H- p: u2 L9 B/ q1 M2 K! Y
Map<int, string> mapStudent;, |; I7 o4 |7 P1 N7 T
2. 数据的插入
! J) y4 ?- x& e- t2 F在构造map容器后( I) f' Q9 K8 @. M2 c; q
第一种:用insert函数插入pair数据
) ~& f- I- F3 K( J$ P/ L" v1 v#pragma warning (disable:4786) )& b5 g) P6 {& J; a* ~3 g( b
#include <map>$ q r- m/ Q, P4 W
#include <string>
2 V" N5 I2 Q6 n. a#include <iostream>
( ]% ]. X5 E! s4 QUsing namespace std;
8 l/ U* A! f6 v& o7 q5 MInt main()7 {( w; ]0 B3 |8 z/ ~9 y
{$ J( y6 k e& V! g# X0 U9 U
Map<int, string> mapStudent;
4 `9 p: L7 O* Q3 a# j mapStudent.insert(pair<int, string>(1, “student_one”));' n6 j7 @2 L q- U! w, M
mapStudent.insert(pair<int, string>(2, “student_two”));
( I) [ N6 S7 |5 k# E mapStudent.insert(pair<int, string>(3, “student_three”));8 j. |6 P6 }- e9 z) c
map<int, string>::iterator iter;7 D: A1 f4 w9 O+ T' @. W
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)$ {6 Z& L2 |$ i7 Z* q m
{
- f$ i# G# ]* J# d1 t, R. j# |. m! ~ Cout<<iter->first<<” ”<<iter->second<<end;2 n2 V5 f9 F# E% \$ L# K- B4 {$ C
}6 L$ P# B" r/ @
}( C7 R0 E7 P. ?; Y% n# D
第二种:用insert函数插入value_type数据,下面举例说明5 O! D- t; f. J, k
#include <map>1 U0 B3 R3 w ]" {# \
#include <string>
7 k5 A [6 o! h7 i+ K; c" b! w#include <iostream>- {% G' [( j' N6 I. X/ }$ Q
Using namespace std;: E9 o7 V. H7 `
Int main()
& K6 m& m" H7 i8 l Q) G' {' _: D{. c* r0 P! P3 c X( p! H' T
Map<int, string> mapStudent;
& m- l9 H" s5 {1 _& m) }6 ~2 a mapStudent.insert(map<int, string>::value_type (1, “student_one”));" [3 y3 x( y9 Y, A8 |& y
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
" d( b& X. F4 c mapStudent.insert(map<int, string>::value_type (3, “student_three”));: N# J; L) d9 N- a/ n
map<int, string>::iterator iter;7 F! _: i+ y6 r8 d N4 }" i/ P9 F1 f
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
5 F9 m/ A z5 i7 Z2 E8 a1 T{
+ C) {8 X8 m; n! P; i Cout<<iter->first<<” ”<<iter->second<<end;
6 f4 Y* l) ^6 ^2 s$ [) v% j}
/ L" R, G# e% M* t8 W+ |6 G}
# {6 s: j9 _) E! k% Q! h4 B. C第三种:用数组方式插入数据,下面举例说明
6 U; i8 K0 q9 @7 D9 S' b2 ^" A#include <map>
0 W' e9 c9 m' V' |. }#include <string>
' K3 G. T3 |; ]8 z6 Y6 }9 [#include <iostream>% \+ u/ p9 _" m
Using namespace std;
& G1 W# w; c' @5 W6 ^( F) F) J. K' LInt main()
4 z# n4 E) I! i' \' h: p4 Z{! Q2 @& S6 G8 ]* e( Z/ {* `5 G
Map<int, string> mapStudent;# Y+ L `2 v5 F6 ~; D$ ?: f5 A
mapStudent[1] = “student_one”;
, U& {* _ p; |$ {2 H mapStudent[2] = “student_two”;/ s- n# d* e) A! f' {$ p
mapStudent[3] = “student_three”;" g; @7 A/ E" _- @4 |: D
map<int, string>::iterator iter;1 W: H4 ^: q) E1 `# |
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++). V" X( j8 |; z$ P3 ] I* S
{& l6 x6 b/ A% X: E8 S
Cout<<iter->first<<” ”<<iter->second<<end; r! G: t" m- U, F
}
8 I' N/ L R7 `2 G8 i1 |+ A Q}! }1 x6 Q7 [; k# e8 c+ [
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
7 G8 R/ V$ \0 k6 ]5 q4 R0 h1 W, ^mapStudent.insert(map<int, string>::value_type (1, “student_one”));) T& T" [# _! s: G
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
; y# W: n8 G9 X) S$ l上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
0 q* V: g% x" }* p9 FPair<map<int, string>::iterator, bool> Insert_Pair;
- }, T2 f5 o4 n8 Z1 z6 QInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));4 M5 A/ c7 p) X, K: B; i: |+ m
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。5 g$ ~9 ^6 l1 }9 ^% p' X0 o- n
下面给出完成代码,演示插入成功与否问题 Z( i: z! E7 \3 d$ s6 o
#include <map>
! F3 S# t- d" v& \0 u#include <string>+ h, Z# d/ i$ p w1 i
#include <iostream>. p; j8 e( u! ~# t9 q9 t0 |8 B, f
Using namespace std;
6 w$ p! o; X/ L! A9 T2 V- [/ [Int main()( d4 G# {4 `% t! ?2 O
{8 p0 y# T+ C' G
Map<int, string> mapStudent;
5 n) T8 v3 E9 |% {" R0 \Pair<map<int, string>::iterator, bool> Insert_Pair;
" T. M+ P7 l7 A% J6 L4 V Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
) [: S" Y1 l* l' Q If(Insert_Pair.second == true)- _; x2 q" }% v" z5 C' ?' h
{
. O* n' q! f/ t- ?! [ Cout<<”Insert Successfully”<<endl;3 k. P( j1 E# r l* B1 k2 x O
}6 G, T2 Q3 _3 ~6 ^/ ]
Else
* q( n! i" s! G- n! i& \; o {9 W3 Y, n- |. k3 r( K3 S D1 q
Cout<<”Insert Failure”<<endl;: {$ B2 |6 r* s/ q7 _. K
}+ z3 \* l5 m% K3 w
|
|