|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数3 z9 i( @, ]( _9 S. t
Map<int, string> mapStudent;
8 d5 r d3 s' z8 x; k5 G4 H% B2. 数据的插入( g- G# @, k; r/ Y/ z
在构造map容器后9 V7 W- g3 ?8 |6 v9 a
第一种:用insert函数插入pair数据
; X2 h' B6 C% A9 t3 Z( ^#pragma warning (disable:4786) )
. e# S3 y# X; T+ q1 o: c' ^. j$ V) N#include <map>1 L4 E$ F, V I) i# C
#include <string>
! {6 Q9 A; N `# k9 g#include <iostream>
0 [* [5 v, `7 i6 x# _4 A% C% qUsing namespace std;
# I# h7 Z& x( q1 E! i' sInt main()
6 s( ]# s1 ~4 h{
0 t$ C( E7 U. H' v' b. O Map<int, string> mapStudent; M! X7 f5 {/ b$ Y
mapStudent.insert(pair<int, string>(1, “student_one”));9 v6 o7 T8 N8 l
mapStudent.insert(pair<int, string>(2, “student_two”));/ S4 m/ G# F" t* x' |8 q7 U
mapStudent.insert(pair<int, string>(3, “student_three”));
5 N$ U* Y4 ^) _4 c map<int, string>::iterator iter;
$ H; G# p* e' k% `9 F$ }. ~ for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)7 X5 K9 r; z& ]2 ?% j
{
- o1 R0 q6 w7 | Cout<<iter->first<<” ”<<iter->second<<end;
^& F( Z- \6 h6 v9 V9 C}$ j7 m; s' R) W" n
}$ C( B* a* T4 k( f0 }, }
第二种:用insert函数插入value_type数据,下面举例说明5 j3 h3 w- ?! |6 J* ` U7 l$ Y& `
#include <map>
/ _- O! y* U4 h$ z#include <string>
' R7 J4 V7 p. e5 H#include <iostream>
* x: C) W I6 wUsing namespace std;
1 ~4 ?8 w/ }2 o: _# _& o% AInt main()
+ g' P3 a7 o9 j% g{
$ ~& E1 r! A! K% m0 J/ X Map<int, string> mapStudent;9 j U/ \7 x5 I7 A* p8 G8 Y
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( w7 G- X7 d4 p8 i* z* _! d mapStudent.insert(map<int, string>::value_type (2, “student_two”));! R4 [$ I8 w7 \, j
mapStudent.insert(map<int, string>::value_type (3, “student_three”));0 b( O+ w/ P$ V. D, h) o' z
map<int, string>::iterator iter;
* b. h1 ^5 k" k2 Q" T* j Z for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
8 ?+ K! d9 r8 n( A& A2 k{1 `8 w# y+ Y/ u: c+ \: M
Cout<<iter->first<<” ”<<iter->second<<end;( K0 n: ~' w* a, b) F+ m
}
, b$ A0 c! n6 b}
- g& D/ K$ \- \6 ^6 \5 D4 |) G7 E第三种:用数组方式插入数据,下面举例说明
' u' e1 d$ q% ~. W9 ?) l( \ N9 ?#include <map>
. E/ W& o- p+ G! Z3 `% W9 L#include <string>) q- t6 Z* X( ?$ k
#include <iostream>
: o/ D- m( T7 G2 Z6 j4 mUsing namespace std;
! D( X' b3 S0 GInt main()
m, d" S$ F. S6 F7 W% C{$ J: u3 q6 |; ~9 |
Map<int, string> mapStudent;2 k7 K" J/ q' }- z, c) h8 _$ B8 t" t
mapStudent[1] = “student_one”;, x+ Y; }* l, F+ n
mapStudent[2] = “student_two”;
% G% |5 n* \; K0 h1 _* v mapStudent[3] = “student_three”;& F% B+ Q6 L) @+ R! d/ o3 I& ]
map<int, string>::iterator iter;' T W; b8 X% ]/ q5 P: |# H
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
4 I8 p# A# j( l/ {+ y% C2 B{
7 p% d. o1 ]$ m |( n- L2 I Cout<<iter->first<<” ”<<iter->second<<end;
) F/ L0 s6 y7 _+ m L}8 E. ~" Z; e$ Z7 R2 c
}! { W6 O3 {3 P! b+ ^9 E
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
6 V( o% w3 u4 z6 DmapStudent.insert(map<int, string>::value_type (1, “student_one”));! I4 s" ~6 K3 K7 t
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
% P1 T- I6 |/ ~4 K$ v/ A上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下1 c/ L( J% I& E% I; Z2 ?9 Q
Pair<map<int, string>::iterator, bool> Insert_Pair; K! j H$ [0 k7 I: r2 x5 G: d
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
- {1 y$ V( L8 f5 D& K! K, w我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。; J) n6 h6 D8 M4 _, t* O% ?
下面给出完成代码,演示插入成功与否问题
& f l! v2 e" Y4 {8 ]; O2 L, C#include <map>
3 m% q9 X6 x+ h5 i#include <string>1 q" |! B# g2 ~ w8 U U1 e
#include <iostream>4 a' \) \$ A/ z( `4 w
Using namespace std;% z, g P; S! F1 w) X; C4 u
Int main()
% n3 ~3 F$ O, }{
) }" E4 W+ a! g( W' q+ \3 D) y5 ^ Map<int, string> mapStudent;
" X& x. y( |" ?Pair<map<int, string>::iterator, bool> Insert_Pair;
2 _% k3 W$ `$ ~0 j( E Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));3 J0 M: C) o \5 ~; J6 ^
If(Insert_Pair.second == true)$ P6 X' [ B% a$ V
{
7 a E; L/ T( T8 x" Q Cout<<”Insert Successfully”<<endl;
6 b; T* J& H) {" `& N! D }. j- ^. [% O6 j- B$ p
Else
! h1 k( A5 F7 J7 p: K0 [% E {
& d* ?0 W- {8 [ Cout<<”Insert Failure”<<endl;. L% [/ j4 K+ Z8 @" D% l" a2 z
}! j9 T7 l4 R1 J# F3 B q
|
|