|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数+ U" ]4 A/ q# G
Map<int, string> mapStudent;' @/ z, S! U3 X4 z6 @- Z
2. 数据的插入
7 ?! d) Q) e' L) U2 k, n* V, h1 A在构造map容器后
% t5 o) V; |7 I6 V! C c第一种:用insert函数插入pair数据
: b3 \- Q5 |! e% T7 K- y#pragma warning (disable:4786) ); f" Q. B5 c" y
#include <map>, |7 Q4 U, z9 H1 @% d9 Z6 F2 l
#include <string>2 l5 f3 `/ [# K
#include <iostream>: B* z) w% O' F
Using namespace std;
2 H, z7 W& f+ ]# P3 M* e, \Int main()
( r8 s& j; J# S/ _/ t{
7 a" _- U) }( [0 L2 Q- k Map<int, string> mapStudent;2 F" ~) o, I% |9 t, u$ b* H
mapStudent.insert(pair<int, string>(1, “student_one”));6 @7 H6 j7 \7 c+ Y* ~: h
mapStudent.insert(pair<int, string>(2, “student_two”));
$ ]0 c# D* }+ ?( H+ e% }- ^" {0 c mapStudent.insert(pair<int, string>(3, “student_three”));6 f: ?5 c# X% d+ g) r; p
map<int, string>::iterator iter;
* {" Q. c- c$ L8 z7 K for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
8 s. D- ]* U# x" h# ?; W2 Y/ w{. y4 y' {! U; L
Cout<<iter->first<<” ”<<iter->second<<end;" F( Y" Q/ m w0 e
}6 v1 l# c: j( l5 k6 N6 ~
}
- k! S$ \7 _ `& [9 T7 y: B& J+ y第二种:用insert函数插入value_type数据,下面举例说明6 r6 ?1 A" J5 W5 B* H2 u0 \. D
#include <map># O# E2 E- {9 i% Q2 \
#include <string>5 k9 C) c+ }! m$ X% `3 [
#include <iostream>
5 R' Q( q$ r3 Y' ~8 ?, D4 _+ kUsing namespace std;
W3 g( u1 O+ G! GInt main()
' D! f4 e e# T2 R2 T, g9 C{+ [- b: k9 }0 ~, m
Map<int, string> mapStudent;
) a p, i$ ^$ c' d mapStudent.insert(map<int, string>::value_type (1, “student_one”));6 P7 \2 W0 L6 H @4 E) h
mapStudent.insert(map<int, string>::value_type (2, “student_two”));# M/ [& R/ I" G- E' Q
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
* x- Z# y' Z2 h/ n3 R map<int, string>::iterator iter;
* ?' ^, {7 Z- G7 L$ k' t for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)2 T% K' }+ }% t3 C
{
, V8 {9 E z7 Y( l# Y; ^( Y9 }9 r Cout<<iter->first<<” ”<<iter->second<<end;
* U9 O4 }: Q. Z1 Y}
/ |. ^! Z$ ]2 ~+ B" u- j# T}
$ S6 k/ C/ y: U' J0 t( j1 e% U7 D第三种:用数组方式插入数据,下面举例说明8 r( a4 ]- ~+ a5 l
#include <map>& O- C( t6 v0 e/ i l# R# _% ^( Z! n
#include <string>
# U, o9 e+ r. G" F#include <iostream>
: A; }( R- H' ]. Y2 G8 HUsing namespace std;4 o! i' T! g# G; T/ D/ j) p
Int main(); ]; h$ O2 {' p* W
{1 p* V3 H( @+ X1 J! K3 T
Map<int, string> mapStudent;
. \4 G2 U- B2 w1 n5 w mapStudent[1] = “student_one”;. t: }) N1 K' O5 a# e
mapStudent[2] = “student_two”;
2 d6 E& |; y' l+ J mapStudent[3] = “student_three”;7 O* `% t7 r$ `
map<int, string>::iterator iter;
# l i6 T. |( r, x) x for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)0 F( K7 A$ t. b' a
{
5 x& m/ y. \9 S Cout<<iter->first<<” ”<<iter->second<<end;) B- ~: i _; }- p- X2 g
}
: |, H0 T; x! @ N}
# T9 \1 m% U, w) e' ~以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明+ ~9 S y% O, M/ F% U6 v) G
mapStudent.insert(map<int, string>::value_type (1, “student_one”));0 p* p/ U$ m8 w4 w p
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
$ A4 b, N; }4 M6 q. v! F$ h6 x上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
/ M' P7 @) q* _0 ?Pair<map<int, string>::iterator, bool> Insert_Pair;! `: J, w6 h0 `- ?* F& l
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));, C1 K: Q' R) V6 h) G
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
+ x2 S) Y% @* F" s) M; R# h下面给出完成代码,演示插入成功与否问题
5 d- ]# p5 A* \#include <map>
; B) c9 z9 l- w: C& `2 {3 L0 u#include <string>. o& W( [- Q' D% `+ y0 U! U6 L
#include <iostream># |7 q% _$ {1 u! x7 ~
Using namespace std;
( p7 _4 e0 k0 d: mInt main()2 P9 ?8 q( c/ O5 c4 ^
{
1 @ E+ Z* \' T" }' x, T3 @% v Map<int, string> mapStudent;
& I4 U( |4 r, x0 l) b4 wPair<map<int, string>::iterator, bool> Insert_Pair;
1 `% v$ A/ c# y& l$ a2 n1 H8 A Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
; d$ M' A4 J% N8 H$ v; D If(Insert_Pair.second == true)+ P5 f* ~( O* _5 [7 E3 L2 n6 ~
{+ [; z" R) ~7 k3 D+ {3 w
Cout<<”Insert Successfully”<<endl;
! r1 l) c5 s; C2 e! y1 m- {9 e }/ _9 W- B8 }8 u+ v
Else$ [5 D4 u4 v7 U- x; l1 E
{ r0 z+ [, ~3 x* s5 k( T; f
Cout<<”Insert Failure”<<endl;( n" r6 g8 s: ?- x1 J
}, D* F: p9 B/ G) a4 V+ r
|
|