请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;: s7 I' y7 k1 A2 ~
map<string ,int>mapstring; map<int,string >mapint;) ] L# j p( X/ C! L, `/ ?
map<sring,char>mapstring; map< char ,string>mapchar;
; w& O ]1 A: x5 i5 y+ ]8 omap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
9 N- R( [, G6 j; Z( Y. Z) N1. maplive.insert(pair<int,string>(102,"aclive"));
- C: I+ @- V: D+ T U2. maplive.insert(map<int,string>::value_type(321,"hai"));
9 x( q3 |7 T; d- E3 f3. maplive[112]="April";//map中最简单最常用的插入添加!( p5 k& Z4 S9 E `2 N' h# k9 A+ Z
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
8 x" h+ Z# n c) Q6 ~0 X6 E2 Y9 dl_it=maplive.find(112);//返回的是一个指针
' m' {& l2 E$ U! }if(l_it==maplive.end())
- I$ `! D B3 Jcout<<"we do not find112"<<endl;
+ f* B' K& }1 @ x+ C- eelsecout<<"wo find112"<<endl;
: m" u1 d+ e+ [3 ?) e
3 P* I; r1 T) ]* H& D( t) {map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
6 k! R ?' Z& R$ y3 L0 U6 T$ y7 t8 F6 } 4. map中元素的删除:: M# T8 w5 i5 t9 j8 T' n
如果删除112;1 `& i; J% v+ n; R3 g$ r
map<int ,string>::iterator l_it;;9 M/ Q* `" P$ Q4 Y
l_it =maplive.find(112);1 Y) E$ v, @. G) T6 X! O# N8 e& ^$ l
if( l_it == maplive.end())5 j. Z3 G4 e& B! G
cout<<"we do not find112"<<endl;
+ E8 I: b4 ~2 telse maplive.erase(l_it);//delete 112;/ [5 T8 I4 B1 J
5. map中 swap的用法:2 f8 e5 t: N- p) O% P: }' q5 t
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
, ?9 b; G8 D% A. z( _For example:9 w' x! p8 V3 ]7 _. U, i
#include<map>
7 w5 R* C! w1 O; f# X0 E' W2 }#include<iostream> usingnamespace std; int main()
& x- v$ J) ]! O( p" Z{
8 ~2 T1 q' i2 s# imap <int, int> m1, m2, m3;* v* B* U) p% ? x3 c
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );" d( S$ X6 Q! F8 a
m1.insert ( pair <int,int> ( 2, 20 ) );5 b7 S/ V. R# h& n; F
m1.insert ( pair <int,int> ( 3, 30 ) );
g& ]! Y0 ~0 { I! `4 j/ G0 E6 U: hm2.insert ( pair <int,int> ( 10, 100 ) );4 @3 s5 s, v) k3 Y4 }
m2.insert ( pair <int,int> ( 20, 200 ) );
0 Y8 Y# U5 R3 v8 _9 V5 Mm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";7 u5 S( S2 h' _% y* \, n2 n; j# k
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
+ y. b! |, K3 l! F9 n! Ncout << " "<<m1_Iter->second;
0 \' n/ h& g9 I; i6 \ l) }cout << "."<< endl; // This isthe member function version of swap
% k2 W- U+ |" Y1 p3 a. o, W// m2 is said to be theargument map; m1 the target map
% a, M# q; I) a5 ^+ Lm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
' J1 `% s* {7 m) Z' ffor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )5 L* h6 w1 A$ w6 R
cout << " "<< m1_Iter ->second;9 d2 a: F1 [: ~' q
cout << "."<< endl;
Y& P, \& i: r0 b4 G' p/ @cout << "After swapping with m2, mapm2 is:";
3 F* }' w8 t" [+ ^9 \4 Dfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
5 ^3 ^% [7 O: O" z' \cout << " "<< m1_Iter ->second;
6 i7 y# D' G4 O: n( C5 T+ rcout << "."<< endl;
7 K0 G: ^1 r; C- h
// This is the specialized template version of swap
1 N D+ C0 c7 e/ z; L2 |swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
$ w5 ~' S. k# S5 K& S: Kfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )2 f% x% N; Q# C1 G
cout << " "<< m1_Iter ->second;0 Y3 B+ H3 F0 u) P4 k7 k" c
cout << "."<< endl;8 r# \) d+ [* F% j% J% B3 \
} 6. map的sort问题:! b5 e. ^. d {" N
Map中的元素是自动按key升序排序,所以不能对map用sort函数:& r, c; [& S9 f9 r2 ?) }3 k2 e
For example:
+ v8 t* X. F8 E3 b#include<map>
* Z: j$ V+ v- u# G#include<iostream> usingnamespace std; int main( )
' D, r: I8 ?4 _$ q7 t5 C' w{4 S& K, N, C3 F
map<int, int> m1;
' N% O# V3 G. f- H6 V# d) zmap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );% D9 z; p& l6 S D1 ]6 e
m1.insert ( pair<int, int> ( 4, 40) );9 w. h, L, E) F- Q2 G
m1.insert ( pair<int, int> ( 3, 60) );
) j5 { g4 }) ^) ^$ Am1.insert ( pair<int, int> ( 2, 50) );
7 k3 Y/ C4 B6 ?m1.insert ( pair<int, int> ( 6, 40) ); T& ~" j. F$ J+ T6 U% W
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
* c) j. c: F3 P5 y9 c5 mfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )) j3 N5 @7 ^; d; ?: {: f) [8 j
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;4 d/ v% e6 k- d( G ?) J( m
; {8 J9 K# S; m$ n}
1 C4 h7 Z+ F9 D. c9 G The original map m1 is:5 t- f0 E2 O; v1 q8 [" O! r
1 20
/ w, x5 F( k, B( M1 ~% p# U$ X2 50
9 @5 _3 J0 @$ m5 n& r3 60
1 {' r9 ~8 W k W7 F: Q- l6 A4 40; A4 i6 M5 e1 v0 a
6 40
0 M& F* a! d( X/ x! H: l0 S8 |7 30 7. map的基本操作函数:
! y0 n, P, p* m2 x8 r+ g% D* qC++Maps 是一种关联式容器,包含“关键字/值”对; K% c% a. @3 K" r, J
begin() 返回指向map头部的迭代器
. j. S6 g! `" M. J% [! bclear() 删除所有元素, u& Q. t3 a7 u' Y2 ?0 W4 q
count() 返回指定元素出现的次数
! x! V' U( u; k6 E% |3 iempty() 如果map为空则返回true
7 n9 o, K4 [4 \9 c E$ J1 K* G- W! Iend() 返回指向map末尾的迭代器/ O& t4 ]: \3 M; e* j, _5 M, a+ X
equal_range() 返回特殊条目的迭代器对# ^6 @/ _- P$ y( D8 S1 P( Q- u
erase() 删除一个元素
$ {5 z! U) E# K/ E$ E* Rfind() 查找一个元素& Y7 a3 m. `" _( ?- N4 y
get_allocator() 返回map的配置器6 w M$ M9 p& q2 n, S9 e
insert() 插入元素8 |/ v, S2 q0 h" N1 L
key_comp() 返回比较元素key的函数3 w1 d1 R1 {" r( A
lower_bound() 返回键值>=给定元素的第一个位置' C& P1 j# b! b1 _- ~9 u/ d
max_size() 返回可以容纳的最大元素个数5 T+ ^- z% i* J9 z1 l
rbegin() 返回一个指向map尾部的逆向迭代器. T7 C$ U7 Q, a2 W3 M v" z7 A
rend() 返回一个指向map头部的逆向迭代器+ [% `5 Z" Y5 @/ I- p9 {9 `* z
size() 返回map中元素的个数
0 r! a" @+ X* O: |) kswap() 交换两个map5 g9 J4 B. [& M% A8 h
upper_bound() 返回键值>给定元素的第一个位置9 E, w, H- l: b
value_comp() 返回比较元素value的函数
4 F2 ^' I1 o% ^( j |