请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;
! M# p% ^% S& V+ bmap<string ,int>mapstring; map<int,string >mapint;
6 |; i6 j8 }9 }& amap<sring,char>mapstring; map< char ,string>mapchar;
4 C1 F' }5 r# Q z" rmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
: X- ~9 ~+ Z/ n2 t3 K9 h- a1. maplive.insert(pair<int,string>(102,"aclive"));$ c# ], u; p; U1 h( H
2. maplive.insert(map<int,string>::value_type(321,"hai"));
9 j, }- U+ o+ r+ p3. maplive[112]="April";//map中最简单最常用的插入添加!
) b; z% p8 @5 _; [6 B! \, I 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; " [4 F5 a# v) J) Y2 L2 X
l_it=maplive.find(112);//返回的是一个指针
$ }1 b$ }. l8 ?( Xif(l_it==maplive.end())
! W0 s w" k7 i; j; ?. icout<<"we do not find112"<<endl;
7 i% M( a9 \5 W9 H7 l$ o% J8 ]+ \elsecout<<"wo find112"<<endl;9 X$ @% @6 s2 f1 Y
1 W2 z7 {3 t6 b0 U7 J$ v- fmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
7 f; M, }6 C K) P' T! D 4. map中元素的删除:
5 K9 S" S9 @2 x, i' u7 |5 t如果删除112;# U# Q& a/ n$ F1 [
map<int ,string>::iterator l_it;;
: @& H7 y3 p+ I2 V: `& @l_it =maplive.find(112);& ^/ K8 m0 L! H7 c5 ]8 b
if( l_it == maplive.end())6 _2 n5 e# y5 E% r* V" f
cout<<"we do not find112"<<endl;
' ^$ o1 r9 G1 ^, Relse maplive.erase(l_it);//delete 112;
# S2 k4 n" u% ]$ t# b* [ 5. map中 swap的用法:, E( _7 L; M- T8 x) O6 Y7 w. b
Map中的swap不是一个容器中的元素交换,而是两个容器交换;8 P3 K2 r# m- T& X
For example:
2 T/ N7 [# A/ x3 Y: Q7 D# e* Z#include<map>
* G, t* y8 [1 ^- q/ ]% M#include<iostream> usingnamespace std; int main()3 Z4 B9 v' p! f4 X2 S
{
3 Q) W; O Y8 m0 L3 f7 C* mmap <int, int> m1, m2, m3; X2 d1 i; Q; T( Q
map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );3 f7 {* x5 }/ L' c
m1.insert ( pair <int,int> ( 2, 20 ) );/ i* U* c% A. f3 i9 l" d6 K
m1.insert ( pair <int,int> ( 3, 30 ) );
1 n& _# T1 K: }: J8 G0 R7 y% dm2.insert ( pair <int,int> ( 10, 100 ) );2 X9 j0 B8 X5 A0 S& u& |) o" y
m2.insert ( pair <int,int> ( 20, 200 ) );. {. c4 T0 E7 K3 N2 P' i: |
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
/ j0 L, `0 T/ T7 C' A: kfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
, x3 g) c$ [1 Z# Icout << " "<<m1_Iter->second;4 {# G8 `( U& b4 k
cout << "."<< endl; // This isthe member function version of swap
3 U; ? `6 D. ~; N6 W// m2 is said to be theargument map; m1 the target map: i+ V4 {# [$ ~6 S# v1 `# Q" k
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";
8 {( X: ?, X2 u! } \; Dfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
; w9 i! ~/ L, g( o7 T4 Tcout << " "<< m1_Iter ->second;: \+ h+ l9 p; X$ B/ g+ ~% D1 i
cout << "."<< endl; 2 L/ {( l. G* U1 d4 Y
cout << "After swapping with m2, mapm2 is:";7 F+ o: R7 J0 E! H9 Z
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
. {2 X& @/ v3 W- m) m+ gcout << " "<< m1_Iter ->second;& i+ l: @# H: K" X2 d6 F
cout << "."<< endl; z" }; V/ I7 ]# l
// This is the specialized template version of swap1 _! K6 C; G. _% E3 p
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";* P8 U$ F w, f0 q& z
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
# \! p/ k' }% P3 e+ ?& j8 lcout << " "<< m1_Iter ->second;& O6 f% l/ |1 n2 Z! `' b! r
cout << "."<< endl;7 \! x& s5 n: X/ O% U) |
} 6. map的sort问题:6 I) _& S/ g8 B5 K) h% t
Map中的元素是自动按key升序排序,所以不能对map用sort函数:1 \8 e# M3 C7 n) f, k
For example:: Q( U `2 @; m( w! o ]$ |) P8 C
#include<map>
) M: Z% H# f8 T" W#include<iostream> usingnamespace std; int main( )6 ~/ {( a* N; U$ I9 [: f
{0 ?& {( [1 d+ k$ v8 q. b
map<int, int> m1;
1 X& V$ _8 e5 K5 z1 @5 nmap <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );
) a8 A2 ~* n, a1 om1.insert ( pair<int, int> ( 4, 40) );# I3 c2 L) q: E
m1.insert ( pair<int, int> ( 3, 60) );
9 J. [% d9 a/ Tm1.insert ( pair<int, int> ( 2, 50) );. ^" M2 b. A: O
m1.insert ( pair<int, int> ( 6, 40) );
( k$ [ s" s W' {3 m+ Y" {0 zm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;5 x5 i2 k% j! A& T# v, W+ F( D6 o7 Q
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )8 U- p& e! G( Q! i p5 k, w. m
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;: ?7 t6 W N. \* Y; J# X- D0 [
# [8 Z6 ^7 x- g4 m}# p# P8 c4 |6 J0 W7 G" q8 c! x
The original map m1 is:
- ]) W% v6 l3 U1 K4 J/ L1 20' L6 N* E, H+ S* F/ k8 L
2 50' {6 w3 o+ ~4 ]3 @+ Z* X
3 60' A1 I: E8 Z# q1 ?( d
4 40
* V+ |- ^5 k W6 40: e$ E+ F# O9 m
7 30 7. map的基本操作函数:2 Y6 A; G |7 r5 ]* g* z' {
C++Maps 是一种关联式容器,包含“关键字/值”对* J( ]. B% Y9 ]( t V2 u/ ], s
begin() 返回指向map头部的迭代器7 v) L3 J! n( {" P4 w! a% \6 B( U
clear() 删除所有元素
& c( B4 E$ _$ b }- W$ j" X& @7 V" ecount() 返回指定元素出现的次数
9 ^# ]; P) z) g a' p( L' j- f, cempty() 如果map为空则返回true" H/ m8 I, R: X* b; V
end() 返回指向map末尾的迭代器
' `9 ]7 c0 q! r8 `, Y g* V8 ]9 wequal_range() 返回特殊条目的迭代器对% C# s0 r! w! {2 w$ V+ c
erase() 删除一个元素" |$ y% x: n4 s3 k2 ^5 c1 y- i4 H5 G
find() 查找一个元素
, c/ g- c' ?3 g7 ~5 I9 j3 eget_allocator() 返回map的配置器
. k8 }+ z1 G; W7 sinsert() 插入元素 W( i X2 L/ E1 n, Z8 U
key_comp() 返回比较元素key的函数6 q* y) p. O) H" o4 p* D- Q: ]
lower_bound() 返回键值>=给定元素的第一个位置) w. V" J0 ~) v0 e; E3 X# Z1 a$ d- Z
max_size() 返回可以容纳的最大元素个数/ K, w( U9 h; C6 @# L2 I
rbegin() 返回一个指向map尾部的逆向迭代器5 j) g( `* S; r& ~$ d6 X9 ^& }
rend() 返回一个指向map头部的逆向迭代器, N# w+ u* T9 V a
size() 返回map中元素的个数
( h* B! \+ `1 ?- T7 lswap() 交换两个map/ U& |0 s* v, E1 n) \: p% x
upper_bound() 返回键值>给定元素的第一个位置
4 I, s- q2 S& ~- }/ C) _. p3 lvalue_comp() 返回比较元素value的函数
5 y$ W: d6 `! k& e; \! H |