请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;5 j3 B- E# ^' Y6 n/ ^3 m3 @
map<string ,int>mapstring; map<int,string >mapint;5 o% h9 k6 ] b1 ?6 i* H y
map<sring,char>mapstring; map< char ,string>mapchar;' v6 x3 u* B- w8 k7 f0 v
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;" ]7 n+ d5 ^5 @
1. maplive.insert(pair<int,string>(102,"aclive"));
8 [' U. A1 P% c1 Q0 p2. maplive.insert(map<int,string>::value_type(321,"hai"));
" F" [ p$ `6 X' S j3. maplive[112]="April";//map中最简单最常用的插入添加!; r: z" h2 O' a: m
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; ) x6 O# B3 b6 W
l_it=maplive.find(112);//返回的是一个指针/ C0 N7 s, _( y6 a2 V H
if(l_it==maplive.end())5 K. F" i% i1 @2 ]
cout<<"we do not find112"<<endl;
# }: O/ P3 u& p3 n$ b3 ~elsecout<<"wo find112"<<endl;
& n$ g' X0 K5 M . \+ r7 o0 e4 e$ ^% r- h
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
! @+ \- P4 D; c( v 4. map中元素的删除:
% q. Z+ f9 H' @; O如果删除112;$ v! \6 U) |; v' O
map<int ,string>::iterator l_it;;4 X( K, K2 Y# P, O8 }# o
l_it =maplive.find(112);+ ?8 t+ Q) ^/ d% [: `5 N$ Z
if( l_it == maplive.end())
3 }5 Z1 Q1 `: }! l6 }( Vcout<<"we do not find112"<<endl;% e, t0 G. I: w9 D
else maplive.erase(l_it);//delete 112;. v0 X2 ^3 u0 |; V2 M
5. map中 swap的用法:
5 |8 g7 n" F: c4 h# q5 K1 Z) E& TMap中的swap不是一个容器中的元素交换,而是两个容器交换;( b( v0 h5 `& F+ t P' L
For example:
z d8 N' j8 Z+ x#include<map>
# L w, A: s! {) w* V#include<iostream> usingnamespace std; int main()
+ O$ |# \/ r, Y# [' m{
% a* D4 \% g" }& ^% Fmap <int, int> m1, m2, m3;+ f7 \/ y. \/ _, O6 V* k J; m3 ^
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
4 G0 j; F% ~$ h$ `9 ], A- l6 A- mm1.insert ( pair <int,int> ( 2, 20 ) );3 J2 F! o! Q# M. J2 r2 r" F
m1.insert ( pair <int,int> ( 3, 30 ) );" G% Z7 o% n7 \- r% G
m2.insert ( pair <int,int> ( 10, 100 ) );
5 l* J6 B6 A8 V& {m2.insert ( pair <int,int> ( 20, 200 ) );
) D- [+ u& J5 v+ [8 ~1 Sm3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";+ Y) O. r: H4 u
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
3 [7 ^# s3 D5 M, h: acout << " "<<m1_Iter->second;
" H( Q1 o$ c9 a; l$ o; m( \& s( f( Ucout << "."<< endl; // This isthe member function version of swap6 t3 Y5 }' z- F5 R: `2 Q
// m2 is said to be theargument map; m1 the target map1 q& k$ r. I9 s+ o
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
% B3 j C8 f4 `; i Gfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )/ [8 L' N$ P# W
cout << " "<< m1_Iter ->second;# H6 f& X4 X; `# r. W
cout << "."<< endl;
+ M/ `+ }; H+ w" Tcout << "After swapping with m2, mapm2 is:";* N6 I1 q9 a% Y+ s" D/ V$ U
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
6 {/ ]* s) l" e& P2 bcout << " "<< m1_Iter ->second;) j4 p$ D( T' a; ]2 ~, {
cout << "."<< endl;
6 I; _ A. g& ^) ?7 `! D3 f3 q
// This is the specialized template version of swap9 G# M3 d, t; a1 A+ v
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
8 B9 I! V, ?8 [3 |) z7 v, ], K+ cfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )3 G% t: u) O0 c9 x1 J
cout << " "<< m1_Iter ->second;9 D" Q9 S% @* I h
cout << "."<< endl;+ M2 T" u$ U7 `7 \6 ^! p; b
} 6. map的sort问题:6 ]) e1 c9 t# n# p! D# Z: ^
Map中的元素是自动按key升序排序,所以不能对map用sort函数:8 [2 Y: s9 w. Z8 |- ^/ J. R
For example:% Z3 Y0 s: R/ @$ k& n% _- H
#include<map>1 x% u' u9 |4 T" a8 h% p' q
#include<iostream> usingnamespace std; int main( )
. |) H' W8 s& G5 z4 q{6 O+ I; Q O1 \- D
map<int, int> m1;
4 V p0 e8 Y U4 ]- t2 N$ S, smap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
" A" W! I l& [: o& P7 p0 tm1.insert ( pair<int, int> ( 4, 40) );* G# K5 Y/ h6 h8 f
m1.insert ( pair<int, int> ( 3, 60) );+ v* |5 l" c6 E" J4 T
m1.insert ( pair<int, int> ( 2, 50) );0 p; w$ z v% Q( o5 S8 G1 E6 B
m1.insert ( pair<int, int> ( 6, 40) );
/ |+ M- u6 ?8 e& U; pm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
2 `- k+ |& G2 F1 @/ I& ` kfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )' W; j8 |5 |5 U B, @( A' o
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;9 y+ o2 a9 K' G7 W+ {9 a7 Q
' k0 J4 ~# [8 n! |
}. I& p9 O6 @2 q! \. P/ N& K
The original map m1 is:3 B3 u5 x+ K0 R% y4 n9 P$ Q2 R
1 20
' R( \8 E3 \" {, @2 50
. ]- h$ j( W* l: t5 F3 60
8 o6 ^# D: T* b* t4 408 o$ j9 N$ O3 d' x. U
6 40. m/ R3 K7 h/ P% S
7 30 7. map的基本操作函数:
: j1 c$ p$ D: I/ c) E0 @C++Maps 是一种关联式容器,包含“关键字/值”对
A) n3 E% `. L# Sbegin() 返回指向map头部的迭代器
+ S9 J# W) l" C) D, {3 Q8 cclear() 删除所有元素( B' m9 ]& Y; i5 c
count() 返回指定元素出现的次数
1 l4 @, c4 D$ Oempty() 如果map为空则返回true
) I9 e1 W4 a" Y) ^end() 返回指向map末尾的迭代器
7 z# R' K& S, I* o yequal_range() 返回特殊条目的迭代器对, S4 j! S" }/ K; m- j9 e$ z
erase() 删除一个元素
6 ? h* p( z8 |! t2 u2 M3 Yfind() 查找一个元素2 x& T. z7 k# B4 ]) u. \+ g
get_allocator() 返回map的配置器1 w! i5 ]& [, |6 f1 O
insert() 插入元素
. A+ H. j7 M' K1 {, [% Nkey_comp() 返回比较元素key的函数
j5 r6 z8 y2 Mlower_bound() 返回键值>=给定元素的第一个位置! h. K$ N& l2 c8 }! o. w8 H
max_size() 返回可以容纳的最大元素个数
' u" f: N: {# O. q$ e& yrbegin() 返回一个指向map尾部的逆向迭代器# M' s* ~, j' S
rend() 返回一个指向map头部的逆向迭代器( l2 ^) _* h4 i
size() 返回map中元素的个数
; D) C0 U: s+ [" j6 Eswap() 交换两个map: r2 ?1 K5 R4 l8 f+ s, T
upper_bound() 返回键值>给定元素的第一个位置+ X' P, Y8 i% O) V" \9 g1 }
value_comp() 返回比较元素value的函数
1 d( W; y F2 Z |