请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;! X$ c& r8 ^% V6 `
map<string ,int>mapstring; map<int,string >mapint;
) j |4 ]* a8 n$ P" b+ omap<sring,char>mapstring; map< char ,string>mapchar;
- P; k& L/ c* t( emap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;+ ?3 v9 c! @- z! @# R
1. maplive.insert(pair<int,string>(102,"aclive"));
9 P2 v7 O. x7 ?2. maplive.insert(map<int,string>::value_type(321,"hai"));
' v; ~$ \9 L3 t3 e+ f6 ~+ `8 Y9 _3. maplive[112]="April";//map中最简单最常用的插入添加!6 f; M- Y9 H; ~+ T+ e" c( C
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; 2 I# R5 `" z0 ?) t( `9 q
l_it=maplive.find(112);//返回的是一个指针
0 b; e9 f3 h: ?5 E1 J6 mif(l_it==maplive.end())
- W! U" B; B" G0 T! X/ c/ q" c! ncout<<"we do not find112"<<endl;
' Q, J8 ~* n; [5 ~' Relsecout<<"wo find112"<<endl;
0 O0 U4 @7 A' l. }3 A. r 7 D- u$ P3 `( T$ [" H
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;. X/ w' O7 p7 h" g0 f8 \4 z; V
4. map中元素的删除:: } u9 }: S( n e6 {0 Z
如果删除112;$ ? \/ o8 R; v0 C
map<int ,string>::iterator l_it;;' G4 U0 J' ]3 D
l_it =maplive.find(112);! N% ~4 x( V8 U( u6 V( ^0 ~
if( l_it == maplive.end())7 C# g( }: f+ Y( H2 P% [+ [
cout<<"we do not find112"<<endl;
. C. }0 q/ I5 K) F+ belse maplive.erase(l_it);//delete 112;2 n2 J5 J. ]6 r# e7 P+ p
5. map中 swap的用法:; S% @$ `2 W9 K
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
% n/ J8 o# A& N/ `; R4 HFor example:
( e2 J7 t) ^4 O+ B; Y- C#include<map>- I& B2 I4 }2 ^# x
#include<iostream> usingnamespace std; int main()
" `( v [1 Z. o" {$ L/ ?6 s/ E* ~8 g{
& M+ u! o" z9 w* R6 O3 H- @, {: zmap <int, int> m1, m2, m3;
$ z% V: D" l/ W; \map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
7 y% C. e3 Z4 `1 Cm1.insert ( pair <int,int> ( 2, 20 ) );
. ^! o. T2 e: |& f! i' M5 }m1.insert ( pair <int,int> ( 3, 30 ) );
. `& r" f# B/ h6 c+ ~: E# k" Cm2.insert ( pair <int,int> ( 10, 100 ) );
" {+ W( c# j9 }/ }; b4 v* Vm2.insert ( pair <int,int> ( 20, 200 ) );
3 k7 `! j# z) j( `' q3 e0 Gm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";. |) W n' e1 w, h1 m$ K
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )4 N" ]9 F7 A% V& Z# U
cout << " "<<m1_Iter->second;
5 J& }- {8 p, s* `+ R4 v1 S& d' Rcout << "."<< endl; // This isthe member function version of swap
: Z1 Y7 S2 w E" w8 G// m2 is said to be theargument map; m1 the target map/ B$ j2 b" C% a
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
) y" _# e) I+ ~for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )4 n6 e1 a- Y) ]! i
cout << " "<< m1_Iter ->second;
: }6 s. [$ I1 ocout << "."<< endl;
; [# p' x) a$ o5 ]! T. M3 X1 u6 [cout << "After swapping with m2, mapm2 is:";
& g/ A1 L- y" ?7 j& X( Ifor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
8 f7 ]) S( }8 l1 hcout << " "<< m1_Iter ->second;
0 E6 f7 p! W& ecout << "."<< endl;
& S5 B* \$ v' B
// This is the specialized template version of swap
- U! J( Q3 k' D) n1 K+ u9 \2 Q ^swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
2 Y) ]2 u' W) S* t0 l" yfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ ): j, N7 W" g( p; e, d% \" l9 W
cout << " "<< m1_Iter ->second;
$ C$ |! s q* z5 [ O4 \+ _cout << "."<< endl;7 a8 o5 A: ^1 R6 h8 c+ G; B& v6 |
} 6. map的sort问题:3 _2 g# x3 Q- R. ^
Map中的元素是自动按key升序排序,所以不能对map用sort函数:- r( q: Z) O; t. B4 E
For example:& ~% f" P, T! R
#include<map>
+ {# p6 l/ X, [, K6 \, V#include<iostream> usingnamespace std; int main( )- G# \; _, q4 j5 P' P6 t
{7 q7 T6 J+ |3 x4 j
map<int, int> m1;! l' d. S( Y5 O( C
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );7 p* R$ K [$ s' X7 b! W- ~
m1.insert ( pair<int, int> ( 4, 40) );- u/ j: v4 o+ U- L5 z% K
m1.insert ( pair<int, int> ( 3, 60) );4 s @( [ d2 g8 t S0 b( [
m1.insert ( pair<int, int> ( 2, 50) );
0 ` }. Z. Y3 ]4 n5 E- Sm1.insert ( pair<int, int> ( 6, 40) );
3 ?8 m+ P( |( f& Y: vm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;$ D3 @2 W$ H6 Y Z3 @
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )1 g9 B% K- O4 L* T) _3 `& Z( g8 b* O
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;6 o' H: {6 {, A: k' P
- r( \! v0 c9 Z, i* A1 {9 [}5 P0 w9 F, ^) H# i5 T1 @8 |- Z
The original map m1 is:
# {5 ]# f! i) r2 I5 z) Q1 20
# \6 R, F2 o, D2 502 N n% q: T, w+ c7 S0 b7 W
3 60" S$ y' R. s$ r/ M
4 402 d; U9 E$ V2 Q9 S b
6 404 Y2 l Z0 c2 T9 e" I
7 30 7. map的基本操作函数:
/ R% f, A+ U# _# I$ N! Y5 NC++Maps 是一种关联式容器,包含“关键字/值”对3 w( U% E- L9 e" I
begin() 返回指向map头部的迭代器
" K+ A1 \- n3 S7 h, P2 u' Bclear() 删除所有元素" w0 O! J& B* S( `2 a) K9 Y8 Z
count() 返回指定元素出现的次数
8 c& t* u0 K8 gempty() 如果map为空则返回true0 K( C1 x% R! j
end() 返回指向map末尾的迭代器
- Y: U p. u- D$ o, b& Qequal_range() 返回特殊条目的迭代器对0 v, C8 S$ _& i- y& @
erase() 删除一个元素5 G" ]; {' e8 l6 o# D7 m5 q1 a- T
find() 查找一个元素( |- Q! {. S! C4 |3 @5 [
get_allocator() 返回map的配置器
! E% G4 j. i) m) R/ Finsert() 插入元素! ], n4 I2 F! o9 O% P
key_comp() 返回比较元素key的函数
; c$ b6 t5 B, F+ F$ Q! |9 blower_bound() 返回键值>=给定元素的第一个位置
3 @0 l( I# T0 n1 ~; p! ?max_size() 返回可以容纳的最大元素个数" ~. \# K9 g, o: Q( G5 @5 A
rbegin() 返回一个指向map尾部的逆向迭代器
& u3 b. O$ Q: u+ a. N8 @rend() 返回一个指向map头部的逆向迭代器- m) t4 _" {0 ~0 H+ y4 k
size() 返回map中元素的个数
* E2 V8 I' H5 [$ _' h+ Mswap() 交换两个map
. q: ~3 W+ h+ y l2 F2 zupper_bound() 返回键值>给定元素的第一个位置
5 |' |6 G/ |5 [! ^+ Rvalue_comp() 返回比较元素value的函数
7 y' ?; f, N+ H |