|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
" }! a& i _, vMap<int, string> mapStudent;0 j+ E5 R- u+ g! a6 }
2. 数据的插入. y4 e( Q3 J+ O+ O+ l
在构造map容器后
& L+ j" O9 _" [" G& ~第一种:用insert函数插入pair数据0 a5 u' q- `4 R5 ? v9 z
#pragma warning (disable:4786) ), F$ p( s6 ?2 W6 ?
#include <map>2 S, m# n! p) p9 O' p
#include <string>' h' p6 `+ i( g3 |8 D8 t
#include <iostream>
, E# g4 h) G A8 X4 oUsing namespace std;
* g0 U3 k8 H: F3 o5 l* K+ d! sInt main()6 G6 K, Q! L; Y' J C
{& }# d6 k9 N3 w$ z
Map<int, string> mapStudent;
; {( }0 B G3 W' w6 F+ Z mapStudent.insert(pair<int, string>(1, “student_one”));
# D3 J, q, i4 w: J# O- l! } mapStudent.insert(pair<int, string>(2, “student_two”));6 @$ ?/ R# R$ H/ d7 m
mapStudent.insert(pair<int, string>(3, “student_three”));
( r8 ~4 B' ^) f& E5 D w1 p& l map<int, string>::iterator iter; ^! c( Y0 p, b
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)4 m! c8 f( i7 Y4 W+ g: O0 {1 p
{
0 f& K8 M" R7 W3 S, [/ V, H4 \, ? Cout<<iter->first<<” ”<<iter->second<<end;
D% h5 N3 i" t c8 p3 a$ A}5 G& S8 i8 r0 l; a/ \) n
}# K. r( p- C* h: l1 Y6 _
第二种:用insert函数插入value_type数据,下面举例说明( X5 g8 J, Q" g" Y, O) V
#include <map>
! P1 ]. C0 E& z! b8 o6 S- X#include <string>
7 q; d# @" v- | ~' J$ k s) g" U#include <iostream># n; g, i. q5 q, b! M: L- V% `
Using namespace std;2 z6 k/ a7 X: Z5 A0 H
Int main()4 o" S$ Q( G, I, m* @
{/ U M, ?) b# b) ~0 f
Map<int, string> mapStudent;" `( i* a, d' z. B! m
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
7 Q1 @7 {- Y: X% g$ a mapStudent.insert(map<int, string>::value_type (2, “student_two”));
+ E& Y/ c# @1 _* i mapStudent.insert(map<int, string>::value_type (3, “student_three”));2 ~6 L$ J+ f, i
map<int, string>::iterator iter;( H* _2 P# q( C' B& x" N% f
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)1 G4 |+ h: Z `( p7 l
{1 r" |8 H; S3 e8 T3 c1 N# n
Cout<<iter->first<<” ”<<iter->second<<end;
; y4 ?& T# f9 m9 C- w$ d}
' Z9 g+ q2 r# i3 i' D6 D% ^}
w" }# `* L" B0 ^4 X第三种:用数组方式插入数据,下面举例说明- F3 c; ]8 b+ W) G- P+ S
#include <map>7 j& {, Z. I( b- {' z
#include <string>% E0 ?$ g8 R# H
#include <iostream>& A( Q7 d3 `3 N, Q6 m
Using namespace std;
( H6 p( B5 M" r! JInt main()3 Y7 e# ?, }. J; E, N8 v1 G- [* v
{
; K( f3 M: W6 f6 e6 L Map<int, string> mapStudent;
/ `& p* z. G# _; V) K' N3 e* _" E mapStudent[1] = “student_one”;7 C0 X8 z3 ]: {
mapStudent[2] = “student_two”;
& D! @; C" n& ?, t mapStudent[3] = “student_three”;
0 W+ i) y$ c V& m) _ map<int, string>::iterator iter;
% |: T z: Y& c8 ~( \( W3 Q* x5 A for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)& ?3 R3 J' L* Z5 K( ^6 \
{
: {! u3 N* h' ^$ o Cout<<iter->first<<” ”<<iter->second<<end;
, f2 W5 y3 N* t7 c- U( C4 G}& U) H; E+ n, t! A8 o1 t
}
7 L/ u( Y3 G5 Z- E6 j以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明6 R8 D2 d. p& O2 _% F4 h
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
& o* n4 Q! X, O* b' DmapStudent.insert(map<int, string>::value_type (1, “student_two”));+ U, A$ q X0 H
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下1 k- r! `' M: G! w
Pair<map<int, string>::iterator, bool> Insert_Pair;
% y) M- w0 N( y8 m4 G. z' @Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
$ x3 g% r9 ?# _& X4 n我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。+ |$ N, M. b& l9 p) D" V' @8 b
下面给出完成代码,演示插入成功与否问题
. X- P7 d6 `8 b4 z3 C* C#include <map>6 M( m' j9 c3 F N# e- i
#include <string>' k* O6 s$ M3 \2 V6 K5 E
#include <iostream>
# j( R: d9 V; {4 H2 P1 b& E2 DUsing namespace std;
9 C+ G9 E) p; ]$ N, I; F& GInt main()4 u% g# q7 P+ m6 w* s1 H6 Q
{2 m/ O% ~4 Z6 P: h$ \* _
Map<int, string> mapStudent;; l9 i( R8 M$ K" D
Pair<map<int, string>::iterator, bool> Insert_Pair;, p3 G% w1 q4 `% S2 f
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
% J! S4 h# x/ x If(Insert_Pair.second == true)
- G/ T' P9 D, ]1 f$ [5 Q% M {
- Y& Y. U, I$ c- [2 A Cout<<”Insert Successfully”<<endl;
) h4 U7 O9 T% ?4 c+ ~9 o; k0 P } z9 |2 U9 C. H: s" I" i
Else* }/ B& K4 p C f0 a) X1 |% r
{
( u" Z+ k/ t0 C* h& u Cout<<”Insert Failure”<<endl;
8 q& z. |; q4 a* N$ q# o/ ?: Y }
4 ?; M0 p- I p. P5 |# ]7 j |
|