|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
4 r8 \6 @ G4 t+ X1 V9 aMap<int, string> mapStudent;
* e2 M7 {0 F0 E- w4 M2. 数据的插入% O% g& p5 e1 L9 [; C
在构造map容器后
* \4 {6 M2 H2 f第一种:用insert函数插入pair数据: R' g; v( A r6 X
#pragma warning (disable:4786) )
! f/ F/ }4 v0 t% w- k! q$ k; ]#include <map>
( k/ J$ b& w3 d z, k! m#include <string>
b* p- t1 c( F. Y/ m#include <iostream>
' D X, I( L2 g3 g5 Z9 rUsing namespace std;
& B' g3 S# r. E( `4 H/ F4 qInt main()
# I3 W8 c3 K: k, n# S{+ \0 e& g p% X& u' M% i
Map<int, string> mapStudent;
. J- N7 J+ W0 w mapStudent.insert(pair<int, string>(1, “student_one”));
0 @$ u. {3 ]0 B5 e0 ~( |( W: H mapStudent.insert(pair<int, string>(2, “student_two”));
, b. v8 B+ d, G, H mapStudent.insert(pair<int, string>(3, “student_three”));% m- A& O4 b* _
map<int, string>::iterator iter;
& J3 X& r+ ]; r8 A' y# Z- c) g# n* f for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)& ]* ]( C: C# Z" ~6 v. B9 K) \+ K
{
' \0 J- l ^, [ Cout<<iter->first<<” ”<<iter->second<<end;' Z) M. q# I& f3 ?
}5 J. j2 P9 \9 Y
}: ~, m2 p9 S* N% G7 k+ C
第二种:用insert函数插入value_type数据,下面举例说明
, N/ D6 X0 u a8 k2 i0 |- B#include <map>
+ c; f9 U% t6 I" Y9 R5 ]! j#include <string>
7 v( N( k( K9 W1 }#include <iostream>" O& x9 z! x- |& M. a; H1 U) i
Using namespace std;
7 o# p/ r8 c `, a- S! v7 D! AInt main()
8 G" h0 Y, _3 n! [' |$ z{/ O7 j2 N" c0 t7 e# V" `8 [8 X
Map<int, string> mapStudent;
( ~1 p( m2 D0 ]( A: O8 S$ m9 u/ c ] mapStudent.insert(map<int, string>::value_type (1, “student_one”));
3 B3 v6 ^& Q6 @- ` mapStudent.insert(map<int, string>::value_type (2, “student_two”));0 Q, C) J7 s9 \- @4 ?5 |, K
mapStudent.insert(map<int, string>::value_type (3, “student_three”));$ f2 f' |' Q N5 D. A$ `
map<int, string>::iterator iter;
/ K% d# ~1 a5 `- C+ P. m+ ?5 T for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
# J7 T: V- V5 _{3 t* O5 q& A7 x
Cout<<iter->first<<” ”<<iter->second<<end;( R/ h( j" L: R) r4 R1 f2 y
}
0 K+ e: m: V O: F- T6 K8 n}5 i( ? N# |( C% G+ q- J% z! \
第三种:用数组方式插入数据,下面举例说明
$ y8 O! e d0 \#include <map>
# v$ w% @% D- h6 @) @#include <string>) [) E6 h; b7 N1 ~ p3 m9 H
#include <iostream>2 o+ x8 E4 D; S& g8 b, U
Using namespace std;
* c7 L! v8 V4 s7 \2 FInt main()
" \) r/ @$ e& @' T5 l{
# F+ @! w9 h7 e1 D' n) ? Map<int, string> mapStudent;+ L$ s& i- Z+ \* w) J5 Q
mapStudent[1] = “student_one”;
% W, |) p( W8 e( Y7 t: \' _ mapStudent[2] = “student_two”;3 A2 D1 e, p) [) u1 B" p2 e$ n
mapStudent[3] = “student_three”;7 D& x5 x* I& V* C4 P8 T2 B" x
map<int, string>::iterator iter;* t8 p' r) U! W i4 j
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)/ Y# m" H$ Q- K* d1 x {! u
{
* n; l/ B# x. @& q" A$ A7 k8 E+ B Cout<<iter->first<<” ”<<iter->second<<end;/ w9 X w, m+ j* h# ?
}
% u- M" M; M' N) ?}
( R$ `( s9 v2 H0 z以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
: v/ b. r8 |7 A: ?- r9 C1 `mapStudent.insert(map<int, string>::value_type (1, “student_one”));8 ^9 `5 {2 ?; {$ t. `) D
mapStudent.insert(map<int, string>::value_type (1, “student_two”));5 @% U7 u/ d2 d/ I, I
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下6 f6 f8 E% M8 N6 x* e3 [6 S
Pair<map<int, string>::iterator, bool> Insert_Pair;; j) Q# D; `1 S6 P: M
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));8 b, `- F) t; f) w
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。2 b; Z- {; t7 I3 {' g( \' e- A
下面给出完成代码,演示插入成功与否问题- v4 x7 N# e( ^& {4 B0 T
#include <map>
: _" |9 c8 ]# j) J% |; f" A% N7 `: D#include <string>' y/ M- T' {! ^7 t6 B+ B9 w* b1 f
#include <iostream>
/ J) b$ Y7 Q, u/ k" C, {3 E0 M( YUsing namespace std;# X+ U% V ^: A3 e, k' H* A
Int main()/ a9 }2 a) \; i' Q* K' [
{
; Q2 h6 Q, u3 S Map<int, string> mapStudent;
2 W7 W# Q1 A5 ^# _) W8 V. MPair<map<int, string>::iterator, bool> Insert_Pair;7 v/ n1 S2 Y$ Q ^. ` Y* c( F8 D
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
. @" ~: L$ t7 V7 s: @) ~' K If(Insert_Pair.second == true)
# G( k' f8 _7 [ {& w4 E; P7 D3 v/ ?- Q+ D
Cout<<”Insert Successfully”<<endl;: X# P3 l. h8 Q' Y* ^$ a# t
}
G3 \9 [& _0 s" W: t Else
) a6 m$ N3 I s6 r) E {- [: M! ]( f. D9 j' _
Cout<<”Insert Failure”<<endl;
4 ?* _. e8 K7 c0 q }1 d! X7 \1 r# z' V$ `) l
|
|