请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;/ B7 A+ o) P9 M6 g
map<string ,int>mapstring; map<int,string >mapint;. P% s7 e# J/ m
map<sring,char>mapstring; map< char ,string>mapchar; r( x! f: M. a% g7 x4 a! B c
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;" J) C; j* V" W# T* k
1. maplive.insert(pair<int,string>(102,"aclive"));; `; V% K0 G/ ~$ I) g9 z% Z8 Q
2. maplive.insert(map<int,string>::value_type(321,"hai"));9 r2 f, o, {+ |1 q' Y% o
3. maplive[112]="April";//map中最简单最常用的插入添加!
+ k% `% ~9 ~3 `. U! I# N 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; 5 S6 i. f! W0 |. o+ O' i
l_it=maplive.find(112);//返回的是一个指针
. @" a6 V! M1 l# x; V. bif(l_it==maplive.end()). \) a1 y" y5 N
cout<<"we do not find112"<<endl;+ P- E \- R) i: O5 D% H
elsecout<<"wo find112"<<endl;: _0 A7 t3 |( b7 B; i$ R2 C4 r
" F5 N8 C2 M( A5 U. s
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
6 ~& z1 h6 j+ W1 M 4. map中元素的删除:
" _% z6 _# n+ d' I如果删除112;: y1 [3 y: Z3 ?/ f" j' m
map<int ,string>::iterator l_it;;: ?" ]1 x) v4 s1 U' a: A) N. q
l_it =maplive.find(112);* p8 ~5 ? ?6 g4 K% j
if( l_it == maplive.end())6 ^/ x) o3 s+ I
cout<<"we do not find112"<<endl;
) T5 o0 c+ Y0 r+ P' c7 Y* d9 Celse maplive.erase(l_it);//delete 112;
# p2 z# G8 p: a0 H6 |8 J/ W0 e0 s7 o 5. map中 swap的用法:
' g9 Q1 A3 p' s. U% cMap中的swap不是一个容器中的元素交换,而是两个容器交换;
0 n. d7 @ u2 a2 w) Z* B' AFor example:
/ g* {; w. _$ a#include<map>
5 d4 M" E, Y, e+ F2 L% ?# z#include<iostream> usingnamespace std; int main()
; D6 k0 Y) V9 Z) C. H! D$ {! K! G{
) Z8 p! ?$ `& a! Dmap <int, int> m1, m2, m3;
! a- u& f$ u w2 K Z0 A) Amap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
9 P) p. h) l" b0 em1.insert ( pair <int,int> ( 2, 20 ) );; O; g( p, v1 w; Q
m1.insert ( pair <int,int> ( 3, 30 ) );8 q3 j5 U1 M. O6 Y
m2.insert ( pair <int,int> ( 10, 100 ) );
& e+ D0 p$ G' E( M6 z& @" z0 ]6 ~m2.insert ( pair <int,int> ( 20, 200 ) );
: ?, K: y+ z! g- Z( S% D& nm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";( c7 k) H' m& X$ e! p
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )) v0 W/ ?: D3 p- n" F% @7 {- j3 W7 C) n
cout << " "<<m1_Iter->second;
8 E: K, v& T! Zcout << "."<< endl; // This isthe member function version of swap
! u- M$ c/ G' w2 b# W, E% g0 ]5 h// m2 is said to be theargument map; m1 the target map6 h9 u9 k1 m, g, X% \6 h
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
4 [# |' O* F1 ~5 T+ Afor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )) } v3 y2 L6 @
cout << " "<< m1_Iter ->second;. G9 E+ A, i9 a7 @7 X2 m8 E
cout << "."<< endl; 0 }9 Z/ z, }; }+ k3 d
cout << "After swapping with m2, mapm2 is:";
4 @/ w1 T' u( O+ H1 `# Ofor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )- f# }+ L' E, p! A+ l, _7 i
cout << " "<< m1_Iter ->second; K, l8 v! J& @2 {
cout << "."<< endl;
d b# d. p" _( y1 o/ M, k9 l// This is the specialized template version of swap, o* O; L$ j: w+ a
swap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";, S. ]3 Y: n; R1 P' {0 i) _; F
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )0 |' V" Y: L7 q4 c, B
cout << " "<< m1_Iter ->second;$ m" A( p1 T, Y' r; Y% H0 M0 o
cout << "."<< endl;) e7 r* ^0 G2 i- R& r- F
} 6. map的sort问题:6 @9 p9 N+ Z: ]( q2 g
Map中的元素是自动按key升序排序,所以不能对map用sort函数: d G* ]9 b8 _) z" }; I
For example:
6 G7 k( `0 @1 c9 a' |* x6 N- w" u#include<map>/ v1 |" \# ~. d, L1 H6 W* z
#include<iostream> usingnamespace std; int main( )
3 J$ N1 T( f9 ~{
K: c4 ]6 l8 n8 e9 F7 Hmap<int, int> m1;. F' e+ I7 n3 y) T/ K9 |7 {
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );1 r( U7 k. Z6 H7 Y
m1.insert ( pair<int, int> ( 4, 40) );
; L. B# a( t$ }: B' ~m1.insert ( pair<int, int> ( 3, 60) );
; b7 a" }7 ~, Fm1.insert ( pair<int, int> ( 2, 50) );
& P6 l/ g8 O7 J% O- `" u/ i/ ]m1.insert ( pair<int, int> ( 6, 40) );
1 G# k- T7 Q1 l; S, p& ?% w$ I* zm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;2 b# y& E* x6 ]' C: X% P9 k0 I
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
* S( N5 `* w. r3 ]2 Tcout << m1_Iter->first<<""<<m1_Iter->second<<endl;
1 e r- y3 | P/ Q: H' W8 I) q3 F2 `' _8 k! ]* ^
}* f# v4 [7 |) b- W
The original map m1 is:( g; y! [; `! r
1 20
( S* `& @3 H+ h% [/ y: ?" ^2 50
0 V! h: h1 b9 T( W4 H) o" V3 60- W. i/ r- c9 C) }7 b7 B2 {
4 40
/ s& i) r; C* V, |6 400 D3 N' e: O7 p5 r- B/ n
7 30 7. map的基本操作函数:
) \: z) n' }; W& C" z1 {1 `C++Maps 是一种关联式容器,包含“关键字/值”对; R( ~" Q6 H1 q" _
begin() 返回指向map头部的迭代器6 n- m8 n$ l- v* U H. Z6 D
clear() 删除所有元素1 N* C/ u' W' \: H
count() 返回指定元素出现的次数
4 I' p6 I) S& kempty() 如果map为空则返回true, K8 t0 S5 I! u# Y
end() 返回指向map末尾的迭代器5 Q3 x" w8 p! ]" T0 q
equal_range() 返回特殊条目的迭代器对: H% q. q8 w! ~; N. Q, W1 d
erase() 删除一个元素# ?1 |3 L# D6 M5 C4 i7 l
find() 查找一个元素8 k5 s/ u% u! X, f$ z
get_allocator() 返回map的配置器7 i7 C6 {$ Z4 b4 ~6 Z# \$ g
insert() 插入元素+ V- H8 U O; P1 e. ^% y7 t
key_comp() 返回比较元素key的函数
/ G3 |: Q! Q D `( klower_bound() 返回键值>=给定元素的第一个位置
* V& C( t: {0 u Q( x0 lmax_size() 返回可以容纳的最大元素个数
' u6 G1 {3 j# ~' c/ q& }) N2 Grbegin() 返回一个指向map尾部的逆向迭代器6 h& h9 c) v* A8 ?
rend() 返回一个指向map头部的逆向迭代器! C n4 L$ y0 H: G
size() 返回map中元素的个数
; _$ l" u2 O) C% V7 t2 ^6 `swap() 交换两个map% s; @# q/ w$ u9 k8 J
upper_bound() 返回键值>给定元素的第一个位置
; ?* _# H- { i0 r$ q' Y9 tvalue_comp() 返回比较元素value的函数
a9 T2 g+ L6 ~0 ^" v |