请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
% q/ w2 W$ } l; A, ^ b) v* e( zmap<string ,int>mapstring; map<int,string >mapint;
; X- r; N7 E" K" T" ^5 d7 p( rmap<sring,char>mapstring; map< char ,string>mapchar;! A0 ^6 f* H2 r4 O3 ~
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
, R* t' E1 v' W/ w g6 D, I1. maplive.insert(pair<int,string>(102,"aclive"));/ E- C+ f; d7 o& R
2. maplive.insert(map<int,string>::value_type(321,"hai"));1 k6 K; C" Q( f& C8 G/ G
3. maplive[112]="April";//map中最简单最常用的插入添加!
( p' z/ E! b1 s3 z5 P 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
Z: [5 g: n0 p# M2 kl_it=maplive.find(112);//返回的是一个指针
0 Q$ Q& _9 `1 k9 C+ Zif(l_it==maplive.end())
$ Y& p! j4 E* r5 J* vcout<<"we do not find112"<<endl;
# T8 s+ K" w; r! telsecout<<"wo find112"<<endl; I0 W, k8 W) n8 N" E
7 B( f" _0 |" F" j# _: e+ \4 Xmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
: u# j! C1 p% X7 V 4. map中元素的删除:0 h- H! i9 v( Z+ Z* L# g. Z* i
如果删除112;
2 h+ O& {* }8 _6 y0 i8 s' ~map<int ,string>::iterator l_it;;; ]- E2 `) v# a; P% v$ J" @
l_it =maplive.find(112);/ W7 N/ N2 I& L2 a e) ~* a% D9 D
if( l_it == maplive.end())
, R/ ]- X0 }+ Ycout<<"we do not find112"<<endl;4 v% ]% j2 J5 I$ F! S2 I
else maplive.erase(l_it);//delete 112;% ~) z+ l; }: H- r/ `" }
5. map中 swap的用法:
8 m- w* l+ l8 s) E+ A o* K# R% DMap中的swap不是一个容器中的元素交换,而是两个容器交换;8 z; Y( `7 N/ N. i, j' r1 D
For example:
/ h% F( B& K* f/ v `$ J2 b#include<map>
: z; ^* [: L# L8 e; J5 _#include<iostream> usingnamespace std; int main()( w- F9 T2 }# A, [( P, ^
{: Z: u1 f) J) E- k) M
map <int, int> m1, m2, m3;5 n6 N8 E# _! k
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );0 U. m W# y+ D
m1.insert ( pair <int,int> ( 2, 20 ) );
4 r& S8 Q4 N, N8 V" e r; wm1.insert ( pair <int,int> ( 3, 30 ) );. p% x7 r& E( U9 x
m2.insert ( pair <int,int> ( 10, 100 ) );; y0 P; X% ~, n. f0 v# q
m2.insert ( pair <int,int> ( 20, 200 ) );- \: _+ p) u, u; F
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
- B( B8 Z# ^- O4 L5 ^* b! Zfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
+ X- ]9 { I3 U3 w& V0 vcout << " "<<m1_Iter->second;
0 M- V, Q) B, a3 k7 fcout << "."<< endl; // This isthe member function version of swap& @' g& I, X" S# n; E7 L! J
// m2 is said to be theargument map; m1 the target map
0 o+ T( Q# x' z$ K. r- Am1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
3 t6 L7 h3 N6 T3 p7 q0 }* k; l4 K5 j% Wfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )& n8 T5 ]9 [* m
cout << " "<< m1_Iter ->second;
+ n+ \7 [- e B; lcout << "."<< endl; & n5 P$ D/ T3 T# R& N
cout << "After swapping with m2, mapm2 is:";) Z- i- q! Q( ^" z1 j8 ]) w
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
) g$ O1 ~ h7 @6 Gcout << " "<< m1_Iter ->second;
Q7 k8 p# L o1 ^0 s4 e- ?cout << "."<< endl; $ \& \! b* B, t" Q
// This is the specialized template version of swap
6 A) s' Y2 W) q5 rswap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";7 C1 w- U* N9 y: j2 R$ n8 y
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )9 d# Z& m+ k3 h! c( {
cout << " "<< m1_Iter ->second;7 g4 A% d4 @3 E: S
cout << "."<< endl;. q6 O$ L( `4 C8 \9 Q4 g" ]
} 6. map的sort问题:
8 w: x: o- u% Q( `$ y5 C2 B2 ^Map中的元素是自动按key升序排序,所以不能对map用sort函数:
( S5 j$ {5 |$ O) gFor example:
* Z1 B8 u. r; W6 ?2 Y- N% F#include<map>
9 k+ _6 r$ u; z7 Z9 L0 `9 g2 S, G#include<iostream> usingnamespace std; int main( )
+ D3 [# _' K0 {2 Z6 y* i" R% u{) Z5 F/ D0 u3 q* X
map<int, int> m1;
+ |* K' U/ C$ W/ e6 P0 n8 Dmap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );, c% e" w$ A0 l2 q2 L
m1.insert ( pair<int, int> ( 4, 40) );
$ K7 b% b' i% U( {+ nm1.insert ( pair<int, int> ( 3, 60) );
6 W' i& x [ \' ^; V4 [6 im1.insert ( pair<int, int> ( 2, 50) );% b; Z, Z/ R9 L8 M# l
m1.insert ( pair<int, int> ( 6, 40) );
$ |2 S' z, u' }* } r1 K2 }6 L# Jm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
+ }- B: ^, p6 {2 Gfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
" C8 ~, N# f$ C/ }cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
2 J' X- j9 q/ ~6 k8 d- u% q& `+ R9 b9 q4 \
}
! Q* t9 n! ^6 V9 g The original map m1 is:
* z1 r; X) S9 {; I/ P, {) d- O1 20% r5 C+ ]2 D; `! ~ B1 a( C6 P
2 50
: \2 D' b: H9 x0 ]% Z8 \3 601 B6 A( x; J+ d4 E0 U% y% B) m
4 40( Z( N. H P* s& b% Y2 U
6 40
7 T# B% y/ P( E' O' r4 c/ ^9 O7 30 7. map的基本操作函数:0 C% b" Y/ d* x0 t. X4 U3 W* M
C++Maps 是一种关联式容器,包含“关键字/值”对
3 O$ K9 C5 i. Cbegin() 返回指向map头部的迭代器
: y) K1 `! @/ [6 [clear() 删除所有元素1 |/ p8 B) H i1 @& X9 D1 l5 b2 U
count() 返回指定元素出现的次数2 C+ K2 |1 }7 ~. s! \9 {
empty() 如果map为空则返回true
! e, J$ l' Y, D& Eend() 返回指向map末尾的迭代器& `+ A& d% @2 I8 k% I
equal_range() 返回特殊条目的迭代器对6 T4 r1 K3 f9 Z, q b
erase() 删除一个元素
7 m4 _$ O- I; I% L( `5 vfind() 查找一个元素! _1 F' K7 k7 N% C
get_allocator() 返回map的配置器
N+ }! ]# \: h1 `; M1 Z5 |insert() 插入元素" x' e5 i+ L# ?' d0 o1 f
key_comp() 返回比较元素key的函数
~) W4 ~: b8 e# Mlower_bound() 返回键值>=给定元素的第一个位置
( O' n. z9 d3 b0 x$ k- Vmax_size() 返回可以容纳的最大元素个数- `1 Z: Z( u' \8 i# P9 }$ l
rbegin() 返回一个指向map尾部的逆向迭代器
/ S* P: G/ r2 wrend() 返回一个指向map头部的逆向迭代器
1 T& R3 d0 _0 Q H& Lsize() 返回map中元素的个数0 F) |3 Y: d+ N( {" R. k* {/ J4 a
swap() 交换两个map2 L+ p$ J6 o# q1 Q
upper_bound() 返回键值>给定元素的第一个位置" W3 ]+ @+ L/ w; v0 T% L2 O0 S7 Y
value_comp() 返回比较元素value的函数 5 t5 ]4 c5 R% [$ D2 O
|