请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;" w" b9 U2 I' x- F
map<string ,int>mapstring; map<int,string >mapint;$ m" n9 V) G: U/ w3 o6 L" y' q
map<sring,char>mapstring; map< char ,string>mapchar;
! j1 P _+ D, I$ M2 pmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
* }2 g# M( j- m: p5 Z1. maplive.insert(pair<int,string>(102,"aclive"));
5 m' W& U' [5 j5 {6 r- y3 Q; e2. maplive.insert(map<int,string>::value_type(321,"hai"));
! [, Y; B. C" _/ z. \2 Q+ d3. maplive[112]="April";//map中最简单最常用的插入添加!$ f) @ @4 D/ K
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; . a$ T) f& [( v6 A+ E6 ?
l_it=maplive.find(112);//返回的是一个指针
( R5 v. f l; L. W. U: y1 Cif(l_it==maplive.end())
5 Q+ {( Z( i) [7 }cout<<"we do not find112"<<endl;- r% d8 M6 g u3 J n
elsecout<<"wo find112"<<endl;( Q& P `' g" A* }
9 G; q H. D* mmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;' b* V" Y6 Z1 P" Q5 h' j& [
4. map中元素的删除:
- r0 V" H4 V& O. I如果删除112;6 D5 N, I8 F' X& k9 H8 B5 p
map<int ,string>::iterator l_it;;
) G2 B, F9 `9 a1 _9 }1 Ol_it =maplive.find(112);$ b: G, Y( g9 f# D; \
if( l_it == maplive.end())
$ t# S# K8 O& i5 I5 z# j+ t$ V" Ccout<<"we do not find112"<<endl;5 ~& u. m7 W, x9 F3 i
else maplive.erase(l_it);//delete 112;
- ~$ X. w* c7 ^) `0 G- p 5. map中 swap的用法:
/ H2 G6 L: V9 @+ v$ n3 ~# zMap中的swap不是一个容器中的元素交换,而是两个容器交换;) j- E0 _: P1 R- b; q
For example:
4 L, [- B D. d( t#include<map>' Z* w) n( {# i& {- r0 }7 b5 {$ @
#include<iostream> usingnamespace std; int main()
7 y: t) \' Y1 }# I( t, V0 k{2 G; a5 Z3 O4 @# K' n6 ~
map <int, int> m1, m2, m3;
0 }5 n6 B2 t! {, \$ R* d9 G3 V- Wmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
% O- m& F1 w+ W' e& sm1.insert ( pair <int,int> ( 2, 20 ) );! l8 P. K |! _
m1.insert ( pair <int,int> ( 3, 30 ) );
" ~9 u6 S: g" [5 D6 { Q2 p+ W1 z, q( Tm2.insert ( pair <int,int> ( 10, 100 ) );
1 } k1 t, |6 {/ ^+ Rm2.insert ( pair <int,int> ( 20, 200 ) );9 n T9 Q2 ~ y- N3 p9 g
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";* Q6 R6 S: _9 }5 H$ Y
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ ) H# Q; H' S% K+ u! }
cout << " "<<m1_Iter->second;6 S a( g+ N+ Z6 d3 D% z; g
cout << "."<< endl; // This isthe member function version of swap
; S; W8 u i0 {2 ~- z8 B/ y// m2 is said to be theargument map; m1 the target map
, o0 R* M) B( vm1.swap( m2); cout << "Afterswapping with m2, map m1 is:";4 W. j/ a8 I+ O2 r
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
( O+ b' p3 n1 vcout << " "<< m1_Iter ->second;
9 Y5 Z; V) q. f3 k4 t, U6 ~/ `, qcout << "."<< endl; ' J& v6 D ^. p4 m+ L$ V6 g" w- X' e
cout << "After swapping with m2, mapm2 is:";
. c$ U) D3 a% c: @4 ]2 Jfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
+ _% S3 L) a# J# Zcout << " "<< m1_Iter ->second;
: Y; F8 P1 V- Pcout << "."<< endl; ' Z" d+ R# o& A4 s
// This is the specialized template version of swap' ]6 i( n) K$ x L( k. p4 a3 A
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
0 C0 Z+ h( i/ a1 z5 s" S- ^for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )1 H3 b# T4 h! P# H( P* P
cout << " "<< m1_Iter ->second;
/ c- o' y" T7 [+ Mcout << "."<< endl;
7 n K) d, u8 s: u0 K/ ~} 6. map的sort问题:
( @7 G9 Q$ \: ?* R# j' l# I' zMap中的元素是自动按key升序排序,所以不能对map用sort函数:
; m2 i7 x4 Q& k$ }5 FFor example:
0 f) H0 n4 J6 k7 a, y7 l#include<map>* v* R( i. t" W. a ]4 {9 k& ~. Z6 r5 k
#include<iostream> usingnamespace std; int main( )6 w" ^: R V! y: T; t) h" Y+ r9 R- ~
{
% G2 B1 X/ y! T5 Wmap<int, int> m1;( E- K1 C3 `/ A+ z; i/ f4 U
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
: n1 ]; a' Z# K4 m' ^m1.insert ( pair<int, int> ( 4, 40) );
4 X; h- Q0 p' m; {8 f8 km1.insert ( pair<int, int> ( 3, 60) );3 }! t+ p$ I2 _1 m2 X" O
m1.insert ( pair<int, int> ( 2, 50) );% z/ z/ B! f" E( D3 Q- n1 M8 h
m1.insert ( pair<int, int> ( 6, 40) );9 I) r7 R* j& Y/ I; N3 ]# i0 S
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
( u- A$ C$ s$ V+ \2 V' xfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )* ^1 M' S- [1 ?7 u2 n' B# A: J
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
! k3 w. z, @6 D& R, b8 J
1 Z7 q8 c$ Q* \5 j5 \4 w" w. e1 a}
, g' D$ L x) n3 S* K$ W% s. b. l4 K$ o The original map m1 is:
' ~. Y2 Z; I U1 205 [6 \* H" d. Z$ L0 N& M7 B C! G5 F
2 50
3 \# X4 W0 r2 I( c: s. j S1 t3 60
& W+ S1 D p/ W; N& o" ~4 40: R H7 V( ~1 S* ?( `& V7 j
6 405 k7 R Z* s& j1 H9 a7 @
7 30 7. map的基本操作函数:
+ G& U3 `$ |& @. s5 F+ W% p* [C++Maps 是一种关联式容器,包含“关键字/值”对
, }! y y# Q: i2 E0 hbegin() 返回指向map头部的迭代器# x2 C3 O8 z# b% t* P
clear() 删除所有元素
8 ~: f# f) f! S) M6 D; ]count() 返回指定元素出现的次数
5 N1 Z, |9 I: c1 }empty() 如果map为空则返回true
) K) ?/ G! u! z' \" U% t6 Iend() 返回指向map末尾的迭代器
7 s% O) E/ Y7 r1 [ a( jequal_range() 返回特殊条目的迭代器对! Q3 q* U/ w2 }, O: A7 j! E
erase() 删除一个元素( v3 i( L) w' {& H
find() 查找一个元素
% N' X2 h- K& ~) g2 z8 E% Hget_allocator() 返回map的配置器
7 |% p9 r, {! B& y( p0 Kinsert() 插入元素. }# g8 I5 a" s0 e$ O
key_comp() 返回比较元素key的函数& M! C. D* a! F, E% U1 b
lower_bound() 返回键值>=给定元素的第一个位置7 ?4 J) q7 k! W3 t" [9 K
max_size() 返回可以容纳的最大元素个数2 r$ X, T, U" ?
rbegin() 返回一个指向map尾部的逆向迭代器& ]9 p/ o/ H6 @$ y
rend() 返回一个指向map头部的逆向迭代器
5 a+ w# c3 U$ q+ ?5 }size() 返回map中元素的个数0 C2 X; }7 [& q& r$ R. D# a
swap() 交换两个map
8 t+ D9 e: y3 Aupper_bound() 返回键值>给定元素的第一个位置5 ~7 S* m* G3 b# z' k
value_comp() 返回比较元素value的函数 8 P5 z8 U- m, v" ] @3 l
|