|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数2 r$ I( q. S/ _0 E0 m7 S. E
Map<int, string> mapStudent;: u+ ?4 F" f1 o0 s+ W/ R% Q
2. 数据的插入
4 ^: M- e5 {/ y; _在构造map容器后
0 {0 H9 w$ t u1 z. _' [% d第一种:用insert函数插入pair数据& i- q7 R: l2 e0 D7 E4 F
#pragma warning (disable:4786) )
! b1 F7 ]: C& Y" ?( W% X#include <map>! Z$ B+ y: s/ K2 t% Y$ S
#include <string>$ O$ z: ^1 k( U* _
#include <iostream>4 K# A8 J$ r) N, S" q
Using namespace std;
, v& B/ j: [- k6 y0 l+ G: uInt main()- w! v! F; q$ Q) g. P0 K2 {1 O* \3 I
{
4 s: \ [: ?+ O) ]' g% d) p Map<int, string> mapStudent;
' V! P, f' v- P. z7 B9 ] mapStudent.insert(pair<int, string>(1, “student_one”));4 e+ j# K" f' C3 R
mapStudent.insert(pair<int, string>(2, “student_two”));) z' C+ k. a2 P4 V
mapStudent.insert(pair<int, string>(3, “student_three”));
2 |+ k, b5 Z& \3 j1 n) D map<int, string>::iterator iter;
/ f* s- S3 y5 J. [ for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
5 r! v9 n/ m7 m# u# ]/ d4 e{
# T6 V: s5 c* d; a7 R Cout<<iter->first<<” ”<<iter->second<<end;& I& W) W7 F2 G4 [" I
}( P' O" q; z" g. b# I
}$ Q8 r# K$ S+ n, O8 X( S& U
第二种:用insert函数插入value_type数据,下面举例说明/ S1 W! l0 b2 n' p$ `! _ V
#include <map>$ d8 `) f. Z& L5 a- p8 X4 O1 X
#include <string>' \0 s; K. z2 v4 D
#include <iostream>
. K. S' [' |$ fUsing namespace std;
1 h1 {) k7 N2 H+ KInt main()9 Z9 k8 Q3 D5 u" A
{2 B* S$ G, r7 @& B1 n: D
Map<int, string> mapStudent;
5 T9 \- l# ?# m5 S- D3 M, P mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( s8 Z' e' C5 ?* C7 M) R% z# k. s mapStudent.insert(map<int, string>::value_type (2, “student_two”));
- w5 K+ |" ]4 L' H6 r mapStudent.insert(map<int, string>::value_type (3, “student_three”));0 }3 r' T- W# A S* p
map<int, string>::iterator iter;
* H }" W5 f+ t1 i2 j8 M for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
# W# s* h5 N$ E! P. a{" Q" W9 S9 i' T: `
Cout<<iter->first<<” ”<<iter->second<<end; ]% X6 d1 p9 |# |9 A+ h
}
" m+ \2 w2 T( \' y4 v}
: X/ t: B$ n6 G7 Q0 t4 Q% r第三种:用数组方式插入数据,下面举例说明# u, |+ O- F+ O5 K" w
#include <map>
6 W- M+ m3 u* n( `#include <string>
4 Y9 M2 h, D. q, J# Q0 x1 E9 h#include <iostream>" E! M; R5 R$ R6 }+ ~4 G6 V3 Q
Using namespace std;
( M% f9 g7 R6 d# C$ [! s) iInt main()
/ K7 L* B+ f r! M1 P{
5 C! V5 C# G6 ]/ @ Map<int, string> mapStudent;
# v. E4 L- k* c# ?: t5 ?, ] mapStudent[1] = “student_one”;* J- K3 Y5 B }5 w) K
mapStudent[2] = “student_two”;
- {5 U/ \3 W. [, e# n! K5 { mapStudent[3] = “student_three”;
9 x5 [; ^% ?% I' ]& T! h4 X% q map<int, string>::iterator iter;# M# z8 ^+ j# n! s4 f2 k) T/ P
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)* N% a3 n* v, r3 r9 S% F2 Q- [
{, D0 P! t7 L5 e+ ~
Cout<<iter->first<<” ”<<iter->second<<end;( g8 j# y( M; g6 i
}
- w1 p+ y( g) @4 |/ G1 b* {}0 k* [5 \) U* y8 F" ~2 q
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明- _ L9 j C; f; p. S
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
, T& ^+ p2 j" e% ^5 r; d/ BmapStudent.insert(map<int, string>::value_type (1, “student_two”));1 P# v! \ }3 O% ^* b% p
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下. \; b6 A& ]3 o2 n
Pair<map<int, string>::iterator, bool> Insert_Pair;
+ t( \5 E$ B0 d9 e3 pInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
' w' g2 s7 z) R4 b我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。4 ]7 J2 p Z( m# Q, `
下面给出完成代码,演示插入成功与否问题+ r% }$ U4 M3 j7 x- y( d+ s# ?
#include <map>! k3 L9 k N9 A5 s; M' k' V
#include <string>
# `6 l' ^ U2 N#include <iostream>) r. f7 Z" y" E' M
Using namespace std;0 N7 A. R: F& k, u1 d
Int main() P- C; W- [/ I
{+ x {) x+ X7 Y3 O
Map<int, string> mapStudent;
0 G m; \3 c4 B! o8 kPair<map<int, string>::iterator, bool> Insert_Pair;8 ?, U% J' C" M! `* h3 K+ z0 m# O0 W
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
2 h, h7 ~+ I" D1 e& v0 s If(Insert_Pair.second == true)
$ Z! D9 V, l& `7 g" L- u$ E {
, C9 g8 c/ ^# |; l0 G, O V. u9 h Cout<<”Insert Successfully”<<endl;
+ e5 Y9 o* C' V4 [" d& j }
- Y% d1 O" W8 |! \7 Z8 ` Else" t- ~0 h+ Z) y m) y
{
% [2 [; ^2 H- [ Cout<<”Insert Failure”<<endl;+ d2 A- d* V1 F5 @2 ^
}
. O2 L& S( L, @& a$ v |
|