|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数+ s. @$ V' s( S9 K+ X' M8 I8 }
Map<int, string> mapStudent;
( l& g+ c/ x2 v1 e1 O6 O$ T% a2. 数据的插入) K9 l1 v- y! K5 M, Y) K' E
在构造map容器后
* o3 L1 k7 m, X' M, n第一种:用insert函数插入pair数据! I' r* m( H" m
#pragma warning (disable:4786) )/ C. V; o& L% r
#include <map>0 p1 I7 c! _% M# _$ o1 w `
#include <string>
! p$ h, W b7 S1 u#include <iostream>& l# O( p$ x& t1 o: g& m, y
Using namespace std;
' |( r8 o, w o, V) ^Int main()
* i) ?6 Q+ d+ N) o3 @{0 G! Q9 P0 l0 X) f0 f
Map<int, string> mapStudent;
2 P1 [) D/ Z; I7 a& t mapStudent.insert(pair<int, string>(1, “student_one”));, R4 o# F: H* K/ A- }" X; h* ^( ^
mapStudent.insert(pair<int, string>(2, “student_two”));
0 l: J6 j- K6 f$ C* F mapStudent.insert(pair<int, string>(3, “student_three”));
$ B. q8 l3 ]% s9 N: L0 m; d. Z3 j7 \ map<int, string>::iterator iter;
" h1 z4 w8 O" F, b4 J6 a for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++); `' @( h% K/ q
{6 m1 z o4 `, m1 V0 Y' J+ D s7 `
Cout<<iter->first<<” ”<<iter->second<<end;
# d; a3 [$ y! Y! i* d v# t}3 J: {" o0 h" D) J- L
}+ x" k% y7 u' w8 Z
第二种:用insert函数插入value_type数据,下面举例说明& ]* @) c/ o* |2 v9 t1 m0 j
#include <map>
# _. z$ ?1 l7 e, I#include <string>
7 x3 s" q% B- O! z#include <iostream>' _$ ?; f/ T& {- ]" @
Using namespace std;
& V5 [9 v% P6 _! A+ T3 j4 AInt main()9 E F; K! ^2 l" P) }) V5 W
{
; S# e3 G! a$ G+ b1 G7 D. l. A Map<int, string> mapStudent;
2 U$ f1 j: i& Y. D) x mapStudent.insert(map<int, string>::value_type (1, “student_one”));) b0 g' {: v+ B3 v8 p, Y. y3 b
mapStudent.insert(map<int, string>::value_type (2, “student_two”));: B% [) y% s$ U* P
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
1 M' Z4 c' e8 g$ ~ map<int, string>::iterator iter;6 Q# b. |8 |2 |) K3 y& f& O
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)& h! r2 y; N' w2 |/ P3 v
{
8 f! z4 t8 i8 }& p1 s# |' H Cout<<iter->first<<” ”<<iter->second<<end;
$ i- n' O* a! Q1 c7 {5 Z/ S}, p% ]; b r' j* A7 E
}! S7 [4 P2 G5 q# l8 K# t5 u
第三种:用数组方式插入数据,下面举例说明3 N0 G- e# ~; Z/ G4 _, @/ k' M1 B
#include <map>( K( i; `$ G! ]+ t; H( i
#include <string>
6 ?5 M, O' U ]# W4 x" N#include <iostream>
# e: y6 V# l6 hUsing namespace std;
$ o$ o. P3 d4 C' w `Int main()% E3 D1 W8 Y" ]+ y. Q+ m
{& K% i$ J6 h6 A& K
Map<int, string> mapStudent;& y8 h& i# Y# o) |% `" b0 s
mapStudent[1] = “student_one”;
) x, K8 w! c0 Y/ C# L% z" n mapStudent[2] = “student_two”;
' q! f. C' \3 Y- B# W# n5 { mapStudent[3] = “student_three”;
2 d. P+ D7 M! Y$ V ~ map<int, string>::iterator iter;
, O# p& u- C- O w- f4 s for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)" [4 R, F% k* h, Z: s" d- f6 `
{
- Q: ?0 U' x7 K! w: N |# S Cout<<iter->first<<” ”<<iter->second<<end;
' `* S. I2 R( }}
4 Q/ \) j' d7 h3 ^: V}
1 Y$ A" n& w+ O以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
, R& v# w) J1 Q$ W& U7 PmapStudent.insert(map<int, string>::value_type (1, “student_one”));$ v( h2 K8 R; L- L& L3 D) r
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
$ v" f ^; k, t上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
7 g6 k, K# z& t: {3 F7 @Pair<map<int, string>::iterator, bool> Insert_Pair;
3 M' ?" }0 U1 rInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
/ m0 |9 T- ^5 H% K+ X- O. x c' M我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。4 h2 t% \0 ^7 q3 y
下面给出完成代码,演示插入成功与否问题
& K2 `% q; e. E) @) }% \! R; @8 h#include <map>5 _% ~" }: B# X; a0 F ~% F2 w! F
#include <string>
* a, G$ c8 }) @1 a z( |5 [#include <iostream>
3 r+ P0 i1 E3 U$ b. p3 hUsing namespace std;# y0 a9 G3 q) k' I( F6 |6 t8 q4 q$ F/ w5 h
Int main()+ w+ g1 N' ]3 C
{
# d% h$ _% Z# T5 g Map<int, string> mapStudent;
/ d- T, g' h! [" _+ f% C# jPair<map<int, string>::iterator, bool> Insert_Pair;' C$ f( O! w, h% g6 q2 e& \
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));* C$ B) @- t% `4 O# Y
If(Insert_Pair.second == true). K/ i# A& O# @1 E' |% E
{3 ~: @7 }2 W4 i' o1 t' H7 u1 x9 N
Cout<<”Insert Successfully”<<endl;2 _5 t0 Q1 F" f+ b4 G
}
+ r+ K: r0 b: y" M: s# W4 @ Else/ z8 P" H- G0 ]5 w) M' s5 z
{8 |. X, ]5 H( F. a s; K
Cout<<”Insert Failure”<<endl;) `: Z9 S+ a) e8 Z( P5 Y$ _7 I
}
$ Q( [$ D" |) l5 [ |
|