|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数1 S* |' `9 T L" J, n" Z1 _
Map<int, string> mapStudent;$ ]8 _/ Z) |0 o7 P6 _ c
2. 数据的插入
& P( v: `2 ~( I$ r% a3 n; }+ ^5 T在构造map容器后
6 q$ c' @0 N* R/ _: r8 z第一种:用insert函数插入pair数据
) l9 q8 l1 \( j7 A9 ~#pragma warning (disable:4786) )
' {2 }( |( g& J' H#include <map>' f5 {4 ~" o* ?1 a9 b' ?5 _
#include <string>7 K L+ U$ e! h @: B
#include <iostream>
" C# X. q' E, A( g& }Using namespace std;; G$ X: n j t# o \1 I3 ^
Int main()
# ?6 ^/ a" s6 s5 {8 H- z{
( o, W; `6 Y1 i5 M8 [* f3 @ Map<int, string> mapStudent;6 S6 x/ Z( X0 _/ \* a$ D) \
mapStudent.insert(pair<int, string>(1, “student_one”));
4 l) |" [. W E mapStudent.insert(pair<int, string>(2, “student_two”));
- u9 B+ @; E, n% K+ j2 B mapStudent.insert(pair<int, string>(3, “student_three”));5 L8 h, h' H4 d
map<int, string>::iterator iter;* j4 k/ |$ Z W# _& }! n2 `2 U
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)+ ]' W/ g t7 Y5 ~ R
{
, L9 o- [( [# b- {: X9 m Cout<<iter->first<<” ”<<iter->second<<end;6 j# k+ S6 G& c; V: ?
}/ _6 }- E" U2 Q. k
}: z) b: ^5 X% e# D( y1 V. N, j
第二种:用insert函数插入value_type数据,下面举例说明
* c3 F+ ^ w5 J4 I$ X#include <map>' d8 f6 C+ E/ ]9 ~6 i- x4 [2 K
#include <string>
7 q0 }) f! F+ J- C1 j#include <iostream>
( H0 \, I, l' e, P& w" ]Using namespace std;. J; w$ @( f# T& L( J$ F% B
Int main()+ D$ N, k* b3 _: j6 B7 A! m
{
: F4 J- h& `6 a! E* o9 F% X Map<int, string> mapStudent;0 Y% O9 F4 s0 E$ L+ K1 ?5 @
mapStudent.insert(map<int, string>::value_type (1, “student_one”));$ w( K; U2 B$ \
mapStudent.insert(map<int, string>::value_type (2, “student_two”));# `: l% b# k1 {6 b1 V
mapStudent.insert(map<int, string>::value_type (3, “student_three”));2 l+ E5 v1 }5 k( ^% B- s- N7 n# I
map<int, string>::iterator iter;
7 t3 s& _* U" f5 b5 e for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
$ B+ ~0 R1 ?6 S" E2 j{
' n8 A2 W' n2 l* l1 H Cout<<iter->first<<” ”<<iter->second<<end;
4 r# G( _! g0 i4 ]8 ^}4 A0 }$ J. z! T9 A5 R5 J
}! {1 ?8 y/ m% K T5 i
第三种:用数组方式插入数据,下面举例说明1 A0 w8 I* T& N' F; R3 C6 l5 e. e( ^
#include <map>7 z) W- g- q$ R3 o0 G2 b3 C
#include <string>
- h3 Q+ h! q, H; x. {$ K4 V! {#include <iostream>, a1 N9 A4 ^' u/ B
Using namespace std;
1 X5 [' K+ L( d( C3 s$ H9 kInt main()
! F9 P- L3 ^2 [* M( |{; z; _+ V0 B6 m# C) \
Map<int, string> mapStudent;% R k% A Z8 U5 s3 n& J" C+ _& |
mapStudent[1] = “student_one”;& ~5 v0 c; C% x
mapStudent[2] = “student_two”;
; Q; Y1 M8 ~1 C& Z: @5 O5 q mapStudent[3] = “student_three”;' Y W; y; \# O" i+ t
map<int, string>::iterator iter;
, z; x5 Z6 w3 B" a for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)' M/ _3 k$ Z% V2 E3 K4 M! ^
{1 z% S. }+ f0 t1 O9 i; N
Cout<<iter->first<<” ”<<iter->second<<end;8 x7 r$ c7 }- B9 }* C: K. G3 P0 s3 w
}# A# B0 a# D0 c( K! E0 l
}+ \8 g# q& v5 X7 _- j s
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明, E P4 V/ j; N
mapStudent.insert(map<int, string>::value_type (1, “student_one”));9 p9 ]- @( U$ P; E- E
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
/ S$ _, G8 l1 `- ~6 P) M0 U- \上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下3 y1 V* X$ F( o" p" R; G
Pair<map<int, string>::iterator, bool> Insert_Pair;: A" `! m% r8 P0 e( F, x
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
) W) ~2 t, \8 s4 j/ W' ^我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。2 L4 [. K9 N' q3 \
下面给出完成代码,演示插入成功与否问题
' b) ~4 z5 s/ m L( p#include <map>, T$ ^/ }4 O$ ~! J
#include <string>
1 j5 H5 g+ w {% |; ^#include <iostream>
: R6 s2 d4 s5 Z# B) rUsing namespace std;9 r# |" t" \8 e& V7 i$ k
Int main()
, ^6 Z) |( e, Q4 d; `* h+ W8 J- ^. g{
% y# i1 q. |! T9 } Map<int, string> mapStudent;
$ _) @( v6 S" y0 ?: j. H$ NPair<map<int, string>::iterator, bool> Insert_Pair;
' n' E p. J( G) |5 s' U; }1 m* `" s Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
1 C* e2 F: k: r3 E If(Insert_Pair.second == true)/ L. l" E( y) b; m1 _- v
{( Z& e0 ]$ _( f+ `" w% F1 N
Cout<<”Insert Successfully”<<endl;; v3 _7 X9 i. R- _6 ?7 S/ s0 R
}0 V8 c4 e6 T* u5 y( ~. I
Else
5 q: L$ N" Z( x: A/ P( b {6 I# Y4 S+ \* C. w& J# Y9 ], Y
Cout<<”Insert Failure”<<endl;+ g; c% A3 B! A9 `& H$ f
}
4 G* ]3 X. G0 o/ l$ `' L$ L |
|