请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
9 Z; i) y' ?; I0 ?: l4 kmap<string ,int>mapstring; map<int,string >mapint;$ i( m2 Q+ ]! \7 [: |# P
map<sring,char>mapstring; map< char ,string>mapchar;
+ `/ d: i- n0 xmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;. k' B, b( W5 I+ A3 u
1. maplive.insert(pair<int,string>(102,"aclive"));
5 a, }& v7 q. S2. maplive.insert(map<int,string>::value_type(321,"hai"));
! {* U) M+ k+ l' m. W+ V. C3. maplive[112]="April";//map中最简单最常用的插入添加!
4 _$ d% a2 v- t& ` 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
: i. U6 W* o0 N& i& G$ S4 h9 V: p% pl_it=maplive.find(112);//返回的是一个指针
9 F/ K2 ]! n' F; Q0 R& D2 z# Fif(l_it==maplive.end())$ V7 N/ I8 S% N- F& [+ U
cout<<"we do not find112"<<endl;0 a3 T8 o" O; p
elsecout<<"wo find112"<<endl;
5 R, I; w `9 } s* _) C X/ z/ q, ^
- v5 G+ G6 R d) w9 r& qmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
3 ^$ F7 _( Z/ M 4. map中元素的删除:/ r4 L5 ]6 {6 K2 M0 n# M/ Z
如果删除112;, T! N }' U$ Q, E
map<int ,string>::iterator l_it;;
1 g) Q# M4 z4 L1 H9 Hl_it =maplive.find(112);
7 v, X5 ?, }1 V2 i2 C2 n( P! W0 kif( l_it == maplive.end())
: Y r" M5 f9 Y6 S) [cout<<"we do not find112"<<endl; {- m# c3 i7 B9 m- ?) p/ @5 \
else maplive.erase(l_it);//delete 112;
# H( G5 d3 m% a$ |" \8 y2 z, d2 ~2 v2 h 5. map中 swap的用法:
: A3 U4 ~6 f$ a1 D+ J! x4 \# D5 PMap中的swap不是一个容器中的元素交换,而是两个容器交换;0 H6 q1 X2 n- {- N- v5 q2 ^
For example:
* ?) c( `1 S! Y3 P#include<map>5 x9 E; J& M- k+ h8 \8 w
#include<iostream> usingnamespace std; int main()3 `% G( O, n2 Q2 a( p
{8 ^6 R5 p& C: t2 f
map <int, int> m1, m2, m3;
% F& C# f+ @: h$ hmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
2 ?7 k' M. i5 ~* O4 a% rm1.insert ( pair <int,int> ( 2, 20 ) );1 r4 x0 C2 M$ L5 r& t( t) S7 s
m1.insert ( pair <int,int> ( 3, 30 ) );: [* s9 Z7 T4 a' |$ q+ W W! |, n; h4 r
m2.insert ( pair <int,int> ( 10, 100 ) );
$ H2 G+ ~+ r2 U- Cm2.insert ( pair <int,int> ( 20, 200 ) );
6 i6 }& U) T5 E9 L9 n3 \& Cm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
! g+ e1 f6 H) Q! x4 Mfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )0 g7 F- ?5 R+ |% C6 A" t9 @
cout << " "<<m1_Iter->second;3 e1 m4 @1 [9 k$ @7 ]. O. z$ V
cout << "."<< endl; // This isthe member function version of swap0 l" a6 k9 \9 ?& b& o' }5 k" _ |
// m2 is said to be theargument map; m1 the target map2 b# G. F( W; F) J/ W7 D
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
. r) c _ D$ V. x; B6 _6 \for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )( |! K3 O) B* [- \! ~4 }
cout << " "<< m1_Iter ->second;
# L9 W. x6 f& P: M0 d% E# {cout << "."<< endl;
8 b/ \5 L+ S* ?+ Lcout << "After swapping with m2, mapm2 is:";
( G) j# B9 E4 f* _* Jfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
7 @# ?* w% {, k; k! D' ~; s) Z& ?8 ?cout << " "<< m1_Iter ->second;) G2 P3 I% w! @. V' f) c
cout << "."<< endl;
9 d) ]" o0 {. r; D# X// This is the specialized template version of swap
: K8 A" v. f) \- [# t! Kswap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";" R9 l1 J) ?9 p
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
( s3 C8 B. k$ F/ G4 Ycout << " "<< m1_Iter ->second;
8 Z- D; ~: Q5 h" m$ [cout << "."<< endl;
9 }9 ?8 J2 |& T2 w) {} 6. map的sort问题:
9 g* u: [) U" R& o$ l+ w% jMap中的元素是自动按key升序排序,所以不能对map用sort函数:
1 @, K+ b( d% J2 S$ y n: EFor example:! ^/ R- D- r/ w0 q- u- J+ y5 d
#include<map>) Q, t- B: K) K5 |/ ~6 }
#include<iostream> usingnamespace std; int main( )
2 b. A" ~5 k2 ]3 _{
( J/ }7 |: y" G$ Q/ qmap<int, int> m1;2 @) ?) w: o) d1 ~- F
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );6 H. G9 E/ [4 m2 o$ j1 }
m1.insert ( pair<int, int> ( 4, 40) );
( o4 Z9 [0 w+ n4 e& a& }m1.insert ( pair<int, int> ( 3, 60) );
7 j7 {! T( G1 @9 j: Rm1.insert ( pair<int, int> ( 2, 50) );
: f( n8 }8 X6 G) }m1.insert ( pair<int, int> ( 6, 40) );
9 G+ [3 p% \; N& S) a }3 fm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;# k, z; F N" w" i9 ]0 m
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
D, ~8 p0 o9 tcout << m1_Iter->first<<""<<m1_Iter->second<<endl;
( X T4 Z, F' C$ T1 Z& D9 k+ l) A1 D4 o6 l
}
9 u, u9 M! G& }" d# N( ~ The original map m1 is:
# _( K7 c- ?/ {5 Q9 p1 203 F; K0 I: f t" W
2 50
; m+ Y' m0 {. t2 K3 60' R3 ^) C3 N% I2 K; G3 @! R4 b
4 40$ c, F# Y6 f7 j, L7 _' W) j: L
6 40
) R7 {; `. B, m! J7 30 7. map的基本操作函数:
[* f6 q/ j: W# M: A6 \) d1 XC++Maps 是一种关联式容器,包含“关键字/值”对
+ K* z) l# c% fbegin() 返回指向map头部的迭代器' P9 a; K. N7 d- H2 F9 e, Z
clear() 删除所有元素
: y) V5 l0 Q' T! C! z; T; ^4 X+ \8 Ycount() 返回指定元素出现的次数( R9 z$ a" X: J
empty() 如果map为空则返回true# {7 a; V+ M( T4 u
end() 返回指向map末尾的迭代器
1 h2 v# G" Z7 {9 @equal_range() 返回特殊条目的迭代器对2 I9 z/ t$ Z. W( ^: N! V7 [' U
erase() 删除一个元素
b, [) T+ S4 J) i% h7 L" hfind() 查找一个元素' q( A% E' T% H! g, b- i
get_allocator() 返回map的配置器6 {8 r% v* ?6 Z$ O0 S* [& _
insert() 插入元素, _1 ?$ S" Z% ^, {
key_comp() 返回比较元素key的函数% d0 O2 q0 A; H
lower_bound() 返回键值>=给定元素的第一个位置
1 U7 g5 c; V+ t. Qmax_size() 返回可以容纳的最大元素个数0 N W2 l" E' b4 L! n
rbegin() 返回一个指向map尾部的逆向迭代器
' Y' a( x2 b. W& ~) Jrend() 返回一个指向map头部的逆向迭代器, p% W( I; x$ ?8 t& i8 G
size() 返回map中元素的个数$ F! p3 I* @' |8 y3 t" W
swap() 交换两个map8 h& T6 I( w( X
upper_bound() 返回键值>给定元素的第一个位置
* I- K1 y! m8 A( z3 |0 C" x/ lvalue_comp() 返回比较元素value的函数
/ }4 g" o8 _. Y" \4 `" K |