请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
- Y' N# n; U6 d, o3 N4 V; Ymap<string ,int>mapstring; map<int,string >mapint;4 d* e4 B. o; S- X8 M
map<sring,char>mapstring; map< char ,string>mapchar; W6 {7 i U# n. G# P- Y
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;1 T% o" t* R3 N5 L, D* ]2 Y
1. maplive.insert(pair<int,string>(102,"aclive"));
A7 E3 C& A1 L) @9 M) B2. maplive.insert(map<int,string>::value_type(321,"hai"));( a/ p) A6 a" @. i7 j3 @. a
3. maplive[112]="April";//map中最简单最常用的插入添加!* d* _$ C, b7 e
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
2 W" R% G- B% ml_it=maplive.find(112);//返回的是一个指针
* N) P9 h! w3 I! a4 ^$ z2 Cif(l_it==maplive.end())* S' k, {" `2 H; [- W! P9 B0 |
cout<<"we do not find112"<<endl;
& P* g/ p, b3 K( G: ^- G* G: f/ q0 |& Welsecout<<"wo find112"<<endl;% }8 ]$ c+ G0 q3 B3 R! p- h
; U) G( ^% Y9 mmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;* M! R+ o9 ^ W1 ^, G
4. map中元素的删除:$ F j J' r T
如果删除112;5 @9 s+ M' @; y2 s+ ]) E% d( V' U
map<int ,string>::iterator l_it;;
8 J" R. i- A$ }4 q" Bl_it =maplive.find(112);
! P+ w- V. `' G! L# \! ~' o% Vif( l_it == maplive.end())
! [# ^) B) R. s, O' Z: S7 H) k) {cout<<"we do not find112"<<endl;# s0 }$ p- |4 v* D) H" y* C
else maplive.erase(l_it);//delete 112;) K) M# J5 f1 g ~8 T
5. map中 swap的用法:
6 a" v9 W5 J9 _' FMap中的swap不是一个容器中的元素交换,而是两个容器交换;6 i: p, ]! U& E) g" D6 x
For example:
6 W/ e! m8 M3 ~2 q* i#include<map>
, u; c X2 ]7 V+ o6 \( @#include<iostream> usingnamespace std; int main()
7 R' I, k, k, o4 k0 C( N{ Y& n- d3 \) \6 e G# H U
map <int, int> m1, m2, m3;; {4 a# i6 }. U% B }3 M
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
+ r' W' M& o7 e" U6 q) x- am1.insert ( pair <int,int> ( 2, 20 ) );
1 L A- g% o1 |" J" C% ~m1.insert ( pair <int,int> ( 3, 30 ) );
% c% }3 k: I6 W1 }+ d; Fm2.insert ( pair <int,int> ( 10, 100 ) );
. U5 s ]+ a* U2 [+ M, F+ D2 xm2.insert ( pair <int,int> ( 20, 200 ) );7 S+ w& ]) V+ l
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";+ u. Z( x6 B/ X7 D; O7 o
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )0 H5 T* L6 h- W- d
cout << " "<<m1_Iter->second;0 F+ H! @% P; Y* b
cout << "."<< endl; // This isthe member function version of swap3 D# O0 C# R+ E/ H$ G# P3 d
// m2 is said to be theargument map; m1 the target map
/ \9 ~. f! U' r4 S; l- y6 Lm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
6 n5 |8 c1 |: D" Efor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
* k( _" F4 n3 g; O% B% V% Jcout << " "<< m1_Iter ->second;
7 A$ \ _/ z; x. Acout << "."<< endl; / L, }" M$ C3 S- W b# ^
cout << "After swapping with m2, mapm2 is:";& j7 q$ ?! }+ V- A9 u- [
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
# U9 I! B0 F' }$ z4 Lcout << " "<< m1_Iter ->second;0 |# a9 Z7 F" B1 _" z. v/ \
cout << "."<< endl; 9 ~* H K/ G/ P* n( i
// This is the specialized template version of swap) l5 L L! f9 g5 }" J) C# J+ M! Z
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
, S d7 F! Z1 I, Z$ Pfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
& y9 n0 Q2 v1 o. Zcout << " "<< m1_Iter ->second;+ I# T6 J1 {2 C% p
cout << "."<< endl;/ i+ W; R6 l' K+ l7 l4 p" ?
} 6. map的sort问题:3 W4 E7 Q* J, s+ y1 e( `
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
& c. `4 ^4 L- J h1 n" L% |0 ]For example:
\! Z6 q/ A9 F#include<map>
9 T1 q2 `( M- l/ g#include<iostream> usingnamespace std; int main( )( I4 [) D3 M t9 h4 ^, ^7 o9 k- b3 P
{
1 {% ]( R3 B2 j" `map<int, int> m1;0 }" g8 r+ W; e/ r
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );2 b) O1 I- E) E5 ^
m1.insert ( pair<int, int> ( 4, 40) );3 V; e8 s7 }! W. |0 O
m1.insert ( pair<int, int> ( 3, 60) );' @, E3 P* V7 I8 g: @
m1.insert ( pair<int, int> ( 2, 50) );
$ x4 Z+ `% }4 l% c( Vm1.insert ( pair<int, int> ( 6, 40) );! V' v" _: b+ G9 s
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;4 v) ]* o( q3 D6 Z: v3 F* J
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ ) J+ U v& ^7 q4 O! O* I3 X
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;( ^/ Q- t. G! s+ o
& U9 e3 ?6 u( Y0 C2 Q; M" C! C, G}
% C; @# h9 r5 `3 i The original map m1 is:
# q' B% [6 C$ H1 20/ j7 }; I) T1 h; X$ G! z
2 50
- ]8 |! k* ?1 U$ W5 W: K! ]; }2 k3 60
3 a- ?' s8 o: V$ e% S+ V4 40
$ y. \$ V$ u2 T6 40
1 v3 k: l3 b! x# J7 30 7. map的基本操作函数:6 r' @" L0 w+ \/ D9 J5 \) c: n( {
C++Maps 是一种关联式容器,包含“关键字/值”对
! f4 d, w8 b( [+ C) Q6 Xbegin() 返回指向map头部的迭代器
1 I& U2 R! p+ ]6 S- n9 F) eclear() 删除所有元素* t. i* B& L) q9 V
count() 返回指定元素出现的次数
! _0 y- Y) M# C& y1 a$ c |empty() 如果map为空则返回true9 V( g B$ x S/ n
end() 返回指向map末尾的迭代器
' Q$ O0 \# z& S) u$ i" r5 O5 Aequal_range() 返回特殊条目的迭代器对 w, U4 ?" ^6 K. P
erase() 删除一个元素/ W& K1 k3 {$ Q8 m' b
find() 查找一个元素! Z/ P3 q; B* r4 L
get_allocator() 返回map的配置器
& A+ `( W' M- o' s$ [insert() 插入元素" D1 [) _- E% M& A5 h2 i9 O
key_comp() 返回比较元素key的函数
; Q( y5 V. N k! jlower_bound() 返回键值>=给定元素的第一个位置
$ G _$ _ q/ N6 S& T, Gmax_size() 返回可以容纳的最大元素个数6 F- A1 I0 ]( K$ B+ o
rbegin() 返回一个指向map尾部的逆向迭代器
( E! ]- A# n: u4 {8 _rend() 返回一个指向map头部的逆向迭代器
/ O; ^) \+ ]& [0 d( {2 ], zsize() 返回map中元素的个数$ b" c4 ~! i* v7 w
swap() 交换两个map
+ I& M1 B2 ^/ ?2 j) ~5 Iupper_bound() 返回键值>给定元素的第一个位置
5 ]) U4 f4 W! m4 J4 c2 g" U, qvalue_comp() 返回比较元素value的函数 ! P/ d; t! V4 H. O. D
|