|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数! h7 V o# w- R* _3 W
Map<int, string> mapStudent;1 @$ @# X ~: ?, @- [/ p* v; X
2. 数据的插入
A. h4 ^+ x+ J7 x在构造map容器后7 T5 E5 f5 J% b+ C2 h' k p& G+ K
第一种:用insert函数插入pair数据 N) r) L$ b0 q" O) g* y
#pragma warning (disable:4786) )5 V# [* e: ?4 Y: {7 b9 E
#include <map>6 ?6 q( [, p4 n
#include <string>4 v( ?6 |/ ^$ U% l! B$ r
#include <iostream>
) \' [( E* V* s: rUsing namespace std;
5 F7 ^0 F r- `: R7 @9 Z# AInt main()
2 K/ G C# t8 c# ?" B{6 ]5 S" `/ V' |9 Z
Map<int, string> mapStudent;1 o( e5 t/ v# B ~ x
mapStudent.insert(pair<int, string>(1, “student_one”));: J+ l8 U$ D; ?" T
mapStudent.insert(pair<int, string>(2, “student_two”));
7 E: f1 u( {5 {; s# Z2 W+ H5 v5 g mapStudent.insert(pair<int, string>(3, “student_three”));) }# m8 ?2 c6 k3 Z R
map<int, string>::iterator iter;
6 |9 g% d/ A+ b& w- q1 j for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
, s9 C: {" [' `{
! F4 I. C* C% | Cout<<iter->first<<” ”<<iter->second<<end;* z6 A0 E8 o" P3 ~9 q
}
1 s G: Y) ?6 z# x* j3 F2 G}
& Z& ^- |2 _+ S3 i3 R H: T* E, o第二种:用insert函数插入value_type数据,下面举例说明
5 H4 K0 d; j* t6 L+ M7 v6 r3 `#include <map>: }" o' D( G5 c/ x
#include <string>
! H: ]# P. F3 O7 O4 M* c. K% ~4 I#include <iostream>
/ m- o4 l! T4 c! T" g' hUsing namespace std;
' ], M3 {3 `4 T4 a0 w p2 l! sInt main()
1 o& X6 _1 s5 h{
8 o0 X5 t# v* ?' Q& H0 t Map<int, string> mapStudent;
: D2 i( o% n0 H# Y6 x4 b2 [2 O mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ }% k2 x1 q1 k# j% h& ~* n mapStudent.insert(map<int, string>::value_type (2, “student_two”));( u6 q0 g- m% A4 {% h1 N, \& X
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
+ a2 v# v2 z' l1 R8 c2 i( [ map<int, string>::iterator iter;/ T6 t$ r) Z2 I& l/ x+ |) D; |
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
$ o$ {: @# R* z H+ x! \, h{
3 s3 K2 S q! j0 _$ E: w5 y: R Cout<<iter->first<<” ”<<iter->second<<end;
. o4 ^& E" Z/ f7 o3 r}. A' D0 B2 o1 ~, ~3 E- O
}
' G' B3 f2 j, u7 K5 e- z" L) q第三种:用数组方式插入数据,下面举例说明' \+ T) A8 A, q" u& B
#include <map>
9 @& | i; o: l0 q+ ?* W#include <string>
# t ]+ A3 @9 b- X$ p$ n#include <iostream>) K6 M5 I' ~* X
Using namespace std;
1 @% Q$ i% v( R0 E4 X5 {+ j) a: Z; OInt main()
1 V" w8 F9 k5 h0 r8 Q. @7 k{) W% `6 j! {; E' k; L) Z
Map<int, string> mapStudent;+ s" b7 J* y R6 T3 R- I. a
mapStudent[1] = “student_one”;
: i" c3 e1 b9 n1 {! v8 u0 T mapStudent[2] = “student_two”;
, i" U5 v! P6 Q5 i' K* s; z mapStudent[3] = “student_three”;
\' g. b4 k) O7 s' P! ^: S map<int, string>::iterator iter;. p Y C0 w: ^$ w. y! m9 P, i6 h4 G
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
# @) y5 [! y/ x% m" T{
9 B, g4 ]0 \& g6 J7 h Cout<<iter->first<<” ”<<iter->second<<end;
+ x6 E- u$ p+ z- U c: Y6 D' V}: w) }1 H U6 D: F6 ?
}6 v" q8 Y$ J% o5 d
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明1 C8 R. a0 Z2 X# ?
mapStudent.insert(map<int, string>::value_type (1, “student_one”));$ k0 G7 s* S4 S p
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
0 ]4 D/ _% q" J. o% l+ X上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下4 l- ^% c7 E! f! o1 S
Pair<map<int, string>::iterator, bool> Insert_Pair;
. d, ~, {; d3 ?$ E Y' T$ @3 C5 y# PInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
7 i" T5 M' c% G7 \: ]我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
& R- h' \1 [' K! p+ G! [5 k下面给出完成代码,演示插入成功与否问题
- v+ Z4 A3 u0 v( ~#include <map>: C5 r7 }$ a- t# g0 y8 G; z$ z
#include <string>& z4 R9 Y& `1 n+ s$ k# V
#include <iostream>, H H4 E; o- D, e6 Q" C4 f1 e( ]
Using namespace std;
. _( k ?. }+ E2 A$ wInt main()# x6 h# D4 M ^( F- x+ L0 M( [) i
{+ T/ g) i B* Z1 R! E2 ^
Map<int, string> mapStudent;
* l" N! l: u) [Pair<map<int, string>::iterator, bool> Insert_Pair;
( m9 _/ ?, U+ Q% g( J1 ~. q( J+ i Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
' d/ w, U }6 W+ { I If(Insert_Pair.second == true)7 X5 Z1 R7 [$ Z9 k# Q: G5 x
{
, a. h( r9 T$ ~3 |" i# k. r. @! Q Cout<<”Insert Successfully”<<endl;
& ]1 M1 m7 ^- K: s: [- q }
; j0 a: y2 I% h$ n4 Q. w6 ?9 t* k Else
3 h! r; d* y! `3 |) r" ^: P6 @# H- I3 @ {4 D) F2 s' D6 I6 S
Cout<<”Insert Failure”<<endl;
/ R2 c( ]$ u5 r# O }4 o$ L/ C0 l: m- A- n: `& M
|
|