|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数$ ? _+ a6 W$ l5 t
Map<int, string> mapStudent;8 r- ?- @* b. B
2. 数据的插入7 m, Q- B$ n. a9 m3 u
在构造map容器后
: m: j0 D: n7 Z. i) F: G第一种:用insert函数插入pair数据
( r; P: |' V$ \3 ~4 |#pragma warning (disable:4786) ): [8 L) v. w) O7 }$ z. I
#include <map>
$ R/ h3 E; g( F$ b0 W! U' V! R#include <string>: b. H E2 Z' b" x9 |
#include <iostream>
; R6 s! X% e) j3 M5 R7 E9 y& zUsing namespace std;8 \' Z# c$ {& m4 o9 M" P, q
Int main()5 J! `0 A) O Y( Z
{
+ I" {, b& J& v7 |+ G" d8 Y Map<int, string> mapStudent;; X6 ?# j* f7 O; M
mapStudent.insert(pair<int, string>(1, “student_one”)); @5 }( [- f& M! [& O3 O
mapStudent.insert(pair<int, string>(2, “student_two”));
" U/ ?- x1 _- J4 v7 @ mapStudent.insert(pair<int, string>(3, “student_three”));
2 k! B1 s8 X& J5 \ map<int, string>::iterator iter;- e: T. I* ^& F; x1 ]" M
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)6 Q8 J8 D5 {( h2 _0 v! P, y
{2 x$ H* d& e d" L3 K) o1 c& r
Cout<<iter->first<<” ”<<iter->second<<end;( ]+ g$ a1 T5 j5 F6 h+ k# {6 J
}
/ J* U1 ?- x4 c( P4 X% m, a9 v}
4 P# E, h4 e8 B/ O! q& J* j第二种:用insert函数插入value_type数据,下面举例说明
$ _! n% [+ h N( \* c h3 |#include <map>* q) \+ c i& `% L6 }7 M0 d7 _3 B
#include <string>
: z* d4 h& U3 |1 E4 v$ P7 h* S#include <iostream>1 m4 U3 O3 f% y6 f0 Q" ]2 z
Using namespace std;
* t4 [; s. d8 [- `/ `9 J1 eInt main()
, G1 }- q' s* X2 J{ s1 h) O, m* n; \. j2 V/ Z S
Map<int, string> mapStudent;
+ K+ ^! s9 R, D) v7 a mapStudent.insert(map<int, string>::value_type (1, “student_one”));
7 A. H( |8 ~6 V5 d4 z mapStudent.insert(map<int, string>::value_type (2, “student_two”));( i6 H# _- K* j: h
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
* e" U* [0 i; e0 M map<int, string>::iterator iter; R3 x1 s3 R* W, m( _2 i6 c
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
5 c2 p3 I) C: `& B7 K% L% d{% g+ P/ ?8 a' W& |0 X
Cout<<iter->first<<” ”<<iter->second<<end;7 a1 N4 Q, V& _) _+ G
}9 J6 C2 x7 Q: O; y& Q
}- H. L7 ~/ \( l9 `. K
第三种:用数组方式插入数据,下面举例说明
* u+ j1 B* Z( ^# [/ \/ i#include <map>
) U& B7 u0 l! q x8 {$ P, p/ `#include <string>
7 R) P1 h$ k. Z0 r U' Z#include <iostream>3 w% ^6 L( i* F" E- {! F, y
Using namespace std;6 W: A$ x3 d% A
Int main()
' v0 J+ X/ t0 @( Q0 Q1 c+ x{; ^: A* P" ^) B; b* f% w" {" c
Map<int, string> mapStudent;& q2 _- I( A1 B5 V; Z
mapStudent[1] = “student_one”;. e5 y1 W L0 b
mapStudent[2] = “student_two”;' Z) ^! w2 V1 ?: \; |
mapStudent[3] = “student_three”;
n! U- u3 C2 ^8 s. g5 T3 E map<int, string>::iterator iter;
1 L* n* k- r! J# p, D( y for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
! w; H. G# M& s{
0 C( j9 d: k J3 i7 v Cout<<iter->first<<” ”<<iter->second<<end;. m. C I! U# p/ I; z2 e. F1 ^2 G
}
# c7 E' o [( @6 c* S0 m% a- C}' J% a8 ^% k' Z1 {: C1 C
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明3 m, U- P" W3 a
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
; Q+ i! C7 `* z: E" emapStudent.insert(map<int, string>::value_type (1, “student_two”));
+ ^- F# J7 J5 c5 }, c上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下) Q Y N' K/ B& `
Pair<map<int, string>::iterator, bool> Insert_Pair;
2 v& m9 r4 j) J* g# D8 CInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
- H4 a% t: P* G/ ]9 Y5 o* O' U我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。# x, W5 E) L& S. W4 s
下面给出完成代码,演示插入成功与否问题
5 a& i- d8 k3 w. |; h6 P#include <map>
' w; P% S9 s. A P#include <string>2 ?/ U& l* q. U5 j8 ? P
#include <iostream>
6 T$ v( R9 w& X4 P6 @4 n: w1 LUsing namespace std;
3 \7 k/ O& s" P$ B5 M Q8 Q$ _9 e7 AInt main() ]. M4 c& y! p( o) e: n2 y0 i2 m8 @
{& B& U1 j5 r: T5 a
Map<int, string> mapStudent;3 E7 M8 K }, C3 |. M! M- o
Pair<map<int, string>::iterator, bool> Insert_Pair;! O1 a+ |. S3 H$ b* |- D+ f3 \
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));9 D$ V- A+ s# G3 u+ Q* m
If(Insert_Pair.second == true)
; q L4 r. e% J2 i {
0 j8 z% t5 d8 l- i' S9 P7 T' `0 |1 C Cout<<”Insert Successfully”<<endl;
) Q" E: \" t4 G: `9 V }! D$ [/ w; \) ?% b
Else& x% L: i8 f* u% s+ x" E* |5 R( y( ^
{
+ _7 i! a, H, ] f$ s4 Q i% J; k Cout<<”Insert Failure”<<endl;( H4 m+ n' x' U7 N9 W. y+ n
}% s' O9 ^! b0 V1 W
|
|