|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数( X- \' ?( g: v- u% h c( ? Q$ ? j
Map<int, string> mapStudent;3 R) H2 S. \" p; |/ F
2. 数据的插入
" c( B; s1 u4 y( f( @, R在构造map容器后
4 ~) a8 d# F# V( {. H' }第一种:用insert函数插入pair数据
% c" l h9 F5 n. y3 `#pragma warning (disable:4786) )* u! A: u; J. v5 G
#include <map>; q* r+ I( l: y+ h
#include <string>
o% T9 }0 o6 U5 d#include <iostream>
. \9 h0 ]3 O8 G6 S j6 X; JUsing namespace std;
+ d6 K1 ?6 q! {+ M; b3 y% ^; NInt main()9 g2 {$ v( E' l0 v
{
7 l' m" x( ^+ O- G) {0 Y Map<int, string> mapStudent;! p! @5 f& b3 p$ k- x% O# o
mapStudent.insert(pair<int, string>(1, “student_one”));
4 _% q- {# v" v/ ~ mapStudent.insert(pair<int, string>(2, “student_two”));
1 @ {! }' f& v2 X# ^; n mapStudent.insert(pair<int, string>(3, “student_three”));
3 A. E+ E b# n1 t% r! p6 w/ x map<int, string>::iterator iter; s. V( k4 g3 }1 n: s
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
, ^+ x2 J5 U) q% S3 Z/ _) o{
" R) W8 k s" Q. w2 d Cout<<iter->first<<” ”<<iter->second<<end;
B1 g- w1 O, P% F0 @}4 Z" l8 t9 `& m# H0 t
}0 e7 J- v$ V2 ]4 _2 p
第二种:用insert函数插入value_type数据,下面举例说明
2 _- x% n9 L) A9 }8 P4 ]% G0 [/ A#include <map>
* k1 u2 m% E9 j, s5 s#include <string>
' N8 @ [+ s9 e! M& [$ `+ [' s: t#include <iostream>
0 o. F* ~& j! X' v$ W1 ?Using namespace std;2 m! b# G( E' v+ _+ N+ U% ~1 [" V
Int main()
2 l! k8 ]- E O# g{7 c* s: ~# z% C# Y3 A( r9 R" Z# C
Map<int, string> mapStudent;
1 k2 J0 Z/ E" q# J6 S; T: @1 H3 J& B; T& W mapStudent.insert(map<int, string>::value_type (1, “student_one”));: E+ v7 I8 P8 {- u, y) ~3 V
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
) G4 d$ m3 n4 o mapStudent.insert(map<int, string>::value_type (3, “student_three”));7 r: S+ Z$ |1 B. ~. t, Z, ]' ~
map<int, string>::iterator iter;
' |* ~# t7 h6 [. w for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)( a; N- e# _/ Y+ ^/ [
{% \# ?' e8 Q- I, A9 p
Cout<<iter->first<<” ”<<iter->second<<end;
b; ]8 w6 j2 x}( [6 J# k# Y0 j$ K1 h4 C9 r
}, _8 u @: Y4 p+ H6 K0 V' l4 W
第三种:用数组方式插入数据,下面举例说明" ^0 r5 ^, `) ?1 a/ v/ t0 Z
#include <map># I0 j0 Q z* [. o
#include <string># j' ^3 [4 v! e( G+ {, G5 s$ y
#include <iostream>
8 W. G: i* |6 R0 yUsing namespace std;* X$ B7 J9 D1 g. j0 B @6 a* k
Int main()
$ `% J! h3 a: [{
6 n G0 u% W9 I5 m Map<int, string> mapStudent;( z; F u" q( @2 x
mapStudent[1] = “student_one”;
8 K0 E% b! L5 N2 C1 Y, X" u mapStudent[2] = “student_two”;$ [! Y2 j' g% k% x+ M# u$ N* V
mapStudent[3] = “student_three”;, L4 v' A" |, o8 F3 h+ o/ r; n0 v
map<int, string>::iterator iter;1 I# P0 {8 E8 v0 L) Q
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
. R+ f9 L+ Q3 ?% x" @{
$ I& d* F) t( f1 \. m7 G6 a Cout<<iter->first<<” ”<<iter->second<<end;
% ]! S' K6 X \: z$ I}& \2 A6 [, |# D( f( w5 q. e, N, k
}* m1 A' b ?/ M8 s; w
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
% s/ f* D& _: _6 x( d8 HmapStudent.insert(map<int, string>::value_type (1, “student_one”));
: c7 Z) B5 ?* r( e7 AmapStudent.insert(map<int, string>::value_type (1, “student_two”));
) ^8 {. v) i' Z上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
5 R; q) c, { p& P- B3 O9 pPair<map<int, string>::iterator, bool> Insert_Pair;5 X1 M0 F( Q4 R5 Z
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”)); p. E% S. u" W/ V# z% G9 b4 q
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
( Y! Q, F9 _4 S) Y下面给出完成代码,演示插入成功与否问题+ S9 Y( k! S$ g# y9 j4 ]2 |4 N
#include <map>
( c+ h2 C3 p9 o5 T( A, ]+ c0 x#include <string>
# D2 `# _6 B M6 z9 M6 D#include <iostream>
, z" }! m+ g% B2 e$ `; W7 QUsing namespace std;& `4 k: S3 ~; W0 y) O s" G) p7 F9 R
Int main()1 a7 f1 x% W( [! F% ]2 Y, D
{
0 V; n8 {( Z1 X/ ^ Map<int, string> mapStudent;
' k P7 l0 _7 rPair<map<int, string>::iterator, bool> Insert_Pair;
5 Y. [$ q+ E% u- p+ d- H Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
% V4 u. x0 j) w" J. b1 S- d If(Insert_Pair.second == true)+ T: s* g0 F3 h
{
3 z; l7 J6 H5 |( Y t3 i Cout<<”Insert Successfully”<<endl;! Y3 D( ~2 V3 {3 E2 O8 G3 d
}. D7 a/ a( H, b: @" c
Else
+ H7 j) _ N- D* d8 j {
% o! Y8 Z* Q+ e0 `8 C: b. D Cout<<”Insert Failure”<<endl;
2 V" g- u$ y2 ^7 C3 _ }
4 o# P# X6 J. P$ @1 g/ ]1 t; R |
|