请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;- z% H, a; X$ c' O- D7 I4 ~; W
map<string ,int>mapstring; map<int,string >mapint;
, g. h) ~3 k( W; l' v' m: ]map<sring,char>mapstring; map< char ,string>mapchar;
) t7 A2 `. d, c) Emap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;8 i4 L0 p( p2 e+ t4 ^# m
1. maplive.insert(pair<int,string>(102,"aclive"));
* R; d) X0 C2 T7 @9 a/ Z7 m2. maplive.insert(map<int,string>::value_type(321,"hai"));4 T7 d* F! t& }- O- r8 d& m
3. maplive[112]="April";//map中最简单最常用的插入添加!6 i$ W8 \: o. y( @
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; & N# M. W. C' t2 s. V. \' }& e
l_it=maplive.find(112);//返回的是一个指针& c; p: _3 \( b" o; z; J' e
if(l_it==maplive.end())0 X2 ^# c2 z! j
cout<<"we do not find112"<<endl;
# g2 v6 L" g3 `elsecout<<"wo find112"<<endl;
( K+ E; j5 W# `9 ^ s( w2 V& }; m& {; ]
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;; a- W! Q6 V+ I
4. map中元素的删除:
0 v! }9 s) [- ?0 @, F如果删除112;
8 C0 C! @5 T6 |2 z" Imap<int ,string>::iterator l_it;;
2 G8 D: N3 I, [7 C+ `9 Ol_it =maplive.find(112);
5 f/ U& _; s% l8 N! z# O" jif( l_it == maplive.end())
* v$ N& z G9 Vcout<<"we do not find112"<<endl;
: f) e2 z/ o+ G* z" Q- Y, w/ uelse maplive.erase(l_it);//delete 112;; o) }# R% i9 t/ F
5. map中 swap的用法:/ x9 f6 K8 W; ^4 v6 v m
Map中的swap不是一个容器中的元素交换,而是两个容器交换;* C# J' F% j! M$ A, ]! q
For example:3 p9 P* }: | p5 ^$ H0 L8 y
#include<map>
s4 J+ R+ [8 V' p6 }5 A#include<iostream> usingnamespace std; int main()
" G \5 L( F/ @{
6 {; U6 G$ R: n# c% a/ }% smap <int, int> m1, m2, m3;* a1 k x6 J! W& S) e5 i9 O2 Y3 e
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
) ^# x' `, z! z9 M6 \2 xm1.insert ( pair <int,int> ( 2, 20 ) );
8 I% T$ c" {. b8 @; s4 t. }/ Cm1.insert ( pair <int,int> ( 3, 30 ) );
# b( C# m. ~" O. u- M2 Lm2.insert ( pair <int,int> ( 10, 100 ) );3 E2 z) `2 N8 B7 s
m2.insert ( pair <int,int> ( 20, 200 ) );
2 r# P! o w6 a. G5 N$ `3 om3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
2 i2 u1 q3 {7 s4 c8 h+ ?4 b9 g; R6 V( Zfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
]$ P' [! |* n% G7 [! p8 y* tcout << " "<<m1_Iter->second;; h/ _2 y' h6 h# l& _5 M( w
cout << "."<< endl; // This isthe member function version of swap% I# P+ P7 v( g# a
// m2 is said to be theargument map; m1 the target map
2 [% a; J; L8 C" d+ H1 g" X1 G5 pm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";4 h( f0 P& I& j& N% ]$ H' d1 t+ P
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )0 p/ y) J. r; a- m
cout << " "<< m1_Iter ->second;
# K2 g# e. {' S, C/ J, M6 E& O& ncout << "."<< endl;
5 j6 M6 |/ D# Y6 Ecout << "After swapping with m2, mapm2 is:";
, S: }; i# A+ y. m- H- J9 }% ~& p& Ufor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
& I1 x2 D) Y2 b' Lcout << " "<< m1_Iter ->second;
4 _/ C; {5 W9 `- F3 Lcout << "."<< endl;
3 _; m) b5 w% r) u( E9 u, `// This is the specialized template version of swap
2 z2 W' {8 P) }+ F. N* ^6 Aswap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";( D, j% o2 D4 D5 Q9 j4 X
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
9 U' B7 w2 o4 }5 o: y7 ?" E3 K) gcout << " "<< m1_Iter ->second; a$ h) F# k' W
cout << "."<< endl;: c, O' p) E$ x+ k
} 6. map的sort问题:4 b% [0 b1 N, H& Q* N0 L
Map中的元素是自动按key升序排序,所以不能对map用sort函数:! `( B A5 d% t: y3 \
For example:
- _' K2 t. V" e7 d#include<map>; u3 Z2 W8 E# [: U
#include<iostream> usingnamespace std; int main( )8 j8 W4 [. ?9 Z5 v" k+ d- H
{7 L% b2 d6 H# Y* }
map<int, int> m1;9 N# O! h/ U8 @8 @& S$ J; ]
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
3 a* u5 y& }0 l3 o# x5 o! Bm1.insert ( pair<int, int> ( 4, 40) );
8 F- P; {& T) S, k9 L* R4 gm1.insert ( pair<int, int> ( 3, 60) );4 E) @ X3 u6 a7 ^0 I) ?
m1.insert ( pair<int, int> ( 2, 50) );0 ~7 U6 W5 h0 Z. {
m1.insert ( pair<int, int> ( 6, 40) );1 U" _/ p$ P: v/ F3 E
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
( T a0 R2 v! M+ H3 ]# c6 z. r j$ w# _for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )% S3 V% O2 W4 d. T# e
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;0 g. C4 s& i) L2 j
" X) U. w+ J# D& ^) w# E+ {: P; Q}2 @9 S' ~5 i4 p# G- T$ g0 |2 w4 Z
The original map m1 is:
; n9 t! L5 [$ K$ X% o1 20
# u4 O3 V& u: \% |1 c; u2 50; j+ Y! r8 Q6 d% _3 d, ?6 z
3 60
! L$ D$ u0 b' F8 \5 U4 40
( ^+ E. l' h0 {) p. Y6 408 C# _3 X+ C# A2 \1 b7 Q
7 30 7. map的基本操作函数:
8 c- N* e1 z9 O& z, f, {! DC++Maps 是一种关联式容器,包含“关键字/值”对- K/ A3 {( t5 g5 X5 e+ o
begin() 返回指向map头部的迭代器% u9 E7 B* Y; D9 |# Z: {6 l
clear() 删除所有元素/ {- i5 `; P$ J% M+ l: }) E
count() 返回指定元素出现的次数* u4 f- s' \9 ]! F" B! s0 R& u/ e
empty() 如果map为空则返回true$ P2 Q2 [* J$ r
end() 返回指向map末尾的迭代器
3 ^) f8 A* V( @equal_range() 返回特殊条目的迭代器对3 n4 {' L+ F0 q6 D
erase() 删除一个元素& \2 b) O+ j H9 e7 p
find() 查找一个元素
2 o5 y' V/ Z1 w. ]get_allocator() 返回map的配置器
/ X& M3 F# P1 k0 \6 dinsert() 插入元素
& p! U0 q! O X9 {. X& n" q! gkey_comp() 返回比较元素key的函数0 L+ E% K2 `' D$ Z2 d/ p3 H; ~6 p) p
lower_bound() 返回键值>=给定元素的第一个位置2 _9 Q. H6 w* K5 r
max_size() 返回可以容纳的最大元素个数
/ m5 X9 x/ W: l- ~rbegin() 返回一个指向map尾部的逆向迭代器
6 _2 ?6 v2 U$ C. |# ]rend() 返回一个指向map头部的逆向迭代器 R' }# [ A* d N6 D4 {9 I& v
size() 返回map中元素的个数
' ~& _: ]7 b( d! W7 r1 c7 Gswap() 交换两个map; a" ^+ A4 T& O* x! ~( P: q1 K
upper_bound() 返回键值>给定元素的第一个位置' }% M9 i+ v0 J4 a; U' z
value_comp() 返回比较元素value的函数 5 L& e0 Q* _) F
|