|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
; n0 T, g& Z, S8 ]8 z: dMap<int, string> mapStudent;
1 D7 }7 o0 H% a0 z% M1 M% A( B2. 数据的插入% z$ X' I( r1 L$ i; v/ ~4 z
在构造map容器后
. Y+ l# E7 K9 o* B/ ~第一种:用insert函数插入pair数据
7 }9 b4 K7 c! P: M. v6 ?#pragma warning (disable:4786) ) ]' i A V2 g, y
#include <map>
! i( e4 R, K* Y0 n#include <string>
' \/ p' _$ `* R- S#include <iostream>+ h B6 {* U! L* S
Using namespace std;
: P/ g) g/ V D0 J8 N3 Y( aInt main()+ p7 V$ `' c4 y8 Z
{
N) v2 D3 r A$ [7 d% D2 M& U Map<int, string> mapStudent;
& L9 u" F5 e0 _2 h1 f; N* z+ j mapStudent.insert(pair<int, string>(1, “student_one”));2 j, P, { b! Q0 |4 H- P- s
mapStudent.insert(pair<int, string>(2, “student_two”));0 O# G/ \5 N8 ~( a
mapStudent.insert(pair<int, string>(3, “student_three”));. l, c8 Q. N% L+ j! K& A( b% O( S& n
map<int, string>::iterator iter;) q5 r4 `, Z, |2 ?, G6 r$ R
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
8 H1 }" Z( E- w; Y; n. G+ X{
1 `( c8 r F6 W5 J Cout<<iter->first<<” ”<<iter->second<<end;
7 y8 {/ X9 H$ I/ w}+ O; M5 ^% i. s7 }
}
/ X% t% \3 S' m' {5 ?第二种:用insert函数插入value_type数据,下面举例说明
" }2 P6 c, z" \! N$ ~$ j#include <map>4 {) Y) H1 H9 M" J0 W R
#include <string>
S' n: c1 \* o( Y, Z$ v. X#include <iostream>
: }- c ?: u3 \" u, `% [! q( H$ jUsing namespace std;
9 t8 R8 t7 H1 c4 Z3 B/ LInt main()
2 B$ H% w, h. ^8 Z; @2 S, d8 O{( y" M" p/ m2 ]( {
Map<int, string> mapStudent;4 t: _8 O: `8 c
mapStudent.insert(map<int, string>::value_type (1, “student_one”));' z, c p8 R0 B
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
$ J- N4 o$ d: H* C+ ?/ W7 [+ U mapStudent.insert(map<int, string>::value_type (3, “student_three”));# a: H8 C5 i) U7 T2 N6 a( J
map<int, string>::iterator iter;' }# ^: Z; E& i
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)8 F3 R7 Q$ j( _. g
{
5 b4 k) w, L& ]5 W2 w1 a7 r$ S7 p Cout<<iter->first<<” ”<<iter->second<<end;
" ^6 @. D2 h1 B3 p# Z. J% L}( I* c. q$ g$ W- W& K: O
}( H; O4 o4 I+ t$ U6 ?
第三种:用数组方式插入数据,下面举例说明
9 [3 l# w* U. w) U; v" Z#include <map>
: z* j7 @! f: j2 C#include <string>* v$ |8 r& F Y1 Z
#include <iostream>
- t6 m/ T7 [- H, q8 i* iUsing namespace std;4 A5 A& d4 o/ r0 I$ t) k
Int main()
0 x% T! \# W/ n, ^{8 ?2 ]7 B" w, w" s: |7 C
Map<int, string> mapStudent;
, Y# i \' G$ b& p3 S, }, @ mapStudent[1] = “student_one”;" h! J( F/ i y) t5 |
mapStudent[2] = “student_two”;0 I6 P( P) x2 f; `
mapStudent[3] = “student_three”;+ l* H* `4 N. g# p
map<int, string>::iterator iter;
8 ?, w3 G6 j0 n( h; } for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
. r. D, K1 `4 G4 E) b3 C{- M+ O/ H6 h; | B0 f0 X. _$ j
Cout<<iter->first<<” ”<<iter->second<<end;% L9 R) @6 Z+ p7 _
}# j S! d L& C
}; j- @ s. L7 s' \5 v9 O
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
- W+ g* ~; F& y/ n5 E8 q# g V- h0 XmapStudent.insert(map<int, string>::value_type (1, “student_one”));# f$ P# M; I" B7 T) r% h
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
& g( z3 J0 o, z; V1 r: o; P7 B上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
H5 ~ }3 s+ u0 j7 pPair<map<int, string>::iterator, bool> Insert_Pair;
; U' C9 g6 p& c, M; Z' ^* b b2 sInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));0 \$ \# Z* K* d) a
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。/ u1 }3 b0 [" L- S
下面给出完成代码,演示插入成功与否问题) ]$ o/ Z4 a. ?
#include <map>
G0 G7 _- l4 o+ w4 C! r#include <string>
L6 b, Z3 X5 B7 F, U' N#include <iostream>
( s) H" X7 q8 g8 \. uUsing namespace std;
7 W6 G3 q1 E2 q5 V: FInt main()2 E# n9 C" Y6 u/ |# q
{
, H1 b3 j& W' i$ Y- L) r; a0 ~* p Map<int, string> mapStudent;; X! F' E8 @$ ~! k. g+ T* i/ l
Pair<map<int, string>::iterator, bool> Insert_Pair;
1 i0 \/ I/ R N" X4 z) p4 ?9 t Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
+ F+ ~. @/ p2 a! ^, ?1 C5 \: X If(Insert_Pair.second == true)
! `" \# ?0 j( T {
# x* a% r- K5 K4 q Cout<<”Insert Successfully”<<endl;4 w6 h4 ?; P- F, \, K
}
5 x2 j- d$ }7 l Else: D b. d0 m/ P( g
{
7 S: C5 m" c% X$ }& D- F Cout<<”Insert Failure”<<endl;
7 a' `- d4 X, H. Q) e7 {! H9 J }
E! @' {- _3 F8 y6 J8 e6 g- C |
|