请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
/ ~' d, C6 f/ b8 W: Ymap<string ,int>mapstring; map<int,string >mapint;% m' Y+ ?; C+ Q G
map<sring,char>mapstring; map< char ,string>mapchar;* i3 V* \, w q, d
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
: Z! E" u* z' X& `8 ?0 R3 F/ ]1. maplive.insert(pair<int,string>(102,"aclive"));
2 z9 R% K! i& n$ q$ `* h2. maplive.insert(map<int,string>::value_type(321,"hai"));7 W* [+ _4 Z4 r! K4 \# [! }4 }& Q. f
3. maplive[112]="April";//map中最简单最常用的插入添加!& C/ M7 M; ~# J) r9 I/ O% I2 m
3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
( g) K/ X* N2 m ?l_it=maplive.find(112);//返回的是一个指针2 W' n9 D1 u& @) b9 c& ]$ o
if(l_it==maplive.end()) W1 J1 f+ E; r+ y2 [9 D4 I+ E- L1 I2 ^
cout<<"we do not find112"<<endl;
4 n7 a: |, U. V1 u7 u" y0 [elsecout<<"wo find112"<<endl;- d* H/ W( p, z9 }: Y- U7 E
' |: F9 h* t% B, r1 Mmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;/ q; w) R" S( D# R# k0 U8 r/ W2 E
4. map中元素的删除:* ~$ m1 \$ ?7 W" f/ h
如果删除112;' l& j% [( K- {5 s& C2 o3 A
map<int ,string>::iterator l_it;;, h2 o6 f. U# J k9 J
l_it =maplive.find(112);4 }2 t8 \8 f5 l' V7 \. R0 T$ H
if( l_it == maplive.end())/ ]9 j+ o& H- s: J* {4 _# C( E
cout<<"we do not find112"<<endl;
$ R; k5 G- { N" Ielse maplive.erase(l_it);//delete 112;0 q7 y' ]! [9 e8 P8 j
5. map中 swap的用法:
7 j, {. d( q/ AMap中的swap不是一个容器中的元素交换,而是两个容器交换;
: e/ T' R/ N* x5 `For example:, Z! z0 k) [1 [1 c" \$ t
#include<map>
" D/ @/ h6 a( q* P5 R) _#include<iostream> usingnamespace std; int main()
5 Z/ X [% O% R- r0 @9 g+ l- ~{; s% K% \$ Y- L- y* z( E& p
map <int, int> m1, m2, m3;
- B/ p1 W3 @- `4 c" L9 a) L3 Dmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );
4 \* ^6 L% J; H' Pm1.insert ( pair <int,int> ( 2, 20 ) );' u) e+ |; u! b
m1.insert ( pair <int,int> ( 3, 30 ) );& H8 h7 M- w8 m( ^9 P- @+ J
m2.insert ( pair <int,int> ( 10, 100 ) );; V8 V1 v! N s; z3 r' _
m2.insert ( pair <int,int> ( 20, 200 ) );8 O: T1 S$ C& j k3 v
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
3 e! {8 M* w$ W. G! Vfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
4 s) u$ p' ~4 w1 [" rcout << " "<<m1_Iter->second;: s- t5 \# D5 z; [3 X6 M; t
cout << "."<< endl; // This isthe member function version of swap
1 O3 M+ K% y; A; b" E// m2 is said to be theargument map; m1 the target map' u. Q6 }4 B* l
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
! F' G% V' {: U) Z1 xfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
+ }. B0 c* ^- d' l1 [cout << " "<< m1_Iter ->second;
# ~, o6 u$ b- e9 f4 C6 K; k. F1 acout << "."<< endl;
; r" b+ ?$ |0 w& l3 Q- e) ^( Acout << "After swapping with m2, mapm2 is:";
' S: O& O& z" f, l$ ]for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )- \3 f% E2 U7 l4 }
cout << " "<< m1_Iter ->second;# d1 F3 L% R0 H1 ^* b
cout << "."<< endl;
; _- }: Q! X+ Z6 M# T
// This is the specialized template version of swap/ q& {# J, }5 X2 }7 J+ `
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";. [; O* B; E/ Q) c" v0 g! z0 `! l/ G
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
( ]5 Y$ [& o' j# h" F! icout << " "<< m1_Iter ->second;
6 B+ Q. g+ J# r, _9 D9 b8 L1 v( \cout << "."<< endl;$ t; R0 k# ~% B9 }% v* g$ c- o3 W% W
} 6. map的sort问题:9 z% P& T, L( V \" R( B) c3 x0 l
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
( m+ F6 Z; X5 R% S5 }& A8 QFor example:9 a. A0 d b- G9 x7 T
#include<map>0 c0 e1 }/ [9 l9 x- j$ q
#include<iostream> usingnamespace std; int main( )3 h& x% D) i* c9 ?- l
{
, m' A: y' v5 T% Q% _map<int, int> m1;5 g6 x: E; X' e7 f
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
4 ?3 T- J8 E8 ?7 A8 O' Cm1.insert ( pair<int, int> ( 4, 40) );0 a3 m( z$ H; V$ y3 a" X% h# Q7 A
m1.insert ( pair<int, int> ( 3, 60) );
6 |2 ?" X, g1 A( t% l6 _! E2 Zm1.insert ( pair<int, int> ( 2, 50) );8 D7 c' }$ x0 }1 g" H6 t
m1.insert ( pair<int, int> ( 6, 40) );' u+ R4 z; m* J1 w' V( }' J2 W
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
6 f! T+ o4 J& A- \) d/ J/ q/ {; K$ Nfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
5 b- A4 K: g! c% [' v/ Zcout << m1_Iter->first<<""<<m1_Iter->second<<endl;* |8 Y1 z+ w- X& c
% n6 L6 Q# r( a; x }
}+ a% E: `, V5 a; P$ b# [; S% s
The original map m1 is:
; e: L: s: G3 ]) Z2 M) A7 d1 20
* R- M% ~6 o( n' ^. v) _, ~4 d2 50
+ D4 `1 I- R2 P1 ?: A3 60: T, V! ^6 L4 [ O
4 40
# a& H; c' X( e" M" M# d4 Z0 R& D6 40
( P! ^# p7 c# N# K7 30 7. map的基本操作函数:
5 M3 q& e2 V6 k! d6 {C++Maps 是一种关联式容器,包含“关键字/值”对
* G' J8 a$ e; O4 U/ l& G* kbegin() 返回指向map头部的迭代器
7 U) o! x* c! Eclear() 删除所有元素& d$ J4 P; J# k2 Y+ k8 w
count() 返回指定元素出现的次数5 m! P; `) t/ y+ ~" s
empty() 如果map为空则返回true
' V+ H' f# c/ n" X0 ~) `& g8 {end() 返回指向map末尾的迭代器$ j1 f8 D3 ?) U/ v4 u
equal_range() 返回特殊条目的迭代器对
/ D+ ]# r1 h3 c/ }/ Yerase() 删除一个元素
: x8 q- l% U) g W0 T3 f0 T; dfind() 查找一个元素
( C) O3 E! o3 u; ?- r, @get_allocator() 返回map的配置器
; v0 H" i: b z2 M7 \) L$ cinsert() 插入元素
: s# P5 q5 y# W' ~/ \key_comp() 返回比较元素key的函数
% S {; X. H6 @4 i" [& [lower_bound() 返回键值>=给定元素的第一个位置) a8 s: b! g; M H
max_size() 返回可以容纳的最大元素个数
: [5 g: e9 ?9 y( l, j' Rrbegin() 返回一个指向map尾部的逆向迭代器
) l/ ~ p+ ?/ [. Z0 o0 T: F5 zrend() 返回一个指向map头部的逆向迭代器4 U' _9 B' W; C {- P+ {
size() 返回map中元素的个数- O) ^7 c' {8 z$ y% F; Y; q
swap() 交换两个map# p& W4 R: Z& |$ \/ Q- c
upper_bound() 返回键值>给定元素的第一个位置* R2 ?8 {- m; B6 o0 e+ L! ^
value_comp() 返回比较元素value的函数 3 j. H! ?( l# `2 b+ y
|