|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数1 d' U4 f6 d' u, K, L N
Map<int, string> mapStudent;6 e% U$ P0 }/ R) q" _5 k
2. 数据的插入
1 g2 S4 Q% e+ D在构造map容器后
1 x8 A' D0 L4 Y! E; G- D第一种:用insert函数插入pair数据$ v- D% ~, U# l8 B* P& p' P! Y
#pragma warning (disable:4786) ): H3 l3 t) G0 l* B0 J, r
#include <map>
9 U9 O) e: @3 @) P, I#include <string>" W( r- U0 d' p ?8 ^& k
#include <iostream>1 {! ]( I+ y" L( M( k7 H
Using namespace std;
8 S3 c7 [# h1 ~$ M0 }* BInt main()$ H, M. b% v+ p g1 r& t
{. f% o0 }5 ?, J# F
Map<int, string> mapStudent;( x, M) t I) } A( S, z! j8 r
mapStudent.insert(pair<int, string>(1, “student_one”));
+ e- l) H1 t$ \1 A: w# j. N/ V8 x8 f mapStudent.insert(pair<int, string>(2, “student_two”));$ U2 V s. J+ {) q/ X7 s
mapStudent.insert(pair<int, string>(3, “student_three”));: X9 B2 o5 k; I; p8 B, A
map<int, string>::iterator iter;* N! ^) ?; x" L5 C) a1 {
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
, i" A4 @/ D3 ^6 E" j4 ^5 b{
3 I+ [& ]1 R9 @7 _7 o Cout<<iter->first<<” ”<<iter->second<<end;
+ N7 s* R! v& C# {}
2 B. r0 p- A4 Q1 b, G7 b7 q}) j! |6 U! I" H
第二种:用insert函数插入value_type数据,下面举例说明; g6 W, J3 ^) O8 `& f3 Q) m
#include <map>
$ T$ _, N+ i; ^, u#include <string>: z! n# ^3 G" G
#include <iostream>( [1 d$ {1 D \: f0 @, e
Using namespace std;- ~5 y0 \* n$ W5 P$ t2 `' \) r
Int main()
" K; ~# O- F$ `1 e' K' L0 [* Y{
2 _& c, S* z% U; L, Z' z" y Map<int, string> mapStudent;
4 e* X k3 n1 v) W( o8 [ mapStudent.insert(map<int, string>::value_type (1, “student_one”));/ L; y0 w# S: Y K
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
6 ~1 Y K4 q/ {4 V% \2 m mapStudent.insert(map<int, string>::value_type (3, “student_three”));( K0 B' \+ U) h" x7 L
map<int, string>::iterator iter; v- v! v Y9 A, S
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) E! x" N5 P# F9 W
{
1 |+ N7 S; _# N) e Cout<<iter->first<<” ”<<iter->second<<end;
' z! L7 H+ I2 H2 Z}
7 X- K/ c6 R$ e( _0 I}
5 n9 v' X% B1 L5 ^' Z6 Y第三种:用数组方式插入数据,下面举例说明
* d/ V, s0 C+ @. E+ x#include <map>
% v. ?9 X7 v+ T4 p#include <string>
7 B l1 z2 d1 [#include <iostream>. W( t, r" i3 t
Using namespace std;
F* I6 K4 T& F, H9 Q, a# VInt main()
0 I4 D- o6 d1 j# S# G$ {{" f4 }% _- z& d
Map<int, string> mapStudent;3 A" r" V, B+ Y3 ~8 _
mapStudent[1] = “student_one”;
5 |+ f7 \, _4 P& U mapStudent[2] = “student_two”;
& @; |( u; v) L; x mapStudent[3] = “student_three”;" s i# Y& C- n* V2 \
map<int, string>::iterator iter;
& |; \$ k7 e0 o; E for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)- e# x ]" T8 r$ G
{$ F* j( T0 q, e* ^
Cout<<iter->first<<” ”<<iter->second<<end;3 P" P! Y& C2 f6 L* Y
}% ]1 A9 w5 n) O# `
}% j3 s: j- y8 u8 e# p1 u3 K% ^# L6 f
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
$ X# ^# j& `2 r% p7 \+ H1 j, w4 ]mapStudent.insert(map<int, string>::value_type (1, “student_one”));
& K5 T" Z2 U# n7 t2 LmapStudent.insert(map<int, string>::value_type (1, “student_two”));
0 z$ u2 ?& B: H* l+ n8 i" v2 \上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
3 H1 X5 p2 j' sPair<map<int, string>::iterator, bool> Insert_Pair; r$ T# g& b6 i. g) N1 q/ o* Z
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));6 v4 x+ z; D0 ~2 R8 q7 z( }2 o' y \% q! ]
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
; ]( P, j9 Y9 ~9 A1 ^# O下面给出完成代码,演示插入成功与否问题. j- t+ i8 l" c% F
#include <map>
5 z1 K2 q; Y- B: i& G* I& |: C#include <string>4 k4 _1 W+ N$ A F& Q% {
#include <iostream>
K- U: r( |% _! p% ]Using namespace std;
8 x' W- b- X7 q% g/ JInt main()
! D( c2 ?1 i2 Q& A- Y( s{
) J3 ~5 t( @4 L' A+ _1 g: ]$ f Map<int, string> mapStudent;
, o2 A9 O, V; }3 K* c$ A6 LPair<map<int, string>::iterator, bool> Insert_Pair;
$ }1 b# X4 E/ S0 M0 I# x; n Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));# X% o# C* D# f, @: N7 F5 N' {% A
If(Insert_Pair.second == true)
/ t. C% j$ t7 s; o8 `8 Z {4 c3 P2 l% F% Z/ s8 ^8 H) k& u" h, o
Cout<<”Insert Successfully”<<endl;
, C/ C6 u( F. i- ~ Q: [ }0 y3 U! ?0 {7 Y
Else, V- {; q5 q+ d, g8 M
{2 A3 e3 f7 ]5 l. ^ a& j, {6 Y
Cout<<”Insert Failure”<<endl;
; u3 i# E8 [; [, m) ~1 ]. m( v }
* [! c2 J* s' W. \* J4 q |
|