请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
5 l8 e8 c2 l- I7 @- z* hmap<string ,int>mapstring; map<int,string >mapint;3 y0 m: a3 ~4 ?4 \2 y$ [; ?: _
map<sring,char>mapstring; map< char ,string>mapchar;8 i$ S$ C+ O3 y7 q' `1 N
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;1 q0 K0 M' U$ l% S3 d! Y
1. maplive.insert(pair<int,string>(102,"aclive"));
! w3 }$ Z- u+ D! c* U5 F/ r' w2. maplive.insert(map<int,string>::value_type(321,"hai"));
% M# Z$ }: d. ]1 \( X% l" d3. maplive[112]="April";//map中最简单最常用的插入添加!
( c- Z) W- L* u+ y9 C 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; + s/ @2 I2 Y8 m _
l_it=maplive.find(112);//返回的是一个指针
( v* e4 U. P# M; N8 Y% S" Jif(l_it==maplive.end())' i' I, u; _2 C0 ?- O3 {
cout<<"we do not find112"<<endl;
* o: F6 l7 l* {& i9 |& \elsecout<<"wo find112"<<endl;4 b, n, N- x, Z# U
; [: }) N- }6 P: b' a9 V& wmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;- o0 `1 y9 `' E* h3 Y% G" Y
4. map中元素的删除:
7 j+ X0 I! j) F* K) u/ i如果删除112;
! E- _8 S( S8 a' [" `+ t0 ], wmap<int ,string>::iterator l_it;;7 d1 j* |# c; ?. k6 @/ _) p7 @; ]' l
l_it =maplive.find(112);
) {. t/ _0 s' u+ T, Xif( l_it == maplive.end())
6 i5 x f" K8 h/ ~8 I; @0 Rcout<<"we do not find112"<<endl;
: @ b! g+ R! j8 c, Y9 Qelse maplive.erase(l_it);//delete 112;6 X# T% }4 h" m# u0 h
5. map中 swap的用法:
8 f" ?. U3 e6 J* sMap中的swap不是一个容器中的元素交换,而是两个容器交换;" e- _% Y9 f7 R# i$ L5 o+ g
For example:
9 f( F, a" h, {$ u# R. ?#include<map>
+ a4 Y( n, i* w% Y1 a7 s0 k#include<iostream> usingnamespace std; int main()
' U5 r* a' k% o4 q2 {{
% ?3 _# _! T# z+ C. W* {' amap <int, int> m1, m2, m3;
0 O7 x, x5 U' {map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );: O* r" q; Y- j3 w
m1.insert ( pair <int,int> ( 2, 20 ) );+ h u; s% ]& N- ?
m1.insert ( pair <int,int> ( 3, 30 ) );+ m' M4 R, t9 S8 u
m2.insert ( pair <int,int> ( 10, 100 ) );
: `0 ]& Q: a) m/ Zm2.insert ( pair <int,int> ( 20, 200 ) );+ W! |$ F/ d c n0 g1 u
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";9 j* `4 y. j Y/ K; ^9 D: b
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
$ s$ u$ |/ P6 \- n$ X6 A$ ]cout << " "<<m1_Iter->second;
. L9 o7 |9 t3 J7 B5 Hcout << "."<< endl; // This isthe member function version of swap8 }* f. z. m" i# \# G
// m2 is said to be theargument map; m1 the target map0 a' A8 `2 T$ R- [) P) g8 D
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
$ y& p2 f% N4 R$ c$ ]. {' @for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )* V% x; p; S' Q1 _
cout << " "<< m1_Iter ->second;5 b6 ^5 p3 u9 j2 `3 T/ C! ?; @, I
cout << "."<< endl;
8 D6 s" l: [/ L4 K% [3 q [" Mcout << "After swapping with m2, mapm2 is:";4 W9 w* T9 @( z5 t7 o& M3 |& }8 i
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
! l- [) i; n7 M; C9 {' K& }cout << " "<< m1_Iter ->second;
M8 A' l- ~# `: c/ ^2 Ccout << "."<< endl;
% g3 v/ S6 k' ^2 U, n8 }' t& @// This is the specialized template version of swap1 [9 D! z6 J1 R: t' W/ {( _/ s
swap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";
2 W) k8 N- O$ y5 R$ ^ p9 M+ @# gfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )* t0 d5 H! s8 V2 E) R, `
cout << " "<< m1_Iter ->second;7 @& l$ }6 u0 d6 p. _
cout << "."<< endl;
+ m; K( L+ l. T% g3 J# Z& D6 ~} 6. map的sort问题:
2 Z* x# J% n% X" U$ iMap中的元素是自动按key升序排序,所以不能对map用sort函数:; s" A5 t* V6 F8 B" D i. ]
For example:. K. J, s. {% S, ^. _: W8 a3 w& a
#include<map>
$ v1 Q& u) y' r* E+ r#include<iostream> usingnamespace std; int main( ): n4 q, d' h# R" C- x' J
{* K4 P5 P9 N8 t9 W" U9 z( i9 _
map<int, int> m1;0 t: q2 `+ ?8 I- g
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
3 K" X/ P' M& m x' \5 mm1.insert ( pair<int, int> ( 4, 40) );, g) `' `* Q' ~ h; e' r
m1.insert ( pair<int, int> ( 3, 60) );
N( \4 B. D% mm1.insert ( pair<int, int> ( 2, 50) );. B7 ?7 D9 @+ I5 X; F, R9 ~6 R
m1.insert ( pair<int, int> ( 6, 40) );0 V7 p) F* L: ` D% m
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;' n5 Q# `8 w$ i
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
1 S$ s" w* L4 O& B% e8 T! Lcout << m1_Iter->first<<""<<m1_Iter->second<<endl;
; @. e3 a: I9 q) i. d
; ^9 k. C1 F8 g8 }' r; U' H}
: L5 K% C2 a+ o: ^, X0 a2 ` The original map m1 is:# q; E1 _2 z6 Q/ a! b* p9 r
1 20" [ v; I. o% |4 w2 `/ c% A
2 50+ J _( U8 z: ]( }. ]8 g. W
3 60
{5 ~% R6 s% R# y K4 40
. K1 m# C3 h* h* G& o6 40
2 H- v% \& V% d1 }7 30 7. map的基本操作函数:
# S$ V; }: g @C++Maps 是一种关联式容器,包含“关键字/值”对+ P2 j; S9 p$ Y" k3 h
begin() 返回指向map头部的迭代器7 ]" G9 b: e! [, V9 \0 p+ V
clear() 删除所有元素
, ^0 V6 F! o0 x# ~count() 返回指定元素出现的次数+ i9 ], ~* s5 }5 Y# c& a; h$ n
empty() 如果map为空则返回true& {1 P- p; b) Q1 b+ K! j
end() 返回指向map末尾的迭代器 x* K$ u' J; m) G4 ?
equal_range() 返回特殊条目的迭代器对7 }' J1 [0 }3 P! J
erase() 删除一个元素6 ]4 a6 X9 @! T- }+ |# g2 G
find() 查找一个元素( d# T% s2 U& x* ]1 l
get_allocator() 返回map的配置器
$ M1 a5 |( z4 h0 O- {/ V& ~7 Zinsert() 插入元素
! ~3 Q1 _5 K) g1 ~2 Akey_comp() 返回比较元素key的函数5 {& S: ^/ B6 R8 N1 V
lower_bound() 返回键值>=给定元素的第一个位置8 e# Y5 c% `% U+ k( n2 N
max_size() 返回可以容纳的最大元素个数
R8 D1 C$ V0 @2 @" J j0 [rbegin() 返回一个指向map尾部的逆向迭代器
! O" }- i$ J$ prend() 返回一个指向map头部的逆向迭代器
- J) u# x7 I4 I! M- Z2 d* S# ^size() 返回map中元素的个数
# c! l# r: B* ^/ Vswap() 交换两个map
M4 S/ X6 Z* i( }. Y0 Oupper_bound() 返回键值>给定元素的第一个位置5 K$ S0 @ j# n! m' g
value_comp() 返回比较元素value的函数 / l0 w+ R; F7 ]! H% t4 E
|