请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
) o5 U- a2 G4 K+ ~1 Mmap<string ,int>mapstring; map<int,string >mapint;, m6 W+ g7 W9 V9 U/ N, K; U7 y& X
map<sring,char>mapstring; map< char ,string>mapchar;
. G3 O* S. P: d4 p( bmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;. m+ L$ r& c/ g+ ?) |) N% j/ u5 C
1. maplive.insert(pair<int,string>(102,"aclive"));8 ~$ q; e# d# e" @# M& D
2. maplive.insert(map<int,string>::value_type(321,"hai"));" S; H5 ?. S4 A7 v4 t8 B
3. maplive[112]="April";//map中最简单最常用的插入添加!
$ [/ j- g2 |( G2 N6 R 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; M2 a% p5 R$ N0 j! a" Q
l_it=maplive.find(112);//返回的是一个指针/ m$ L6 b7 H; ^4 q" `# Z# g
if(l_it==maplive.end())1 Z! q9 Z M7 y+ t" j1 D
cout<<"we do not find112"<<endl;
2 R$ Z5 s! |6 \7 I% A" l) W: A p8 @elsecout<<"wo find112"<<endl;! ]" T. p! Y7 H9 b' a9 R6 n q
4 ^; P+ B& n& P2 u" U& F4 B
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;1 T' N# U o, y+ W3 V1 M
4. map中元素的删除:
- j6 O! u& g7 g0 p3 T l如果删除112;+ U) f0 X! c, E! ^# g c
map<int ,string>::iterator l_it;;' |& l! C2 z, |+ |3 T/ Z+ p
l_it =maplive.find(112);6 o8 ^: q- w$ M
if( l_it == maplive.end()) _: `1 t$ R3 S1 B& _6 a
cout<<"we do not find112"<<endl;; ^. y X$ r8 `0 }* G
else maplive.erase(l_it);//delete 112;! K( i0 K, l" B" m% X1 y# S, }2 v
5. map中 swap的用法:( d" G8 a) G ^0 {* t6 y. C" z7 u
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
, S( T: J- f8 R" [For example:! q: e2 Z3 W" ?' [& D
#include<map>; x: U. V+ N9 H6 `0 }: f
#include<iostream> usingnamespace std; int main()6 _: U3 j) a' A* @, @& L2 y
{! W0 P# }, ]: M: c( N' u! `
map <int, int> m1, m2, m3;0 h- |' W. u6 a: h7 O. {! A) L
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );; Q' j4 h/ z S$ ~! ]0 M) v/ \
m1.insert ( pair <int,int> ( 2, 20 ) );. t2 f3 W% v/ w. H3 }
m1.insert ( pair <int,int> ( 3, 30 ) );
; U9 O3 g, Z! c; L: C( \* u4 {m2.insert ( pair <int,int> ( 10, 100 ) );
" j2 o! m- p) Um2.insert ( pair <int,int> ( 20, 200 ) );
/ [3 v% {0 V g* `m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
- H/ d: j$ v! G: H* w; ifor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
, o/ p. l$ n7 [4 E9 Z( N4 mcout << " "<<m1_Iter->second;
0 \: h' K7 H# ccout << "."<< endl; // This isthe member function version of swap$ b* l2 Y5 [/ F
// m2 is said to be theargument map; m1 the target map
7 n9 P: D& n# F% G$ Qm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";3 D+ o" ]- `0 g8 E+ v% `
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
% \. y1 p0 d& J! Q b- E2 } tcout << " "<< m1_Iter ->second;0 l+ ^9 Y! h' F: `
cout << "."<< endl;
9 V7 I1 Y0 S; I% l6 M% ^, @& ~cout << "After swapping with m2, mapm2 is:";
( ?, \- ?' z2 }2 C% y( Lfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )) \. w- D# M2 Y) A' o' E: n* U
cout << " "<< m1_Iter ->second;
( V2 m3 @8 Z) E- J% T7 tcout << "."<< endl;
, v; o* A- e$ I# E// This is the specialized template version of swap
$ V2 x, O+ M# G- _* U% ?9 hswap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";
+ y# g& N2 ~, P" v, X$ ?for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
2 j6 c: z/ x6 A2 j# k: ncout << " "<< m1_Iter ->second;
: M( `4 \( b/ E; [cout << "."<< endl; q; W; W. w7 L' H$ D1 e
} 6. map的sort问题:
3 n' J/ v% j' V w/ C: GMap中的元素是自动按key升序排序,所以不能对map用sort函数:" Z7 e: i. z; Z& a
For example:
9 l8 {* R% f* w/ T# S+ w. Z4 D5 W& b#include<map>
m+ l/ P% J4 T" m8 n#include<iostream> usingnamespace std; int main( )
[: y3 g2 R0 Y{
% ?9 c5 Z/ S1 H' a4 ~* r5 c. |map<int, int> m1;8 q( `% ~6 Z7 f/ y5 } s
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
. {6 y' J7 _) }- w: Z7 R. Vm1.insert ( pair<int, int> ( 4, 40) );
$ I* E9 h. h" l" J9 n: im1.insert ( pair<int, int> ( 3, 60) );1 e, D Q [+ K6 x5 k4 Z4 g; i
m1.insert ( pair<int, int> ( 2, 50) );
6 r" O8 ^5 b0 [3 M( p! S, n$ q/ sm1.insert ( pair<int, int> ( 6, 40) );
: p# ^% H, B1 o% ym1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;6 F3 {/ y. A' G
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )9 {: x% p( G9 }, B! N* s
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
$ Q, k& W4 E5 V9 Z# c, ] p0 O% f! S* q6 M7 B0 A
}' Y( R H+ Q6 ?. A
The original map m1 is: H! L2 n3 J* @
1 20
' N- x/ e0 w' _, q9 r- z2 505 p e" X2 w0 I8 X& b
3 60
3 n4 t4 ]; y! S2 S7 Z4 40: Q( _5 ^5 m& c( q$ Q# y
6 40
! _; b6 H" B* q0 y7 30 7. map的基本操作函数:( Y7 W# ?' d- s
C++Maps 是一种关联式容器,包含“关键字/值”对
5 B' a) {3 p( V5 k2 Jbegin() 返回指向map头部的迭代器
2 x2 x3 C# A8 D; \" U4 K4 o1 eclear() 删除所有元素
6 Q+ m5 |& _2 M. g: K5 h0 ucount() 返回指定元素出现的次数
7 {% H6 @3 D7 Eempty() 如果map为空则返回true
, n8 U& F9 R, {% t7 L, Dend() 返回指向map末尾的迭代器7 k/ ?. j3 y8 \1 L3 S5 c; l2 Z. s
equal_range() 返回特殊条目的迭代器对; x- y) E. C `& R& U
erase() 删除一个元素
4 L% s! d+ ?( |6 Vfind() 查找一个元素
6 r. K& o' W' u9 Zget_allocator() 返回map的配置器
6 R' Z9 F0 ]% ]# \8 G& Kinsert() 插入元素
: `7 u! s) [. `# Vkey_comp() 返回比较元素key的函数+ _: ?0 B' L! N! T
lower_bound() 返回键值>=给定元素的第一个位置
( u) Y, L9 U7 _5 S& o% ymax_size() 返回可以容纳的最大元素个数
! |- D+ ?/ w' i- v0 Irbegin() 返回一个指向map尾部的逆向迭代器
$ v8 V7 K# I1 H# s+ s+ F$ p0 prend() 返回一个指向map头部的逆向迭代器2 m [2 t* `2 ^5 S# ]& N
size() 返回map中元素的个数
! N9 Y& i9 x# s$ E& ]swap() 交换两个map, U$ }: F' J) |5 c
upper_bound() 返回键值>给定元素的第一个位置1 G D& R2 \7 o& ^( U
value_comp() 返回比较元素value的函数
: x% i3 g5 {7 R) s# z2 D8 d3 k |