请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
/ X# S$ a( ?6 ?0 Q$ T& emap<string ,int>mapstring; map<int,string >mapint;) |3 D8 k2 X# Y* |. c4 i/ T
map<sring,char>mapstring; map< char ,string>mapchar;, C) i& B+ O; t& @
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
: o* ?: ~4 T3 E+ m ~# }7 Z7 I1. maplive.insert(pair<int,string>(102,"aclive"));
' Q6 v. ^) f$ ^4 p6 s, H, X" Y2. maplive.insert(map<int,string>::value_type(321,"hai"));# o$ u: B) F+ \5 f$ v5 a; p6 p
3. maplive[112]="April";//map中最简单最常用的插入添加!
8 x8 A# a' Y& m5 k" ?* p: G2 c 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; & c; f6 E6 n0 {' q) C6 f( f: b' P2 [
l_it=maplive.find(112);//返回的是一个指针
! G" U8 W$ F' I$ m8 {; Eif(l_it==maplive.end()). Q' n+ O3 q. S# u" F: f
cout<<"we do not find112"<<endl;
1 l# A; J( Y3 ^/ r8 [8 F/ y- A6 jelsecout<<"wo find112"<<endl;6 V. e: U' K; J: d( C2 V I4 i
4 j+ U2 `5 L& v) C: P+ E
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
( R; }' Z% f! p- b- N+ V( ~ 4. map中元素的删除:
7 |, p$ y! ^6 p" j" z- @如果删除112;" j R( E. W5 w! c+ A1 q7 a
map<int ,string>::iterator l_it;;
% {' S3 b3 b. C% C) Hl_it =maplive.find(112);
' S4 G) _$ x) o' C9 tif( l_it == maplive.end())) b7 @# v/ t* n+ ^% Q+ G
cout<<"we do not find112"<<endl;
( P! f/ k+ b2 @- w7 X+ Qelse maplive.erase(l_it);//delete 112;
: I, v; T% ?1 f) i: o( I! y 5. map中 swap的用法:5 m' ?! k9 w% _& A' d7 J# e' P
Map中的swap不是一个容器中的元素交换,而是两个容器交换;9 L$ O( q- h: N1 M* ] l
For example:5 D0 w3 d) n' `& I% U
#include<map>
+ G1 K) y/ ~& j#include<iostream> usingnamespace std; int main()8 _6 I F0 h, J( z" V) A* j
{9 x% m1 Y. }+ r0 z
map <int, int> m1, m2, m3;
B4 O5 S1 d/ {/ ~- _map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
5 g3 k; N( f& d5 Wm1.insert ( pair <int,int> ( 2, 20 ) );8 t. |1 z( Y2 c5 w$ n
m1.insert ( pair <int,int> ( 3, 30 ) );: s* D1 Z( k% j9 [4 {; h1 S
m2.insert ( pair <int,int> ( 10, 100 ) );
1 L0 u! j4 t0 J. d5 Vm2.insert ( pair <int,int> ( 20, 200 ) );
8 T, o5 K. f- g$ w+ j- K9 c, km3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";4 ~3 Q: `7 W: w/ U
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )% }2 c4 u, S( E) \
cout << " "<<m1_Iter->second;2 J( I8 h" B r1 w# }) j& ^1 Z) D
cout << "."<< endl; // This isthe member function version of swap+ q @6 |2 I" T
// m2 is said to be theargument map; m1 the target map
) j- L0 t2 |- Z* O7 }, z) rm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";; ]3 M/ u* ]' ?! {/ q' a, I! p/ p
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
- q; r9 h# U! M- w4 Qcout << " "<< m1_Iter ->second;
. a$ {5 x9 ^$ B% L- `cout << "."<< endl;
1 H# E7 g0 H0 L3 f# ~% s. G* Q5 rcout << "After swapping with m2, mapm2 is:";& e: K, {. L8 e9 }
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
- P- @; t3 ?; s" j. Y3 v9 H. Ocout << " "<< m1_Iter ->second;
* v, c6 G, _/ l, T5 fcout << "."<< endl;
) k+ H* Y* h4 k// This is the specialized template version of swap4 v% W, s- I" x% A
swap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";
4 q9 a$ |) _: c/ K" K$ w& C6 Ofor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
1 Q& p* Q) h8 V! E0 h4 |7 Fcout << " "<< m1_Iter ->second;
: w5 Z- ~% Y0 H- x7 [cout << "."<< endl;
4 P1 a8 Y, y0 r! z} 6. map的sort问题:
) q4 d# r% b* ~ o8 FMap中的元素是自动按key升序排序,所以不能对map用sort函数:
& j/ v) b4 j9 x3 oFor example:
4 O! \9 R: \! z* N N* K4 @#include<map>
: I4 @) g, ?, _7 D% P#include<iostream> usingnamespace std; int main( )
0 t0 |4 a% ?: s/ Y6 E& t{- h0 G; d: r7 f9 J& [
map<int, int> m1;
5 Q4 `2 g5 [+ M% Jmap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );* E6 ?. I, j6 x
m1.insert ( pair<int, int> ( 4, 40) );
2 g |2 G8 D3 @9 Lm1.insert ( pair<int, int> ( 3, 60) );
' O$ `! \* B5 d# A* w$ l9 r. u+ H' @m1.insert ( pair<int, int> ( 2, 50) );* ~, v3 e) A+ H) f
m1.insert ( pair<int, int> ( 6, 40) );
9 z6 x- Y( s1 j2 Tm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;: T- g0 p8 O# n7 E8 C- W
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )' L; q3 b# T0 r2 E) T# C, f% d7 {( h
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;# l/ l% }& I$ J& u
" C* m* M8 {9 e/ y" e
}: d8 V$ I: f2 J) o' V
The original map m1 is:& L3 b! N( [6 ?/ v( l( t
1 20
' G0 r2 g6 [" j# T/ Q4 L" G! h& z2 50( C; T5 ], f: Z* q/ L" {
3 60
1 e% M7 C- x3 G0 c9 U6 F4 406 K1 D; j7 r( Q/ q; S( a
6 405 F* k, C( l. T+ V& \% f
7 30 7. map的基本操作函数:0 v# @' O' K' u* j
C++Maps 是一种关联式容器,包含“关键字/值”对$ A; l4 j: Z! F1 ]* U$ W
begin() 返回指向map头部的迭代器
. P6 o1 f: u. r0 t S! X# r7 Aclear() 删除所有元素
! d7 C# o1 v) p7 q, K% tcount() 返回指定元素出现的次数1 _+ C" B7 a$ P1 m3 N7 c7 s
empty() 如果map为空则返回true, U d) u$ A% Q6 Z$ m3 ]; E ?. t1 Y3 }
end() 返回指向map末尾的迭代器# _9 ?, A. w& Z! L
equal_range() 返回特殊条目的迭代器对
# q( s( ]& T7 {+ I/ a/ gerase() 删除一个元素
- ?0 @1 v7 t! z C7 A2 E$ k% lfind() 查找一个元素0 s- N7 S. I$ p
get_allocator() 返回map的配置器( t$ M" |$ ~& u$ y; D6 _, _
insert() 插入元素0 m. K; E% |& b% K
key_comp() 返回比较元素key的函数 N" U, q0 d; k: ?, D
lower_bound() 返回键值>=给定元素的第一个位置, S; H) x' ^9 S$ J7 ^4 u; ~8 V5 v
max_size() 返回可以容纳的最大元素个数
# a' E* [3 q( Q" nrbegin() 返回一个指向map尾部的逆向迭代器# A$ ~+ ]( k9 C* j9 u$ |2 q
rend() 返回一个指向map头部的逆向迭代器
7 ~$ S# V2 }3 \1 r; ~size() 返回map中元素的个数8 j5 d7 |0 W% x6 x1 A
swap() 交换两个map
; n0 A4 A% `9 V m* h8 Supper_bound() 返回键值>给定元素的第一个位置
6 ~' h# y o4 U& E3 R6 Yvalue_comp() 返回比较元素value的函数
: E% M, C! j3 E" a |