|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数9 d+ S" f$ X4 ^" s V0 N8 j4 t' y# F
Map<int, string> mapStudent;! T2 x" R" \' I) l! C$ h5 @* b }) {
2. 数据的插入
; k0 d* J @* B+ d2 X3 }6 R, {在构造map容器后
$ m8 C9 i6 R# d, s5 s第一种:用insert函数插入pair数据
% J. H6 G/ N7 f' o& N" s6 f+ m1 K#pragma warning (disable:4786) )
) I: o$ S7 O& [1 `1 E) f$ Z#include <map>
5 a8 g: V8 H3 Q9 }$ O#include <string>
4 x6 y' v% ^+ N+ Q. |6 b0 N3 q#include <iostream>
$ \) k J& g3 ~Using namespace std;
' \1 w. I/ r7 @! x N H: T0 U, k5 C+ z' rInt main() b& d6 D- |5 g
{9 F' B* F" H! B: T
Map<int, string> mapStudent;6 p* u. a) e* A1 |3 l- W
mapStudent.insert(pair<int, string>(1, “student_one”));
4 U- @7 u* W+ H# I mapStudent.insert(pair<int, string>(2, “student_two”));
: H4 z: r+ n6 x0 }# h% u T mapStudent.insert(pair<int, string>(3, “student_three”));# x/ Z# i8 T% Z. e1 v0 P5 x
map<int, string>::iterator iter;
, z: o" u% E* T/ v4 ] for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
7 c5 G: O, j* o. p2 A8 \, E{
0 D2 L; [9 X) o% t2 i; w6 g Cout<<iter->first<<” ”<<iter->second<<end;
" V( S, U/ p- k$ b5 u p! B& F}. I* Z+ J2 N; r$ b8 n! N
}& r- K* L1 s- g- `: n$ M
第二种:用insert函数插入value_type数据,下面举例说明# m/ o) H3 |% W U
#include <map>/ s0 H0 p8 ?. j
#include <string>+ k8 m0 z( F" e3 p j% j. O. B
#include <iostream>: ^ b1 t+ @! b, G ?# U& Z( }. g* F
Using namespace std;8 v0 t2 d, ]1 M5 n* c2 V- f; J
Int main()& [2 e; u, Z0 W+ t' _
{0 w' x6 B2 f0 X, f4 [; S
Map<int, string> mapStudent;
/ ]# y. k& u/ G3 u$ q3 \ mapStudent.insert(map<int, string>::value_type (1, “student_one”));3 }3 |. c1 {' r& s$ q& v
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
5 f3 I6 X& _1 N$ t, v+ E! A mapStudent.insert(map<int, string>::value_type (3, “student_three”));* \/ p& \* F; \* x
map<int, string>::iterator iter;, L; q4 J* l" w. ~. o
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
% m/ s3 `9 p4 s{
7 J/ ~$ O0 \% Y$ U7 g- D Cout<<iter->first<<” ”<<iter->second<<end;6 M* x) D& N6 c; G, h( \
}8 F: b$ ^) _: _& \+ y. I, h* F, C1 u
}! |- j$ u) z) @: F% Y# ?* G7 f2 o
第三种:用数组方式插入数据,下面举例说明
" f( u% y- U5 w, {- g" I#include <map>. ^" V0 s/ S) Q- U) j, H# G% G' X5 a" I
#include <string>
: d( v2 G$ j* ?. D#include <iostream>' H" g! b, _- N$ u, v
Using namespace std;
0 M4 o7 i1 @' c- Z7 J' s" ^Int main()
1 F: `. X5 x @7 A9 Z{; j. A) T c; {+ L
Map<int, string> mapStudent;$ J5 |7 @" z6 H2 Z7 Q6 B; F) X3 {9 m
mapStudent[1] = “student_one”;7 d& E7 z) p# J" {3 z9 `' K0 x2 n5 T) G
mapStudent[2] = “student_two”;+ a/ A# S6 I$ O2 G9 d
mapStudent[3] = “student_three”;+ G; j# D, G: A# r& v
map<int, string>::iterator iter;- p/ B# c- ~$ U: G
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
; O9 g# ~8 ~8 N, C- f! B! T{% E3 p1 `0 y0 {8 ?3 F# }
Cout<<iter->first<<” ”<<iter->second<<end;
& Q5 M$ z# q7 @# g}
8 c8 p; X. @- q5 _}
4 V2 T+ e, W. B& L6 @5 P: O以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明& ?$ J" k2 v- M3 n
mapStudent.insert(map<int, string>::value_type (1, “student_one”));6 w; X! R2 C3 o, G `) m
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
7 F7 p9 H3 q/ ~+ M: o2 a: @上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
1 M( ~9 U( s9 E) QPair<map<int, string>::iterator, bool> Insert_Pair;
. b+ r" P# i8 C1 KInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));9 H! c3 S; n4 l1 c
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。" ?2 Y+ b3 |$ H$ @- m9 R
下面给出完成代码,演示插入成功与否问题
0 n! F" w* B% o! @5 Z#include <map>' a2 _0 D7 g) r" J" }9 ~- o
#include <string>/ J. w5 o8 w/ h( O3 R" a- b
#include <iostream>
* g( r) K8 H& ~* ~. PUsing namespace std;
% f% i$ }$ k1 H8 F3 {- `Int main()
6 S% F9 f/ a8 N4 K' X9 O{8 h8 h7 b7 r- R% d0 S
Map<int, string> mapStudent;
8 m5 h( E6 h4 {; G: i |Pair<map<int, string>::iterator, bool> Insert_Pair;: a( w/ C& ^% `% u& Y
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
. ?3 z& `2 {9 J/ {) }! [ If(Insert_Pair.second == true)7 J* C# O2 z8 o
{& _2 j/ F" R! t, }
Cout<<”Insert Successfully”<<endl;$ F! |, k: S$ q7 c
}
: H, F: ~9 A5 T+ ^, U" h. H Else" ]2 o. n' M9 s D. V8 ~
{
9 h& Y. R% Q) `. [- O; N, t' ] Cout<<”Insert Failure”<<endl;
% [- ]: l( p% d) Q( I) H O- I }7 \# D+ @9 x. a8 E0 U& ~# h
|
|