|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
5 ^& g2 m4 ~* |6 a! SMap<int, string> mapStudent;8 r# o8 L7 d% U0 H% E
2. 数据的插入3 m! c7 H3 j5 q0 r
在构造map容器后
( c, [: q) \4 ]" R5 S第一种:用insert函数插入pair数据
' q6 }; ]. N6 `; t0 o L! A#pragma warning (disable:4786) )# Y3 @# H6 _5 W1 E2 ?
#include <map>
2 G! m; C4 {+ G& d2 o. R#include <string>7 w7 F A2 e. M- f8 Z' O
#include <iostream>/ O- W; K% B5 i h( V5 w P
Using namespace std;
# e7 o6 x! f. ~7 cInt main()4 ?% O+ X) t* T# m- y& b& |
{
T6 O2 C# ~8 }2 r# d! q; u Map<int, string> mapStudent;% P7 o" B, _' K0 M- i) [3 J
mapStudent.insert(pair<int, string>(1, “student_one”));" I! r6 Z1 [- S9 s# r4 n1 x
mapStudent.insert(pair<int, string>(2, “student_two”));
+ H6 m+ O- v( n2 n" s" p! [7 v mapStudent.insert(pair<int, string>(3, “student_three”));, S) e/ F6 D, `$ Z& i4 G1 y: ~
map<int, string>::iterator iter;, f; n+ S9 i! l. n o( n; T
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
$ g& v. f- j- t{' t3 _6 T- U$ K0 q: L$ ?( _
Cout<<iter->first<<” ”<<iter->second<<end;
$ C. h; O9 a x0 p) R) V6 W! l}
/ I1 |2 t& [3 F}
; O, ~7 ]) C7 j第二种:用insert函数插入value_type数据,下面举例说明
, P, G( [" w+ H9 Q' A$ g0 H9 @* L#include <map>- C8 Y, [( Z; \- S
#include <string>, j/ M3 \# w) w
#include <iostream>
4 i" s) l! `) bUsing namespace std;
' z& q, H& p) e: j3 e6 L( w( qInt main(); q4 ]; _' O0 \- c. U1 l+ W$ @
{
, H l5 v0 F+ ^( H+ Q. c+ N Map<int, string> mapStudent;1 B2 t" L' i/ T
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
: }4 T# p3 n1 Y. h7 v mapStudent.insert(map<int, string>::value_type (2, “student_two”));! `6 N+ M' g$ c2 C7 U- b5 N& j
mapStudent.insert(map<int, string>::value_type (3, “student_three”));
& s+ }7 k: Q4 Y7 ~; ^% f map<int, string>::iterator iter;3 X( s1 u e/ @- d: l m( i/ z
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)8 Q8 ?' w6 f. l4 E( U& L' W0 }7 w( H, p
{
. ^$ ~ E; J2 l: g5 ~% | Cout<<iter->first<<” ”<<iter->second<<end;
; m4 p/ N* a$ U}
1 J' v! I: [8 @5 T4 b6 P+ \}
2 u2 f; w% W- f2 N第三种:用数组方式插入数据,下面举例说明
& g; l$ _% y% w; L#include <map>- j; M$ ^5 V; I# C) z h7 m
#include <string>
( h% }5 N" Y s& }% M#include <iostream>' m! o- d3 x7 w1 ]! E
Using namespace std;- v8 b$ b' \; B7 S' b( d! f4 o% G
Int main()' s. w# q) v; y- x4 |. v) j
{
" e2 J5 x7 D6 u; _ Map<int, string> mapStudent;6 D" w4 x! Y- I! f$ l0 N
mapStudent[1] = “student_one”;" t2 E: c$ v; j6 r, z
mapStudent[2] = “student_two”;
6 W) c/ I6 y2 } mapStudent[3] = “student_three”;
1 S7 b( m* a; |8 n- M+ W2 V: y- ? map<int, string>::iterator iter;8 B f/ X) D2 I( Y# R# t
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
! B9 e. D3 J, J. T+ O' g{
- |% J$ U! @! Q6 F" h3 k+ E% b4 ]: ~ Cout<<iter->first<<” ”<<iter->second<<end;; N7 q6 j1 V! O+ K
}8 {5 _- M3 @4 A& j2 X' h
}6 u5 r o: ^1 ~7 s6 m5 Z
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
4 _, \, q9 v- z& WmapStudent.insert(map<int, string>::value_type (1, “student_one”));+ b! `, \5 K; p- _- w; f
mapStudent.insert(map<int, string>::value_type (1, “student_two”));- M* _3 T: I9 @& c3 | ]* {+ {
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下: V$ y8 O" F0 \# O( g2 c: b
Pair<map<int, string>::iterator, bool> Insert_Pair;
: l3 d* c% l% OInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
) y5 r5 f$ F! ?我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。4 w8 A! W# \% ~, n
下面给出完成代码,演示插入成功与否问题
8 J# ?' \7 K* |- a$ ` X& N4 B/ Y#include <map>5 g7 P" t5 \2 m* ~! E) Y
#include <string>2 t$ G+ I1 x+ I. e
#include <iostream>" s) G8 g( S$ _
Using namespace std;$ Y0 W. E% {$ t: M8 a; O
Int main()
5 y9 o8 R0 |5 M- R) d$ T9 F" V{
$ f+ I; r" ~6 K( l# q8 u Map<int, string> mapStudent;
7 q, M% r2 o; tPair<map<int, string>::iterator, bool> Insert_Pair;
- L- y! O! {3 n: v: S. W Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));$ i0 I" q4 h# E0 R( N- p
If(Insert_Pair.second == true)
W4 h9 s" H9 g2 H {
4 x: `+ V" M3 U# }6 u; y+ N; d' ^7 l! i Cout<<”Insert Successfully”<<endl;1 ?' G$ l- P J3 C7 K& @4 @8 m
}
/ f' F# [' y }" A# @ Else5 i% r) k$ D9 a3 y$ @$ M9 B' t8 J
{* o4 V. A! R0 ?9 f& y, e" G
Cout<<”Insert Failure”<<endl;
7 ?8 G: B" M. f }
. ]) V8 S, x$ S+ {$ X |
|