请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;/ t; E+ S$ L. E) {7 Q$ c0 G9 N# j
map<string ,int>mapstring; map<int,string >mapint;
5 l) L( t* X j0 ^6 k" Emap<sring,char>mapstring; map< char ,string>mapchar;
' ]. f) o9 {7 S# Amap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
, q6 C) y2 T( v7 G1. maplive.insert(pair<int,string>(102,"aclive"));
( t" {" d4 L# u2. maplive.insert(map<int,string>::value_type(321,"hai"));
! u, y* q$ D" X+ b: |1 ]3. maplive[112]="April";//map中最简单最常用的插入添加!
" t) j" {5 i- o$ H+ p 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; $ } O2 G/ C, n4 G
l_it=maplive.find(112);//返回的是一个指针
; s$ |6 F( B6 a6 c$ v2 e) sif(l_it==maplive.end())
" `/ o' q1 A/ I; Z3 o; Fcout<<"we do not find112"<<endl;9 g, n2 u2 a0 A/ S* C1 }
elsecout<<"wo find112"<<endl;: j$ a2 {( U7 Q
& E& O7 W0 _: s% L) ^
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
1 P$ n. O- _9 \ N4 z 4. map中元素的删除:! M' ^- l1 ], x0 }2 a8 F3 ?
如果删除112;
# ]4 O; J) k2 q. G" P1 i. umap<int ,string>::iterator l_it;;
m- B4 w. U1 R9 v& R3 s9 t& ml_it =maplive.find(112);
2 Z) F( p$ F/ |1 T) [if( l_it == maplive.end())
# ~9 {6 ~* j. C4 N- b, Z3 n" w3 i! @cout<<"we do not find112"<<endl;
9 u) P" e; W$ {9 ]# e! Oelse maplive.erase(l_it);//delete 112;3 Q6 X4 _" z- l: D N
5. map中 swap的用法:
. M5 s, O# E3 \) R9 pMap中的swap不是一个容器中的元素交换,而是两个容器交换;8 I, X5 A; d6 G' B: N+ g- E
For example:, v% u0 Y+ z2 w3 M' i* h2 Z
#include<map>
, j$ E2 o G6 @1 C: [#include<iostream> usingnamespace std; int main()9 I1 m- ~( ]1 U0 U7 I' m
{! l+ \% i% P! f0 t3 |
map <int, int> m1, m2, m3;/ b) H$ h$ V, @ p
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
% Q9 O/ ~( ?% m, u9 w P; em1.insert ( pair <int,int> ( 2, 20 ) );
$ o+ r, l7 Y2 h6 `$ X0 y( hm1.insert ( pair <int,int> ( 3, 30 ) );
! o3 o7 u$ o" L) ~m2.insert ( pair <int,int> ( 10, 100 ) );
1 O8 a$ `* e! M* \, q" X, Mm2.insert ( pair <int,int> ( 20, 200 ) );
6 Y; U( h0 [$ \m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
$ q7 b) u: X* {: l( F% i( e% l8 J$ xfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )( r& A6 `; L5 r, r
cout << " "<<m1_Iter->second;
" d/ A! l K8 r- Xcout << "."<< endl; // This isthe member function version of swap& a2 d- a- X$ g( [
// m2 is said to be theargument map; m1 the target map
/ e) |* c/ ~# P0 b) r6 Im1.swap( m2); cout << "Afterswapping with m2, map m1 is:";, `: x; R a5 G
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
0 P0 q1 e9 d5 q2 [cout << " "<< m1_Iter ->second;) G; c0 w3 ^: o" s
cout << "."<< endl;
9 \: \1 |6 L' g1 x4 o" Ucout << "After swapping with m2, mapm2 is:";8 ^0 L ?# v f- o8 c
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
7 |* a/ j5 Z5 B8 h. Ccout << " "<< m1_Iter ->second;
1 G( c! U$ A9 Ncout << "."<< endl;
. `. K! i8 ]/ j6 i
// This is the specialized template version of swap
, _3 |, @% p* v" o( }1 d) k" Nswap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
) y/ Y( z: ` W* ^5 G% P* lfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )( a6 |! q6 E/ Y: J) b/ K% x
cout << " "<< m1_Iter ->second;! X- {7 ]% p# f; H# M. K5 g
cout << "."<< endl;7 f# _, O' ~ c2 l6 V' o
} 6. map的sort问题:7 f: ]: m9 n5 K5 k
Map中的元素是自动按key升序排序,所以不能对map用sort函数:9 s/ x8 I; A1 b& G
For example:
/ O# {4 ~+ X& r7 \#include<map>" z4 E6 u$ t. I+ Q% K' g, {
#include<iostream> usingnamespace std; int main( )
- N% M! |0 p' i$ M) X- E{2 g J" c2 ~+ x1 S [, p2 `
map<int, int> m1;+ t" f+ L, u# d3 k- u
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
" j+ _" {# E: `- {8 K9 Y2 y! R+ s2 Dm1.insert ( pair<int, int> ( 4, 40) );& V; Z. j( V2 q& d" K
m1.insert ( pair<int, int> ( 3, 60) );
& U( H3 p" _0 ]) x3 pm1.insert ( pair<int, int> ( 2, 50) );5 I' |6 q, |& E+ O
m1.insert ( pair<int, int> ( 6, 40) );; F8 @) L- [6 X" \. q; e! K
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;8 w7 P; h A+ K
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ ). c' ]" I7 C H/ A
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
, k1 {4 _! T% h% F F3 x' m6 z) s* ^, I) p
}
& p& G% k+ Y" W, H8 v6 _- a6 Q The original map m1 is:8 e' T2 X+ N0 V8 g6 z) h
1 20
* D9 _1 P: T. F% l5 F4 _! ^2 502 Z V2 F7 s. [( V* e4 C8 {
3 60
6 q! d" `0 D: D3 g) e4 405 }0 c R9 v" ?
6 40
2 N o5 \# _0 v, U9 l) d9 _" n7 30 7. map的基本操作函数:
8 a1 N, |( v. F4 h: C& j" hC++Maps 是一种关联式容器,包含“关键字/值”对
6 {& j' ~! w3 M& t, `& Kbegin() 返回指向map头部的迭代器4 Q; Y4 ~. T' C2 [/ `4 `# v& ?% B
clear() 删除所有元素
. R+ c9 k" q3 ecount() 返回指定元素出现的次数
! W4 M* ]! i0 D1 y, Rempty() 如果map为空则返回true% h* q+ h' c' }5 p' j& r
end() 返回指向map末尾的迭代器
2 m P8 [* a7 q. z- g; jequal_range() 返回特殊条目的迭代器对6 Z8 |% W- l! E5 u: t6 y- c s
erase() 删除一个元素1 {/ ~" _) F0 W6 Z' \
find() 查找一个元素5 w1 ]8 f+ S3 Z7 l6 ]) A9 O
get_allocator() 返回map的配置器
0 }8 H S$ Q8 u! @1 linsert() 插入元素
/ N6 @: H, O. R% `) N7 ^key_comp() 返回比较元素key的函数
" K7 y- q$ h7 O7 q, n0 Wlower_bound() 返回键值>=给定元素的第一个位置
x2 M1 X$ ?5 ^. g5 U0 `max_size() 返回可以容纳的最大元素个数: }3 q* H6 i" q( k& Y$ c
rbegin() 返回一个指向map尾部的逆向迭代器* `+ p* N1 v5 C+ |9 _, Y( S
rend() 返回一个指向map头部的逆向迭代器' W8 \7 Q5 k1 a
size() 返回map中元素的个数
# \$ n2 S/ V1 V0 x4 D1 s: f+ Yswap() 交换两个map
( O7 r' h! r. M$ d7 Zupper_bound() 返回键值>给定元素的第一个位置* y1 ~! A" i3 K. t9 t
value_comp() 返回比较元素value的函数
' Q; _9 [& _% G. D; g( N |