请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
9 J9 \! e- |6 \map<string ,int>mapstring; map<int,string >mapint;8 q8 y* x0 e; I3 r, B9 B( B. F
map<sring,char>mapstring; map< char ,string>mapchar;) t% u5 H- O4 T, Z& {9 C0 M
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;" W! `* \: t1 x# r! _- `& ]
1. maplive.insert(pair<int,string>(102,"aclive"));
% _: I/ r' J& p% H/ r: [$ S2. maplive.insert(map<int,string>::value_type(321,"hai"));
( N) d2 B, |7 s! r0 k' L" v3. maplive[112]="April";//map中最简单最常用的插入添加!
# ~) R6 n9 G @1 P' |: J4 \& X8 t 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; * ^' N: T N# O/ k
l_it=maplive.find(112);//返回的是一个指针 s% ]5 V2 s: i- Q% _
if(l_it==maplive.end())! h6 i5 I4 l' T5 i) w6 F* a
cout<<"we do not find112"<<endl;0 o1 Z! ?+ L) B& m) ?" s; `6 E
elsecout<<"wo find112"<<endl;
# U" S7 T6 q& F% p0 m, c . _* ?/ e, A" {1 y; {" \" \
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
6 o# m0 N5 r* W% J: `- X- k7 Q* w 4. map中元素的删除:3 L2 |/ w; R% U+ x) [' Q1 O
如果删除112;
% ?7 r% O! z3 ^, Zmap<int ,string>::iterator l_it;;4 a4 H+ M$ E% l( w8 S
l_it =maplive.find(112);
1 J! x: E# W/ E1 I& s9 H6 |if( l_it == maplive.end())' {6 @, O% ]- L* D2 e- K, L
cout<<"we do not find112"<<endl;9 p+ h" C0 Y# ^: p3 `# E/ g
else maplive.erase(l_it);//delete 112;* r4 E) Z# _3 v7 F, G0 U
5. map中 swap的用法:1 \8 S A9 Y) s4 r3 W
Map中的swap不是一个容器中的元素交换,而是两个容器交换;$ s8 p( B- m! e$ \1 ^, a& i! A* G( e
For example:
/ R1 t$ S0 {( M! Y+ [* E#include<map>
% |3 a3 e' V9 z; ~#include<iostream> usingnamespace std; int main()
4 s- X& ~4 i# B( B* b0 W{. k0 l; |0 l- V9 h2 G, i
map <int, int> m1, m2, m3;5 e* W9 ?# I1 L8 ~
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );0 f% S6 o* b- B4 w
m1.insert ( pair <int,int> ( 2, 20 ) );; D! q9 C4 ?, T O/ C
m1.insert ( pair <int,int> ( 3, 30 ) );
6 y: R3 L7 p8 t4 Q5 L( im2.insert ( pair <int,int> ( 10, 100 ) );, {) k* q/ p; U5 f" B
m2.insert ( pair <int,int> ( 20, 200 ) );0 z1 x( L$ j* }4 j
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
6 k8 t% G( L' d) P3 \4 Y7 qfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )" C$ q* n% F9 I* }, Y: v. S7 y
cout << " "<<m1_Iter->second;
x3 ^. r3 C w8 a6 z( m" rcout << "."<< endl; // This isthe member function version of swap
, B$ `3 c- ~% _" \// m2 is said to be theargument map; m1 the target map
+ [8 u3 C" t" q, @# bm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";& F. B8 g2 N* u& U6 y
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )$ H3 w2 c/ D S+ s4 n. v8 V9 k5 k, ~
cout << " "<< m1_Iter ->second;2 Y! [+ V! m) }6 H N5 C
cout << "."<< endl; 7 S4 |: j8 F7 x g4 U' E
cout << "After swapping with m2, mapm2 is:";. m, N6 f; }) d( \& r( `* G
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )6 h' J" e4 Y2 K D6 h$ Z+ }
cout << " "<< m1_Iter ->second;
" H0 H8 |7 o# _ P$ d0 |5 w9 j2 i, Icout << "."<< endl; 4 ?, ~3 }( p* p* Y! u3 I, _
// This is the specialized template version of swap
; @. i% O# J* e7 u- tswap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
! o) b8 {& i: F+ X* Wfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
4 i7 J3 u2 [' b0 H' O- j; e- T/ p. zcout << " "<< m1_Iter ->second;5 z6 B4 \" d. X# T
cout << "."<< endl;7 l3 {/ q9 q- w, Q
} 6. map的sort问题:
3 B5 {8 k- R) X/ dMap中的元素是自动按key升序排序,所以不能对map用sort函数:
5 a" z; W% m: e1 H# B7 V8 r. VFor example:
) m5 G& ]6 T2 T/ r#include<map>
3 |+ g# R2 `6 o- `5 _#include<iostream> usingnamespace std; int main( )
+ w* V6 h- e& f{
# Y: z1 [9 p5 ^* ^6 R* Bmap<int, int> m1;
. v6 F/ W3 H* b& _4 B+ Rmap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
/ P, R7 ^% U ~3 ?! c$ C) I0 tm1.insert ( pair<int, int> ( 4, 40) );
! T* S; w5 n/ Mm1.insert ( pair<int, int> ( 3, 60) );: j' t4 W* }' q8 ~+ ^
m1.insert ( pair<int, int> ( 2, 50) );9 ^0 B# O$ \5 P; O9 v% V
m1.insert ( pair<int, int> ( 6, 40) );: {7 R4 @$ Z6 S4 K
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
, Z' \9 x2 \8 r3 Pfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
5 r* g, c0 T0 M6 ^( U! z! G$ C: Hcout << m1_Iter->first<<""<<m1_Iter->second<<endl;3 }2 f( I; \- Q: x0 j& ~) U
' I$ d; ] J9 T4 B" c! m, Q0 @8 R
}: E, T# E6 [3 D6 w
The original map m1 is:
% ~9 N" m5 Y4 J7 s1 20
( x3 J3 D. h" w. i2 50
) T8 Y0 H0 o" n# A+ l3 60
$ x2 `% A; |- K, v ~0 k0 {" V4 40 x9 @; L; R& x& @0 A, T0 s7 ?7 G, |
6 40
" K! W' y0 S7 b% r7 T$ X) a7 30 7. map的基本操作函数:, X3 P- J& e) F) g, N+ W
C++Maps 是一种关联式容器,包含“关键字/值”对
1 P6 S. @ J- T$ o( M5 ybegin() 返回指向map头部的迭代器: z8 o' ^# h! g, F
clear() 删除所有元素
2 _# M- L% f8 |. c1 `& Vcount() 返回指定元素出现的次数
1 f4 y1 o' y$ Wempty() 如果map为空则返回true
* a! N4 _2 Y3 l2 u; ]& X% |end() 返回指向map末尾的迭代器 s- P. G" S0 L7 y
equal_range() 返回特殊条目的迭代器对
6 m5 n$ f4 r) \! {+ X: qerase() 删除一个元素
; I' q( I& G7 vfind() 查找一个元素) R0 [/ I% K' W
get_allocator() 返回map的配置器
! v X7 D# S4 [- S- Jinsert() 插入元素
^1 H1 g- u/ p8 Kkey_comp() 返回比较元素key的函数
" O4 S' n9 X b! S+ Q# ]+ Z5 D0 slower_bound() 返回键值>=给定元素的第一个位置
8 l. h$ e9 ~2 D! Hmax_size() 返回可以容纳的最大元素个数
' D' P$ z3 U5 d5 m7 Frbegin() 返回一个指向map尾部的逆向迭代器
6 I+ }- ?% Z G0 T! _/ Orend() 返回一个指向map头部的逆向迭代器/ a% t' M2 Q* y# j0 F
size() 返回map中元素的个数$ O; b3 S, V& J( r, T( G% o
swap() 交换两个map/ F% v* [/ a; m3 N! ~: F; q
upper_bound() 返回键值>给定元素的第一个位置
6 N2 Z' o/ a7 [2 g1 @/ hvalue_comp() 返回比较元素value的函数 $ e7 U8 N0 C; k
|