请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
( b1 V7 {/ l# u b1 Hmap<string ,int>mapstring; map<int,string >mapint;
% K& V* }* F4 ]0 v. Z Omap<sring,char>mapstring; map< char ,string>mapchar;
) |5 p1 Q1 Q) E; lmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;, P1 x$ j: c- k& \' p4 m
1. maplive.insert(pair<int,string>(102,"aclive"));
- o _( V7 \ u& M2. maplive.insert(map<int,string>::value_type(321,"hai"));5 M+ j7 H* @; n* L: ^
3. maplive[112]="April";//map中最简单最常用的插入添加!) y3 a; {8 w3 }2 q6 z4 y8 Q
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; + `' _ t; t+ c+ b" Y" g
l_it=maplive.find(112);//返回的是一个指针( [/ S2 s2 q0 D% R' G9 u& P% C/ n" [
if(l_it==maplive.end())
7 s4 Y* {; C! q9 F: c; R; zcout<<"we do not find112"<<endl;; B! Y1 C. p: @; V. _
elsecout<<"wo find112"<<endl;5 b2 g8 W" r9 U
' N \% I- h: T% ^$ Q9 Z8 M
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl; @3 l! s4 x' U" a
4. map中元素的删除:
0 P8 D: A4 ]) ?: Q3 Z8 D5 U7 j如果删除112;
8 [/ R% Z! y# f, ], }. Fmap<int ,string>::iterator l_it;;
% s- Q" U4 g8 X R# b/ Z. rl_it =maplive.find(112);4 ?/ h; Q/ I: r' D% {8 ? j
if( l_it == maplive.end())
' L5 L6 [4 w- O5 ]9 K4 Lcout<<"we do not find112"<<endl;
7 d+ B: Z, H/ }+ Qelse maplive.erase(l_it);//delete 112;
- g" u$ I: n& \! E x! h 5. map中 swap的用法:* V, y& }8 O& H; }' y$ ?
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
+ w* K6 o7 P9 {For example:. E8 l- H4 `8 b6 m6 U1 a
#include<map>
* z) |8 V3 h! H- I#include<iostream> usingnamespace std; int main(), N o: w5 Q; q" I( P
{8 h! j" x5 j$ p
map <int, int> m1, m2, m3;% C: f7 G2 {; r' J
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
& @7 H/ j. H& H4 f9 Am1.insert ( pair <int,int> ( 2, 20 ) );- {: M B2 K2 @2 \
m1.insert ( pair <int,int> ( 3, 30 ) );; i- x0 |0 G K4 f
m2.insert ( pair <int,int> ( 10, 100 ) );2 ^& l8 e7 p" F: z: p
m2.insert ( pair <int,int> ( 20, 200 ) );
1 _% o7 V: z% t. ^0 Tm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";% Z; p# \+ d7 F# y) F; W
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
( o9 a7 l- h/ D6 P0 R5 mcout << " "<<m1_Iter->second;3 k& H, A8 h$ x" z
cout << "."<< endl; // This isthe member function version of swap8 Q. v( b* ]! K2 h7 k! S
// m2 is said to be theargument map; m1 the target map
1 e3 }3 w+ t( I, Dm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";! k+ x& e, \9 h1 t
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
, L+ w9 b& V8 n2 e0 I& |5 lcout << " "<< m1_Iter ->second;" E5 H9 T* [1 ^5 z( {
cout << "."<< endl;
5 W/ ~1 j8 Y% k3 w0 T/ | w4 O9 ncout << "After swapping with m2, mapm2 is:";8 B* n% J* r5 `9 i( Z6 ?. o/ a' \
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
) z V- w+ Y4 [. ?* L6 M( p H; u* gcout << " "<< m1_Iter ->second;
! l, Z N* J: O" ecout << "."<< endl;
) r6 X0 L6 Y' z( W+ b& s+ o1 m// This is the specialized template version of swap
" s: C: |1 L" I Oswap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";
# H. C9 r. o5 ^4 ^for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
: q$ ?* ~7 v" y4 `% G/ Zcout << " "<< m1_Iter ->second;
7 u. z- b8 k7 T0 z4 M, j; Xcout << "."<< endl;
$ ~7 Y1 Q: v+ v, `: V} 6. map的sort问题:
: `5 J; B# v+ d& U) a+ Z2 JMap中的元素是自动按key升序排序,所以不能对map用sort函数:
" M; o; @8 I k( pFor example:/ T; `: u) _3 r! W! C
#include<map>
K, B) D% \4 p4 ^ R2 k# p6 r0 f' W#include<iostream> usingnamespace std; int main( )0 C% {0 Z6 _1 }- B6 H! a/ N
{+ A" I9 s( k, ~
map<int, int> m1;
6 ?/ g8 S6 L# n# Mmap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
1 A& M3 R) C8 Z& T7 q; Dm1.insert ( pair<int, int> ( 4, 40) );7 z% N) \7 I" ]$ ]3 p8 P
m1.insert ( pair<int, int> ( 3, 60) );. d' R7 s) @( Q6 j* @0 h# x
m1.insert ( pair<int, int> ( 2, 50) );5 z( O. J! R# _: `
m1.insert ( pair<int, int> ( 6, 40) );
4 Y3 u9 S0 @+ u- ?9 ~- {7 Vm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;! L8 P0 j0 T4 `# | x
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )+ n4 y5 K8 k& r7 ^
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
m8 x: A4 c o! w: N' i
$ x* F1 u4 L% ~, s5 z}
4 w* m( H4 r# C" c- H6 I9 R$ ? The original map m1 is:
7 Z* p' c9 M5 A* c- P6 }1 20
' Q! I# G" G/ u( k2 P/ h2 50; u9 X: S/ P4 k j' K
3 60
0 k$ h% u6 a+ O* h ~4 40
9 M- M- n+ A; ?( Z$ e! H7 C( z6 40* J. W& g0 z0 [+ K% R
7 30 7. map的基本操作函数:
6 g# j B/ F# R5 g3 A' {" G( _C++Maps 是一种关联式容器,包含“关键字/值”对6 W6 a- m# S* e2 j8 H/ l% y; b* @( r
begin() 返回指向map头部的迭代器) H7 s; Z$ \/ ` H
clear() 删除所有元素
; ^+ D9 E; e0 q1 ^$ h( S: hcount() 返回指定元素出现的次数
. U9 X* x* h: x. U; [* Hempty() 如果map为空则返回true7 L1 R# J6 l- o, [/ R
end() 返回指向map末尾的迭代器
" J# |1 T. A% C! H( xequal_range() 返回特殊条目的迭代器对8 d- I+ C0 s7 _% L+ X
erase() 删除一个元素
: N1 |7 ^7 U0 V4 N/ d+ H1 \find() 查找一个元素; w" d3 _5 R7 X8 G3 i
get_allocator() 返回map的配置器: q. e+ `2 R5 B9 l5 N, X' _
insert() 插入元素
. S& d2 W* ~' jkey_comp() 返回比较元素key的函数5 N O5 H4 ]! y. n( m6 ^
lower_bound() 返回键值>=给定元素的第一个位置
5 L+ A' G3 e5 S wmax_size() 返回可以容纳的最大元素个数
0 d6 n, H4 z3 h: e8 R% Prbegin() 返回一个指向map尾部的逆向迭代器5 Y# |4 A7 T p% y D6 z% e+ @
rend() 返回一个指向map头部的逆向迭代器
% H# B: \) q( X& ssize() 返回map中元素的个数
* M' m/ R# \0 d/ L7 }swap() 交换两个map
9 E! Y9 o7 R t) W" Wupper_bound() 返回键值>给定元素的第一个位置0 X$ ~2 p8 }& K$ Q. y
value_comp() 返回比较元素value的函数 7 f$ c+ S% F$ L/ x
|