|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
1.map的构造函数
3 T, t) l; u& [. F& LMap<int, string> mapStudent;
) S5 _5 _+ D) y; M- R% i& Z2. 数据的插入
2 ]" I" a/ j2 ?6 x6 l7 [在构造map容器后
' L' G2 K$ Y, l/ [$ L+ u; z第一种:用insert函数插入pair数据
0 s u9 Y" C4 N, _/ ~1 B#pragma warning (disable:4786) )$ _' T8 m8 Q/ V# Q; }( J; v$ c) F O
#include <map>, m- }7 R- f8 ^( j9 P( {
#include <string>
% n0 o: D9 H' @4 l( ]. y3 k* z#include <iostream>
* j" f% y8 O/ r5 V) MUsing namespace std;
$ h3 {$ W9 A( q4 \* h8 H/ lInt main()0 \1 |4 p# m1 w4 [- r
{
+ K% E6 h- I3 X2 m m Map<int, string> mapStudent;
- i, [) d5 q" _6 V& f F8 h" }1 }% _: t5 i mapStudent.insert(pair<int, string>(1, “student_one”));) X) W( H8 |2 `7 ^, i: P t4 A
mapStudent.insert(pair<int, string>(2, “student_two”));
0 G3 |3 i1 K: q( z# z/ @ mapStudent.insert(pair<int, string>(3, “student_three”));: i |2 M b, ]( h8 I2 h* q
map<int, string>::iterator iter;
; v: v" x# H- L5 N% A; | for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)3 x2 k+ i' W+ i! ~- k" }
{1 K3 i2 Q" Y! p2 v1 j
Cout<<iter->first<<” ”<<iter->second<<end;: x Z7 _, m* y( i: H$ O5 ?
}8 p; a3 t0 A( k$ t: c% q; ?
}$ h0 Z9 H" B5 ^7 N8 s7 u ?
第二种:用insert函数插入value_type数据,下面举例说明$ z) q: P1 ~, G+ ?* v
#include <map>
' { y% \+ t' h1 L: O# `3 r/ b# R' S, _#include <string>6 y O" a! V9 u! L( }
#include <iostream>7 b4 _/ ]% e) i0 W
Using namespace std;
( t$ |6 S3 X1 }( d" aInt main()
3 ?) M$ }) S; }7 P{* ^+ B: ~; r9 L5 p5 o4 K5 ]
Map<int, string> mapStudent;1 E! N6 r, \- M0 S7 B! M
mapStudent.insert(map<int, string>::value_type (1, “student_one”));! U3 K; X# t- X! H' {# ~0 x
mapStudent.insert(map<int, string>::value_type (2, “student_two”));
: x! C0 P* p& z) I( Q' d mapStudent.insert(map<int, string>::value_type (3, “student_three”));% g5 T1 Y! O% D9 f# v) [0 S0 k
map<int, string>::iterator iter;
w4 ^& j5 f7 ~ for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)1 F" l X: _. w1 k4 z$ C
{" K& V: d2 A: t) m
Cout<<iter->first<<” ”<<iter->second<<end;
4 i D. T# B/ |6 F; y) ?$ d}. G; X1 e# o$ M- c; K( ?, ]' ]
}9 k+ N3 F& l* J2 n8 P
第三种:用数组方式插入数据,下面举例说明
/ X! _4 |3 }" Q; y#include <map># W# C( x) o! v/ I% A
#include <string>
* V1 x6 {8 t0 O& u9 r#include <iostream>
" Z/ f- T, y0 i8 R* Y4 Y) vUsing namespace std;
& N& m0 f* O0 }) L) jInt main(); i: ^8 |1 A) X- \
{
5 s/ q, k. I: u; Z8 R. z Map<int, string> mapStudent;* @( _$ Y# P5 W. i
mapStudent[1] = “student_one”;" P/ N7 y1 B4 {1 d6 l. m
mapStudent[2] = “student_two”;" v# t# a) H1 u1 G2 J; C
mapStudent[3] = “student_three”;
8 _8 d& z: G1 t U5 ?! J map<int, string>::iterator iter;
{5 G. l5 B7 W; ^. \7 | for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
4 _% O1 R/ T8 ~% y{
* e7 k% v. i$ R# s- a$ w* \ Cout<<iter->first<<” ”<<iter->second<<end;7 ^( [% s( R R+ D. T4 G
}
- C" _0 Q( S; k4 m& L9 z6 b}* ~; P. i6 ^$ X: |5 w
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明6 v S7 B1 |" B' |) q) l
mapStudent.insert(map<int, string>::value_type (1, “student_one”));* U5 H# m+ N* t' a# |
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
. d% a3 H" g* A {+ B4 n上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
8 Y8 {# U" ^! D0 r5 w: m9 Q* @Pair<map<int, string>::iterator, bool> Insert_Pair;
T& `3 y5 D3 m. h' Y M' kInsert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));/ F3 Z; N6 ]4 q' Z9 F
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
8 H5 [' U6 Z! @8 a& m下面给出完成代码,演示插入成功与否问题9 v& U- H# A$ k4 N8 s1 K7 A& a
#include <map>. v( j. c) v4 l* Z% q; T# W+ k
#include <string>
1 c: w6 j/ f' x2 j5 I9 D#include <iostream>; \( e7 N3 L! s# r, u
Using namespace std;+ i0 n+ J& b" j* c6 H/ `4 p' I
Int main()/ I9 `! x4 u! h9 o/ h
{% ]/ ^* h, K2 Q0 G! Y
Map<int, string> mapStudent;
4 ]+ ]4 ^" N/ _Pair<map<int, string>::iterator, bool> Insert_Pair;
5 M! W# Y. F! n: Z Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
2 N9 U' |$ h, |' ? If(Insert_Pair.second == true)
8 h, C) w3 U% ^4 K/ L {9 ?! W5 C/ W) j/ l/ G
Cout<<”Insert Successfully”<<endl;7 b& R) O6 N& I
}( L' f1 ]0 j8 l4 k i$ u( _
Else
. B5 `2 G8 A8 Q' w {5 e8 g* t$ C0 N8 _" T' `
Cout<<”Insert Failure”<<endl;
# B+ B+ B: f% e% T% t/ S3 j: M }
. K5 ~+ Y/ F; a. \' v |
|