请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
4 w% n0 i. B( a& `% R7 wmap<string ,int>mapstring; map<int,string >mapint;
7 N' a, _; M# D/ Q1 X& {- emap<sring,char>mapstring; map< char ,string>mapchar;7 f- O+ R, K2 W* L- G- N- ~
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
7 S- p! _, ?( K0 A- G* M8 r1. maplive.insert(pair<int,string>(102,"aclive"));3 h* e+ F1 H' _. n6 ~
2. maplive.insert(map<int,string>::value_type(321,"hai"));
: g% E0 n9 n/ g+ L( m3. maplive[112]="April";//map中最简单最常用的插入添加!
: E8 H* L) A7 `# u: {3 j 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; , F1 B) @; G% l( \, {& j) r
l_it=maplive.find(112);//返回的是一个指针% X5 Z" i& i- @ r( r2 B; Y/ ~
if(l_it==maplive.end())5 y$ d) i- G* I
cout<<"we do not find112"<<endl;
" ` V+ e5 P) s1 \: B* R% Welsecout<<"wo find112"<<endl;
* T/ d- O J, i! t
7 \! `' [! {& [; z) K) _- w4 H" F( Pmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;0 M$ l& y4 e. x2 ?8 Z; K
4. map中元素的删除:& D% b# C I9 k+ a' |% [, Z
如果删除112;, ]9 K7 t5 w8 V8 {/ k
map<int ,string>::iterator l_it;;
7 e7 H3 t# N: f7 cl_it =maplive.find(112);* X1 M. L$ @/ h5 o1 P
if( l_it == maplive.end())' U }* h/ B: d- \
cout<<"we do not find112"<<endl;0 y8 W# _4 d K8 k% t
else maplive.erase(l_it);//delete 112;
- G* J, c. K( C) r 5. map中 swap的用法:0 E, D+ w0 I' z5 X0 o
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
2 F6 F7 K% B/ |& VFor example:- F3 U# ]! @4 X( }. d' _. j( x9 W
#include<map>
! S. \: u2 v6 ^$ g' p9 u0 q#include<iostream> usingnamespace std; int main()/ U! @4 i. i' w# ~( D! V1 m
{6 O7 _3 T) M, x: l
map <int, int> m1, m2, m3;
8 Z' ~2 V( q+ [1 g! kmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );; \: _' V* B1 F0 q
m1.insert ( pair <int,int> ( 2, 20 ) );; y" C# Y# n" T3 L3 [8 `& [
m1.insert ( pair <int,int> ( 3, 30 ) );% h$ ^/ T, }2 w0 T! P) m% y6 ]
m2.insert ( pair <int,int> ( 10, 100 ) );- v4 h1 T8 e9 ^! o) {
m2.insert ( pair <int,int> ( 20, 200 ) );" d4 l* k$ I! _1 G6 K. q
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
6 u$ Z# Y3 Z: X+ g+ Ifor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
0 O: U" H! Z. O+ `cout << " "<<m1_Iter->second;6 |' B7 X/ i% V, I
cout << "."<< endl; // This isthe member function version of swap! d2 V0 m7 K$ y* Q/ u. f! i4 b
// m2 is said to be theargument map; m1 the target map% n# a; Z3 N; s: m$ ^
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
$ w- U! F1 p6 b7 P2 U4 h! Ofor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )" N$ a4 r4 Q. [6 W
cout << " "<< m1_Iter ->second;
; Z; T! a, G- f* w9 x: wcout << "."<< endl;
; o2 \+ A+ I5 s( Hcout << "After swapping with m2, mapm2 is:";- ~5 Z0 l( s4 G3 t. Y4 ?+ H. j; g! O
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )6 F5 B0 H. ~, u; k
cout << " "<< m1_Iter ->second;
+ U1 c* U$ E7 g9 K0 H3 xcout << "."<< endl;
% F0 | s3 Y# H2 A* H2 t+ d* U; a7 Y
// This is the specialized template version of swap5 K* n0 n; ^9 e+ M4 s% d. y
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
% d- N, ^$ [. u* o$ g& u& \for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
8 X* n* |% i' u& acout << " "<< m1_Iter ->second;+ f9 e9 _2 D; B: Z
cout << "."<< endl;
/ f/ B: R' U2 k# I/ e2 s, y} 6. map的sort问题:+ c- g! y! p( ]0 M5 ?% \8 c Q
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
* n( J) a, c! tFor example:9 U# _3 R& t6 Q) n
#include<map>
9 L" W2 l s- s' J1 h#include<iostream> usingnamespace std; int main( )
" Y& F2 z+ X- r{! Z8 s9 M" s6 }$ l O$ p |5 D5 {
map<int, int> m1;
; D" Q) v4 e; m: Amap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
6 F8 D$ G3 p5 d6 b# b0 q1 T/ Km1.insert ( pair<int, int> ( 4, 40) );
" x& l; Z# G/ _8 \3 p3 [5 }m1.insert ( pair<int, int> ( 3, 60) );
* u7 V- H1 r- Y% _m1.insert ( pair<int, int> ( 2, 50) );/ b3 r* g. X5 M4 f
m1.insert ( pair<int, int> ( 6, 40) );
2 F2 F7 ]% P& C: N8 im1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
+ h4 E; F) R; L5 q/ y5 \4 s( E% D, efor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
8 `/ y, |) e- K1 U" jcout << m1_Iter->first<<""<<m1_Iter->second<<endl;, M1 t; v' E" n0 l/ \- ~0 R
8 c' l3 o+ d- l8 r
}
& B+ T0 T: O3 a* v! z3 s& c$ ^ The original map m1 is:
, s4 }, d: d% |) [1 209 x- t% C5 |1 w( v$ N
2 50
4 W0 O: j( i/ s6 c2 t/ d% [" H3 60
# `2 S) n" b4 C- K+ J1 B4 w7 N4 40
3 K/ ?: d! M( n3 k& `) v/ i9 I. l6 408 h- U; ?9 W. d9 U( U6 G$ X6 W
7 30 7. map的基本操作函数:2 c: H f+ X& p, @+ x' u
C++Maps 是一种关联式容器,包含“关键字/值”对
$ H5 P0 n7 [# G$ vbegin() 返回指向map头部的迭代器
' O$ O" B8 @: k6 t9 l8 W& W6 Xclear() 删除所有元素+ L( Q6 z+ q5 c y4 v, |
count() 返回指定元素出现的次数! L: [" S% ^2 j/ T6 k" C) q
empty() 如果map为空则返回true o+ ~' v* w) E
end() 返回指向map末尾的迭代器
% T3 t4 [: J# Xequal_range() 返回特殊条目的迭代器对
# c0 j6 O' R5 J" o( D; V) ?erase() 删除一个元素2 }, ~8 }( y) ]1 E: w
find() 查找一个元素
- h5 N# Y2 n3 W/ M9 Vget_allocator() 返回map的配置器) ^5 c8 Q: k1 S5 T8 m1 `% d
insert() 插入元素. h w2 A$ g1 C
key_comp() 返回比较元素key的函数: h S, I. n- u2 h9 X" `
lower_bound() 返回键值>=给定元素的第一个位置& j4 _, L0 A `
max_size() 返回可以容纳的最大元素个数
: p8 R! Z# Q/ L3 E6 ]% Lrbegin() 返回一个指向map尾部的逆向迭代器/ e% T0 u/ i' _. Q2 g
rend() 返回一个指向map头部的逆向迭代器1 u) y0 ~; T3 G R7 G9 [5 J
size() 返回map中元素的个数& q- `4 d$ ^( O& ?, p& q: L8 f
swap() 交换两个map! ?1 z/ R* n! z8 Y, l, }
upper_bound() 返回键值>给定元素的第一个位置, s( ~+ y( p6 v3 ~! r
value_comp() 返回比较元素value的函数 * r* z- p1 b; k( K2 e, Q
|