请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;. ]) e/ Q& H9 R- T/ o& ?8 k
map<string ,int>mapstring; map<int,string >mapint;' k) @( W& B% V+ b, \6 \: P# ^
map<sring,char>mapstring; map< char ,string>mapchar;; R8 H0 W- i5 b. Y
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
9 D1 u1 W& y5 r6 Q1. maplive.insert(pair<int,string>(102,"aclive"));0 I) `" ~: {& d
2. maplive.insert(map<int,string>::value_type(321,"hai"));8 W' }/ x) I7 @
3. maplive[112]="April";//map中最简单最常用的插入添加!
. c: o/ g+ |0 H! s } 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
; w- N8 A4 I1 y' M& A" g5 o2 p1 Fl_it=maplive.find(112);//返回的是一个指针. ~% B+ E6 C3 Z8 p; c
if(l_it==maplive.end())
& X) g; m/ j9 Y! W( \! Ocout<<"we do not find112"<<endl;$ `) I8 ]$ O( n# a
elsecout<<"wo find112"<<endl;) v! v3 N1 r& U2 J" }8 s( m; z
$ Y1 K9 k4 B2 G; rmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;5 Z- o6 Z: Q/ I' g
4. map中元素的删除:( }; {& H6 K9 Y9 H
如果删除112;# d* C2 m2 w9 M" Z8 \1 L7 s
map<int ,string>::iterator l_it;;
% B/ c" u1 z s+ g: y! B5 Dl_it =maplive.find(112);
0 {' l- y3 ]% r' p& jif( l_it == maplive.end())* ]: y4 T) h8 ^+ M- m! f8 T
cout<<"we do not find112"<<endl;2 C9 ~, i: D) q6 k' a" n
else maplive.erase(l_it);//delete 112;
! P5 p* |# ]: \, u" G+ H 5. map中 swap的用法:+ d* w' ^) ^0 ^* _" H5 T9 ^
Map中的swap不是一个容器中的元素交换,而是两个容器交换;) S$ I) s# l9 {2 U* {; o* C9 D, N) B
For example:7 F3 ]* N8 T% }$ d4 O- Y1 C
#include<map>
# R! L" o4 F! g" ]8 q#include<iostream> usingnamespace std; int main()
) n" T9 i* ]8 S8 }. D0 ?: N{/ R/ R6 |8 w* g: a, Y- q' D
map <int, int> m1, m2, m3;
/ h! x% ?2 a8 f6 w; cmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );: c4 o+ ?* [4 E! F) G
m1.insert ( pair <int,int> ( 2, 20 ) );
, p1 T" N) w& q' H) k* m! s+ Km1.insert ( pair <int,int> ( 3, 30 ) ); O4 l" k9 H7 F0 W& |% g
m2.insert ( pair <int,int> ( 10, 100 ) );/ f% r2 \5 N) k3 r) e" N. S% N
m2.insert ( pair <int,int> ( 20, 200 ) );
; Q8 U9 m; p3 b' g8 \m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";: T/ e! _) n) W( D4 U7 ~" C
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
. r7 p" d3 x5 U/ N5 tcout << " "<<m1_Iter->second;
8 ^7 M; t2 v4 t# I, jcout << "."<< endl; // This isthe member function version of swap2 `( v& g9 x3 n$ K
// m2 is said to be theargument map; m1 the target map- c0 {5 R2 v: m% ?& V! E0 v
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";( W3 x' x. O, D8 B
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
' g) d3 X7 o- qcout << " "<< m1_Iter ->second;
9 u/ v3 x8 i# }" S9 G- k0 Wcout << "."<< endl;
' C8 P z9 x7 M0 A& U7 d& scout << "After swapping with m2, mapm2 is:";3 \; D4 A) {6 g" p/ [5 I: g
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
1 O1 H9 m: j, Z# p( _/ \6 p wcout << " "<< m1_Iter ->second;
0 Y$ ^) x) ]! |/ ?7 S* O3 ^cout << "."<< endl;
$ ]0 t7 }8 V1 J; F3 c6 K! t, E0 k// This is the specialized template version of swap/ @$ @9 H6 w. f$ { m! ]
swap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";
1 g+ W& |- j, [7 `* tfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
1 @5 t% T9 L- O4 r% F1 ]- |3 g6 u0 |cout << " "<< m1_Iter ->second;) c* t! V5 B* z1 x3 a
cout << "."<< endl;
) i8 [0 t* b! u2 S2 w, P3 K} 6. map的sort问题:
. }7 C7 v( h4 s$ L3 m# sMap中的元素是自动按key升序排序,所以不能对map用sort函数:
7 f: `) [* ~2 F+ W' r/ g1 _For example:+ \4 [' o) r" D& F/ T
#include<map>2 H9 f( J; Q3 G/ f' F1 r
#include<iostream> usingnamespace std; int main( )
% A0 n% ~2 `% Z9 L W7 s{
# a' B+ S) R, N9 |map<int, int> m1;6 r C9 E) j9 A! z
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
8 j8 S4 ]4 |3 e$ O, }8 B% d" Fm1.insert ( pair<int, int> ( 4, 40) );3 K9 w" P5 v2 d8 F
m1.insert ( pair<int, int> ( 3, 60) );
- H+ n4 ?+ r. Q. Bm1.insert ( pair<int, int> ( 2, 50) );
0 F4 l, u( ] A4 ?3 {m1.insert ( pair<int, int> ( 6, 40) );0 G. _, s. v+ G* A: T# Y6 v- B
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
5 S1 I& r( x( L* A, v6 l3 b8 Rfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )5 {# C& V- _- ?/ B
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;, m# q3 q: S: X! i6 O$ ?# [
. ?- E+ E& @; X; S6 L. ~: W
}" A( V1 X! m8 S7 v% x
The original map m1 is:
# P$ j+ I4 P7 y: Z6 M9 T6 q1 20) ~3 J; k% W8 u; O: S' s1 o& d
2 50) c" A6 r: O( B3 U
3 60" x: i2 C+ s; ]3 p
4 40/ Y( t5 f- R3 R! j6 H% E
6 40% \4 o5 T0 I: E4 y/ E
7 30 7. map的基本操作函数:
& J" }+ E9 m* x& ]; t! _" g7 }* ~, HC++Maps 是一种关联式容器,包含“关键字/值”对
+ t& n3 g' Z. g% [: Zbegin() 返回指向map头部的迭代器
0 J, \1 e3 E6 K; s: O' p4 e2 U0 Zclear() 删除所有元素1 B! U. X$ i7 p! |3 b1 ?2 P# l
count() 返回指定元素出现的次数
8 Q9 a4 U5 Z% c: C+ a1 Hempty() 如果map为空则返回true3 l; L$ u0 A# \
end() 返回指向map末尾的迭代器% @! l2 _) ?6 }8 Q6 B! u) j) S
equal_range() 返回特殊条目的迭代器对
3 V6 _& h; S. M) Q( Derase() 删除一个元素# _( n1 b& f: ?7 J& O/ V3 X
find() 查找一个元素' [& o/ \7 N4 N( ` r3 \
get_allocator() 返回map的配置器( n% Y. Z% _4 N2 L7 e
insert() 插入元素. O! N" a7 z' g5 ?
key_comp() 返回比较元素key的函数
9 c: J2 J! Y( J3 `/ c$ l& R" ^lower_bound() 返回键值>=给定元素的第一个位置. B1 ?8 D6 q" v) y1 [
max_size() 返回可以容纳的最大元素个数
V! h. J8 b" _/ Z$ ?% F+ Trbegin() 返回一个指向map尾部的逆向迭代器% `8 I/ |" Y0 _1 ?6 d4 h& `
rend() 返回一个指向map头部的逆向迭代器$ Y7 R; S1 c& n b
size() 返回map中元素的个数8 C& ?' Q! o, ^- @; h* p
swap() 交换两个map8 d9 B7 N1 r5 L9 e( ]
upper_bound() 返回键值>给定元素的第一个位置
9 T5 I' s1 K% c+ C& [* uvalue_comp() 返回比较元素value的函数
+ \, x* Z h+ X* N/ D8 r6 R% E; J) ? |