|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数" D2 C3 U5 J7 r( @1 u- x
Map<int, string> mapStudent;) A( j9 T; z4 G, Y
2. 数据的插入
; |: E2 o7 ?& B- l5 ? ?在构造map容器后( Q/ k" k- B9 J& y- h8 o# v t2 k
第一种:用insert函数插入pair数据
) `7 u5 F& y t, T% e7 z8 H#pragma warning (disable:4786) )
. I. d. d- H* K! |9 z w$ L#include <map>' F9 r$ w6 l0 ~) k4 H1 i
#include <string>
" G' W( A/ z' r% t#include <iostream>2 n6 y1 M3 {2 ^' a
Using namespace std;1 w7 n" x$ |: e. q" R8 m8 e9 ]
Int main()* R) b9 v E0 I: `* i
{
1 p$ l- }$ O3 t5 d: F Map<int, string> mapStudent;
) u; X. a+ H- p" [% _ mapStudent.insert(pair<int, string>(1, “student_one”));& u3 u. b9 c3 {* [/ e5 T; e- y
mapStudent.insert(pair<int, string>(2, “student_two”));5 a) u4 U7 _. }
mapStudent.insert(pair<int, string>(3, “student_three”));
9 V9 }8 h5 U7 R map<int, string>::iterator iter;
' `: K: p) h* P+ G$ U& a for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
: M0 ]0 O4 G* C+ u{, ?7 ~3 u( g2 p; ]! f
Cout<<iter->first<<” ”<<iter->second<<end;
- i/ U) b) B- u4 o/ i3 O. T}7 s: J9 K x7 u% O$ i. }
}
" |; Q" @ ]+ v- F$ X$ J* h2 B第二种:用insert函数插入value_type数据,下面举例说明
. v" F7 B0 g' ~, V#include <map>
3 D% f) A+ H- b# p+ `* ]; r#include <string>
: E2 W9 B8 W% I, G#include <iostream>
, d# {# \1 h$ h- z" ?1 j- T2 z+ KUsing namespace std;8 q! |1 d" i" ^" x% x9 n0 Z# a
Int main()/ \3 D) o8 _* j) W6 b( V2 i
{
8 d B) e3 V: |* u1 R9 K) F! g Map<int, string> mapStudent;
" r( F3 i6 s$ w: I mapStudent.insert(map<int, string>::value_type (1, “student_one”));
1 y% D4 A8 q% S" }" J, h& b9 I mapStudent.insert(map<int, string>::value_type (2, “student_two”));2 X2 ~/ z u5 p: _
mapStudent.insert(map<int, string>::value_type (3, “student_three”));8 \9 Z8 l! Q$ y C/ h
map<int, string>::iterator iter;$ j! _4 ^! Q8 K( V' [* {
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)4 c: t: n$ x0 Z! e& W; q1 Q
{
. e, G( z2 o/ ? Cout<<iter->first<<” ”<<iter->second<<end;. y* ^ I0 @9 o1 b8 x$ J
}7 X# O2 A8 ?0 f5 j% q7 R& [0 k
}
0 B0 ~. d5 U9 ^第三种:用数组方式插入数据,下面举例说明
' S9 P; G8 F+ p" n% W* q2 I# F#include <map>0 x" x9 R$ T, I$ ?4 u7 a
#include <string>
5 |8 V/ ~* x. L1 n$ {" F }9 r#include <iostream>
" J1 |2 f; i0 g- m0 Q6 M% wUsing namespace std;% E; z. K* w$ H: X5 o
Int main()
1 u" t6 y9 _. @3 V+ }{
7 k- b5 m* m$ C5 R0 h Map<int, string> mapStudent;, S4 J0 r' @8 C, U
mapStudent[1] = “student_one”;
# F) @: l/ Q5 x5 O/ p8 P. e mapStudent[2] = “student_two”;
# x* s D, h# E) o' f! o mapStudent[3] = “student_three”;$ L& |5 @, c# g: \) j9 ?
map<int, string>::iterator iter;
1 `5 S2 L# ], H5 l1 b for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
2 K: i% \3 ^- e' [1 W{% F: Z3 K+ e4 ^0 w. x1 r
Cout<<iter->first<<” ”<<iter->second<<end;
& C& v8 t4 C0 y) T}
3 o4 y1 l1 l- h! z8 A. ^5 t* v9 v}
/ C" R% G# @" P以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
: v$ ^7 b8 Y2 @( k6 KmapStudent.insert(map<int, string>::value_type (1, “student_one”));
# D) e8 Y" A( K) z8 f1 smapStudent.insert(map<int, string>::value_type (1, “student_two”));
! J1 \: k" v6 X) I8 G% j' ]( K" b5 T上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
! k2 V9 |. s' U+ ?7 `Pair<map<int, string>::iterator, bool> Insert_Pair;
& P1 F) o& u8 P. n. hInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
# [' Q& O9 k/ J( {1 D6 D我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
f5 a. ]# ~' G2 A9 d5 I下面给出完成代码,演示插入成功与否问题. v) }* o, n$ G- }" d& ~# T4 R A
#include <map>* d. q2 d: k: }0 I/ ]9 `: c
#include <string>% a" T; {6 z$ x- ?7 O
#include <iostream>. k5 |6 K" D# j" w; _* _ j" m
Using namespace std;
" x5 `/ `! ]' C, k# _5 jInt main()
8 v3 i, O% @8 m# `+ u{
" D: ?4 k; K! z6 m Map<int, string> mapStudent;
5 u/ G/ x' c! f" n5 r* ~( ?Pair<map<int, string>::iterator, bool> Insert_Pair;
$ Y/ P8 Q- L) o% L5 @4 z3 a1 f6 @ Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
! \/ H5 q4 P+ q2 Z1 z1 e If(Insert_Pair.second == true)
. u4 {+ D6 S/ s4 U" }5 o$ x {; A( t6 C0 z9 }
Cout<<”Insert Successfully”<<endl;
# n# M8 t* ]- ]5 Z0 F' Z3 k: D }8 ^9 A- U b& m0 v) S$ q
Else, H% T* t _* E9 n$ W+ G
{- Y2 ?! k0 p3 X1 K8 v
Cout<<”Insert Failure”<<endl;
' \/ G: f2 h j* j% b }
9 w0 o! \# T5 s5 s! Z( a6 B |
|