|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
5 Q- O, f. V6 I+ W% k3 ?! _5 ZMap<int, string> mapStudent;: e, W+ J# x- d; }) T0 z6 x' h1 Y
2. 数据的插入
5 o d2 x3 M* r3 O( n在构造map容器后
O. Q7 c2 `& I4 j2 Z! I第一种:用insert函数插入pair数据
/ u/ v6 r* Z/ r% D" P8 m9 M2 V#pragma warning (disable:4786) )5 j2 G/ @1 x1 Z; i" I) y
#include <map>% D) N( n9 D& R! c! w7 r
#include <string>
# \! U' O* ?9 v5 J" c1 _#include <iostream>
" A4 M2 }& n) h6 hUsing namespace std;
$ G# D* K- z" x- HInt main()
- @9 t( y. l6 ?{" [' S/ L5 h* v/ f N/ F+ L
Map<int, string> mapStudent;- [" {7 M \* k- {7 J. d0 H
mapStudent.insert(pair<int, string>(1, “student_one”));
5 a2 M$ k. e: { mapStudent.insert(pair<int, string>(2, “student_two”));3 ^" F8 K( T( m/ f" k2 `
mapStudent.insert(pair<int, string>(3, “student_three”));
/ J3 i- u( |" e) |+ t map<int, string>::iterator iter;, ^- Q5 ]( ~) P+ Y- d
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
3 o5 Y8 A1 j6 Z8 @2 H& V{
) N3 p9 B8 |2 v% H; i9 c Cout<<iter->first<<” ”<<iter->second<<end;! ?0 N1 B: t: @4 i9 f7 y \/ ^
}. z1 e8 Z+ ~; X% k8 e3 R5 f9 s9 k6 m
}
/ V% s1 w. [+ _# s第二种:用insert函数插入value_type数据,下面举例说明9 c2 U6 |! M; h$ H: O/ {
#include <map>1 ?, T/ {8 ?0 D- `& z) W
#include <string>
, Z& s1 n; v; v' U: s/ @2 b#include <iostream>
7 q' B& ~" F: R5 O/ qUsing namespace std;
$ j0 Q( \2 E* v7 S3 V6 X" z7 a: A; VInt main()
& y" ?* J' c/ W{) _4 Q/ e9 H3 e7 g) A
Map<int, string> mapStudent;
" C. Y; a2 t2 X8 ?+ I( z( i E mapStudent.insert(map<int, string>::value_type (1, “student_one”));
( X' I' ]* L R6 K7 `, c2 m mapStudent.insert(map<int, string>::value_type (2, “student_two”));4 I3 Y2 _6 n! v5 u" A) T2 b
mapStudent.insert(map<int, string>::value_type (3, “student_three”));3 h( Z' t3 J# |- Z; n
map<int, string>::iterator iter;% T; o. g6 k9 Q
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)8 q2 E, [: L: ]$ ^) k
{' }, X O5 f0 n1 c! U
Cout<<iter->first<<” ”<<iter->second<<end;
, Q" D& W; f1 p5 O2 O}9 w1 Z, t- E w! m( L
}% t% C* Y* U/ i2 e# W; m8 G$ ~
第三种:用数组方式插入数据,下面举例说明" h: Q( ?# R+ B" }3 a/ Y9 a/ @3 F) ?
#include <map>
& e! E! z3 f% L/ ]7 h#include <string>
( b3 I; G# C3 I6 G. R" U# C, _4 @#include <iostream>
* e7 X1 [" n9 f* X6 XUsing namespace std;
J+ d7 j- D% HInt main()1 p# _5 K& J, c3 R3 d
{+ y$ N$ f" {3 \3 x' p: j% f
Map<int, string> mapStudent;2 Y! h, e* G$ K, z6 x
mapStudent[1] = “student_one”; J/ ~3 Q" U8 v! m
mapStudent[2] = “student_two”;
6 y- k5 h( \0 K- N5 ^ mapStudent[3] = “student_three”;0 S8 q) W. X% |! X
map<int, string>::iterator iter;3 v; A- q! t1 ?+ D
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
) Z0 }( G. B+ A0 ~) u{( T7 _7 w4 K5 R& o. m- c; _' r6 G
Cout<<iter->first<<” ”<<iter->second<<end;
( [9 K2 t2 J. ?' t' T}
$ j& C9 D9 Y! P. d. U: q}
0 L( b4 t) M: c" E以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
2 [) N& a6 V% k* g! w* a" s8 lmapStudent.insert(map<int, string>::value_type (1, “student_one”));
4 g4 i- I+ J- ~0 }+ Y0 EmapStudent.insert(map<int, string>::value_type (1, “student_two”));* M; T3 T, `$ A7 P7 [2 I
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
6 N& n/ H& ^0 c. _2 LPair<map<int, string>::iterator, bool> Insert_Pair;
0 j* V/ `# s) R% j9 s1 m1 QInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
! _3 s6 I U- t我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
) S5 `8 a: D) D1 U5 G* ^/ ]下面给出完成代码,演示插入成功与否问题0 H) F( u% v, ~4 s: d. P* R! E
#include <map>
! R' t" Q1 [( D; z7 A4 s' u! |. G#include <string>6 e+ _( d: F& h0 {
#include <iostream>! {" h3 }! k9 J0 s" q' M" e& O
Using namespace std;
" s4 y% P& E. D! S; n: e' n' bInt main()3 w& Y4 J- Q0 e
{
) e9 X2 D; o3 B; V& \3 Z Map<int, string> mapStudent;, r8 o) V" @5 i3 M/ A# y8 {; S
Pair<map<int, string>::iterator, bool> Insert_Pair;
, ^7 y9 E; Q p& H: b5 ` Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));9 M# e& \5 z7 g4 _
If(Insert_Pair.second == true)- J/ r, \" Q- u8 x& m
{
: g+ t) s8 o( V. g+ ~& e Cout<<”Insert Successfully”<<endl;( W0 f Q4 L+ X" V4 [# ` e
}
9 [1 X$ G ~$ }6 f5 B9 [3 X8 t Else Z% [4 M' z. a% O8 E
{
/ ?0 g9 G5 n( H+ Z& s! \- y5 Q Cout<<”Insert Failure”<<endl;
r$ [. P* a: |2 h6 W$ r }! l% ^+ j% s+ o, y1 G0 ^
|
|