请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;, E4 P, _; x7 z5 g- I' Q
map<string ,int>mapstring; map<int,string >mapint;. E$ b: }4 n; y, b# C: C6 y) u
map<sring,char>mapstring; map< char ,string>mapchar;7 B6 o' u! h# T+ u2 H" N
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
5 x0 O ~. u& j" c0 \1. maplive.insert(pair<int,string>(102,"aclive"));
4 o Y, V9 y6 s9 ]& L- D6 ^( H; [2. maplive.insert(map<int,string>::value_type(321,"hai"));" P$ G: }: y8 ^- ]2 Q2 d
3. maplive[112]="April";//map中最简单最常用的插入添加!& V) y: Y3 [* E) h6 ?7 d
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
- {/ L. q* R/ Pl_it=maplive.find(112);//返回的是一个指针) a; Q7 d9 C9 N: y. W5 g& d
if(l_it==maplive.end())' \- `' b4 \% J& x! G% I9 n
cout<<"we do not find112"<<endl;
* L1 \1 T2 ~, R$ Telsecout<<"wo find112"<<endl;
3 ? x; R! e y, t
1 \ k; e5 e/ b" U; @% ~* r4 y& gmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
0 c% o2 B$ J5 x1 s: V 4. map中元素的删除:! r) U8 ?' x* _/ o$ M
如果删除112; D4 i' g+ U; Y- O) h; g8 P
map<int ,string>::iterator l_it;;) J+ M7 H( R$ ]7 D4 O8 e; B
l_it =maplive.find(112);
I6 ^, W: X# _' f, X1 t$ z% j& fif( l_it == maplive.end())
- }3 j! d( x. s8 C) b1 Ocout<<"we do not find112"<<endl;8 h% o7 N7 n1 \
else maplive.erase(l_it);//delete 112;
3 R+ n @. Z* X c, |. N 5. map中 swap的用法:
" a) b8 F( H' @0 H0 ^Map中的swap不是一个容器中的元素交换,而是两个容器交换;
' d5 z9 ?: o9 g$ ^$ yFor example:$ b& R G7 G8 x( O- t; x* |
#include<map>% k0 Z4 ?0 G) k2 d2 z
#include<iostream> usingnamespace std; int main()
4 ~8 b7 D5 p$ _$ u$ o0 R! T3 c{
9 c8 Y8 c! s" c% [6 y/ Mmap <int, int> m1, m2, m3;: C. Z5 y( Q/ ?0 q L
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
' V7 w) O* f; O% Q$ h! X1 z! F( C) Im1.insert ( pair <int,int> ( 2, 20 ) );6 W) @2 ^8 K$ t1 Y% m: i3 e) G
m1.insert ( pair <int,int> ( 3, 30 ) );
$ h* |2 p& _8 y km2.insert ( pair <int,int> ( 10, 100 ) );
0 g4 g7 E" J0 \, q/ p- ]7 }m2.insert ( pair <int,int> ( 20, 200 ) );
; Q0 K: m9 l0 D2 P, S+ L. Y9 ^# S2 _1 y/ Tm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
5 t" C2 m) L" K0 Q% _for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
# E4 L: c4 a8 y7 Mcout << " "<<m1_Iter->second;; E) Y& m. m& N1 m3 R! b+ R
cout << "."<< endl; // This isthe member function version of swap
$ _# }! ~3 k( b+ [- L// m2 is said to be theargument map; m1 the target map; R/ @6 g& g& C) h9 Q! @
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";4 B# d K( q% @; x8 V }
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )% e5 Z) F& z$ E. C; z1 V0 h
cout << " "<< m1_Iter ->second;" m/ j3 J, g8 j+ U
cout << "."<< endl;
; D& f0 f$ z' _' u; B4 Ccout << "After swapping with m2, mapm2 is:";
$ V3 l1 u8 b& b2 {( J' sfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )4 @# K- ^4 M8 H h/ a
cout << " "<< m1_Iter ->second;
; M* t1 F( M' {0 fcout << "."<< endl;
( ~, t) I0 M! l0 @: V3 h" p, c
// This is the specialized template version of swap
" D5 k3 m/ v+ i- O4 D* v' k. B5 h8 Tswap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";$ J5 e/ |8 B8 E5 P7 W
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )) ~2 G& f8 B% ?' ?2 \* P
cout << " "<< m1_Iter ->second;
; g. j* H+ \# Q5 d' I$ i+ o5 Ecout << "."<< endl;# G$ l2 C7 p4 k, Z3 D3 A2 X
} 6. map的sort问题:
3 |0 l- I1 q" O0 M" ^6 l) oMap中的元素是自动按key升序排序,所以不能对map用sort函数:
: D4 z7 h8 R" Y! n( s9 x& q7 p/ ?For example:
% [3 D+ a; l% R- b7 }( H% R/ D* ?#include<map> h$ Z3 _* u* }0 z: H3 ^4 \) L" T
#include<iostream> usingnamespace std; int main( )" L- O1 Z `+ g) V5 R
{
) ^3 ]) M e# o5 pmap<int, int> m1;' f V. r/ G) L$ R- p8 \2 f
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
# I4 @ d' X4 c0 fm1.insert ( pair<int, int> ( 4, 40) );2 i/ _+ V7 x8 s
m1.insert ( pair<int, int> ( 3, 60) );( R1 e4 Q( ~+ n, s+ Y
m1.insert ( pair<int, int> ( 2, 50) );
3 S( l2 [5 ]+ a2 B" c$ y1 gm1.insert ( pair<int, int> ( 6, 40) );
; p5 N! j8 A; a$ [m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;$ K) U/ n. f$ [ I' @
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )& a" D+ Q4 P3 `
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
2 `6 E& p8 Y) {7 c7 y- u# k9 q
5 s/ n- c, `9 X- m% I, |9 O" `}2 R5 c$ ^4 g: q) ?9 z* N" e1 V( G0 k
The original map m1 is:
- i: j7 q- u( l6 ?1 r1 20
3 X2 x8 |& j6 V: i' |2 50' Q: |$ W2 x7 l: |( U" _, d6 A. ?- B
3 60
* t6 g( u7 A/ h; d- \9 t4 40
. o+ B6 V" H! }, F& K9 B9 k6 408 E5 G5 ?) L5 q0 M
7 30 7. map的基本操作函数:
, F( s" U9 P. i' d: D7 [, OC++Maps 是一种关联式容器,包含“关键字/值”对
! j3 f: m3 R" b' C8 Rbegin() 返回指向map头部的迭代器
! r/ _3 H! r+ Y+ I3 n: q$ dclear() 删除所有元素. g* G N+ I" f& T
count() 返回指定元素出现的次数: I& y% _! }: u$ N6 B
empty() 如果map为空则返回true
( p6 G" e- R& y# n& I( u7 D* Dend() 返回指向map末尾的迭代器
9 @/ N+ t2 \ r* Jequal_range() 返回特殊条目的迭代器对
& H5 W& _4 B# _# b5 Ferase() 删除一个元素
; d& ~+ v- n8 S7 m( u2 Jfind() 查找一个元素
) }' S R( G7 j! Z: s; I0 ^1 zget_allocator() 返回map的配置器" d U5 S. b( x4 r
insert() 插入元素
2 j, m% _* z( H7 W( Skey_comp() 返回比较元素key的函数
* \5 V: a& W* T4 N# ~; V. Dlower_bound() 返回键值>=给定元素的第一个位置
9 s2 a/ {9 G8 f/ {& qmax_size() 返回可以容纳的最大元素个数. q: F: a. o2 ^" S5 f
rbegin() 返回一个指向map尾部的逆向迭代器$ c7 U( o8 l; f- K7 ^" u: b6 Y9 B
rend() 返回一个指向map头部的逆向迭代器+ D8 W, F Q) l2 U ]* |- Z
size() 返回map中元素的个数
- d, O$ O( O( Q3 w! T4 b6 }swap() 交换两个map
& v: _% L1 {( B! U* r Gupper_bound() 返回键值>给定元素的第一个位置
2 F: o6 b) P3 a1 g% yvalue_comp() 返回比较元素value的函数 0 Y2 ]# @ t/ Q4 G2 ~# k8 O
|