|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数6 K) Z- w6 L# E: ~
Map<int, string> mapStudent;, O: {) S8 ~8 ^8 I
2. 数据的插入; L% |3 b$ Q$ i& u7 R# ?- G
在构造map容器后1 D& y0 h j! w* x" W6 X
第一种:用insert函数插入pair数据
( r) F9 ^- q# \$ q* E, ^#pragma warning (disable:4786) )
- y3 i( C7 z6 g c5 ]#include <map>
M2 R, o/ z: Q: a4 |. t#include <string>- ~. b+ |4 x( y4 ~" J' P+ b
#include <iostream>
' [2 a! S7 _5 |Using namespace std;
# |2 J0 y7 ^ o9 ~3 a, RInt main()
) D @9 l3 R1 V2 h7 i{
) r) i1 X, Q' Q' C- [: ~ Map<int, string> mapStudent;6 F. {, n5 R) b6 M9 z6 R, _
mapStudent.insert(pair<int, string>(1, “student_one”));
+ r# x/ h1 a. t$ C7 M$ c( ` mapStudent.insert(pair<int, string>(2, “student_two”));( M( \' G6 A8 F7 ~0 B
mapStudent.insert(pair<int, string>(3, “student_three”));
a L; t0 @* T5 G: u+ _7 | map<int, string>::iterator iter;7 @: R* l$ a; P! x( z
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)* W+ l& b; E0 L& O2 F6 |
{
+ K i& f# p7 ?1 \9 {7 f Cout<<iter->first<<” ”<<iter->second<<end; c7 `5 u! T/ Q2 X% `0 v
}
. d, `& t2 \, e3 h5 v5 c* C/ n4 l* @}
) [1 i" j+ Q" b6 I第二种:用insert函数插入value_type数据,下面举例说明$ l* k. K! l7 b
#include <map>5 [3 t( ?: ?1 l4 f
#include <string>
3 d! I4 _! V. e$ o* ~% N c1 I7 l#include <iostream>4 h2 `3 f+ |8 M! w! d' n- \
Using namespace std;& J1 E1 a' b g$ d9 g) h
Int main()
1 V! q w, D! o2 a1 n d9 ^{
- {7 N% p' O _4 d/ X Map<int, string> mapStudent;6 b4 M- y4 g9 w- K3 i
mapStudent.insert(map<int, string>::value_type (1, “student_one”));$ F* n+ K: `, c1 B
mapStudent.insert(map<int, string>::value_type (2, “student_two”));( i: @& V* |7 a2 c9 Z5 S% a9 h
mapStudent.insert(map<int, string>::value_type (3, “student_three”));$ W! q7 h* A, z9 a1 g" c
map<int, string>::iterator iter;
( N7 L( ?, [/ D for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
. L/ f, R" w- |( g9 a& b1 a* `{
* a1 ^ m; M, A: b" u Cout<<iter->first<<” ”<<iter->second<<end;8 g \; F. l, f/ F
}8 l/ |9 J- K* D7 v6 q( X
}
; A4 W- Y8 `' n& T/ W5 h第三种:用数组方式插入数据,下面举例说明
1 @$ }8 b& j2 V, |#include <map>: J9 o; _- R* E6 A. m
#include <string>4 L6 ^6 L( p9 M8 j
#include <iostream>; F9 o/ n# P7 D+ f2 g
Using namespace std;
, M5 g4 ^$ s* u6 TInt main()
# W1 o5 l# L. b. |) |7 G: F: Z{
' z, m; L' A: L9 v, k) w Map<int, string> mapStudent;4 H2 m. R9 [ n( H# x/ r8 j
mapStudent[1] = “student_one”;
( L4 @! U% h8 z( d8 [7 M9 ? mapStudent[2] = “student_two”;
+ p5 P' v* f6 z mapStudent[3] = “student_three”;
& ?& v; a0 g; i% u( q" F map<int, string>::iterator iter;7 x/ _+ _4 e5 T8 a( N
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)" a# C4 \! n6 i F6 x- y7 |& X
{' q+ ~( P8 C/ n) q, t* i5 [- U
Cout<<iter->first<<” ”<<iter->second<<end;2 R5 e% F& `. j' p2 B6 b3 f) E' N- Z( g
}
( o5 u4 z8 _4 {8 ?}
( g/ V, o% d% ~2 z1 o以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明6 C% b: p3 g% t2 u! `
mapStudent.insert(map<int, string>::value_type (1, “student_one”));* r1 K o2 T4 `8 M$ f. H6 M# x
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
6 d* B! Q# Y0 l5 I$ o上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
) I, k( L3 Q3 p! P- _# t/ aPair<map<int, string>::iterator, bool> Insert_Pair;& M& t3 V, L: u/ t; O5 T/ A
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
6 A( R" S2 Q0 `7 U+ z* K我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。( z. g6 g/ d e7 _ k% v- I
下面给出完成代码,演示插入成功与否问题
+ v2 i3 \2 M8 E+ I) M8 D Z5 b#include <map>
/ }; p! @& q5 _3 f8 |6 m! ?#include <string>! D6 t5 O7 P3 w4 r9 L
#include <iostream>5 J& z! j* u4 k
Using namespace std;
+ ^& g& N6 ?1 ?2 nInt main()
: n9 H3 O9 d" H1 _/ h6 v{
+ P& t; L2 K8 P6 n9 J Map<int, string> mapStudent;
2 Y8 c" ~- U( n/ C7 L. YPair<map<int, string>::iterator, bool> Insert_Pair;
. |$ _/ F/ M S% t+ G! i Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
- w4 M3 A7 |$ {# [% I If(Insert_Pair.second == true)- k4 d b9 g. A. t- s* c
{
- M( z; O" v- i6 T( z Cout<<”Insert Successfully”<<endl;' y7 J1 s Z9 j8 I8 R1 m/ x
}
7 _# F i6 M }+ o Else
s% S. {! U8 @& r# B$ }/ S% U \4 k {
8 w; x% a/ V* X0 W Cout<<”Insert Failure”<<endl;
, v1 p. H* P( _" E" W; V } h! C* r; J _9 D, D7 x1 N1 F
|
|