请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;/ y3 {6 ~ @& |0 @. Y* a
map<string ,int>mapstring; map<int,string >mapint;/ X! Q( _5 s5 ^+ p, o3 D
map<sring,char>mapstring; map< char ,string>mapchar;
2 L, I# x1 s* z0 jmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;3 A9 O: Y m9 l
1. maplive.insert(pair<int,string>(102,"aclive"));
. D, \& h( p, [2. maplive.insert(map<int,string>::value_type(321,"hai"));
$ c. Y5 x0 L% _2 w7 {% j8 c: K3. maplive[112]="April";//map中最简单最常用的插入添加!
% H. J5 @# c# I \: c5 Q# Y( K 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;; % q# m4 q( @- D$ g; L: I
l_it=maplive.find(112);//返回的是一个指针! \4 N" }4 J+ Q
if(l_it==maplive.end())
5 _- n/ @6 \) [+ Q, W2 Ccout<<"we do not find112"<<endl;
" A- M, P( ^$ d o+ E O6 Qelsecout<<"wo find112"<<endl;) V! J- B- z9 _
! F+ w2 J" `) [) Hmap<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;2 C0 u. |/ M( h* v9 s
4. map中元素的删除:
, K, p2 [* t$ J! D; k如果删除112;$ j# ], n3 P# y K0 G$ k* U
map<int ,string>::iterator l_it;;; ]0 b7 r9 b, w0 Z/ ?# L" R
l_it =maplive.find(112);
; V M- H& c* H6 V% z% |( X$ bif( l_it == maplive.end())
8 j7 J! ~& p5 }) D3 S! O6 [cout<<"we do not find112"<<endl;
4 U) F q3 J, {( F' melse maplive.erase(l_it);//delete 112;
; G- ]( B* E0 x: E* ~ 5. map中 swap的用法:
- ?! i$ G* g/ b0 r9 LMap中的swap不是一个容器中的元素交换,而是两个容器交换;* W: R7 R1 b! @/ I: U$ O, h& B: D$ I
For example:
) D5 u& M/ J2 {5 f#include<map>7 X ~! D" t' [3 k4 q- i9 x1 X
#include<iostream> usingnamespace std; int main()
2 R! g, t" p( f# v9 V7 I{# F+ Z3 B( v2 l9 s" H
map <int, int> m1, m2, m3;
0 _4 U) [! c: h# S8 W0 hmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );! {+ G+ w8 U3 Q8 E
m1.insert ( pair <int,int> ( 2, 20 ) );+ c$ v+ g6 p7 s3 @
m1.insert ( pair <int,int> ( 3, 30 ) );4 `! U9 `1 ^3 n$ w
m2.insert ( pair <int,int> ( 10, 100 ) );5 V! H* O5 Z% P0 L% f
m2.insert ( pair <int,int> ( 20, 200 ) );& U8 ^& Q: r$ [- ?
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
6 i8 N5 C) y$ b# L( m: |4 m+ @4 Q- ^" Vfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )* x$ {! [% r# d% w3 E1 v
cout << " "<<m1_Iter->second;
- b8 y, {: J8 {0 v0 G+ Q5 w0 jcout << "."<< endl; // This isthe member function version of swap& Y# P3 h1 B1 ]* Z4 J
// m2 is said to be theargument map; m1 the target map
7 r/ s: ]5 h8 I, ^m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";& `8 K9 f& v7 R4 P( ?! b3 e
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
: k- j; E2 i4 x' j8 G6 u3 a8 dcout << " "<< m1_Iter ->second;
3 n$ c4 q0 `1 K; |cout << "."<< endl; 4 r% x* Z2 C- u. S' g
cout << "After swapping with m2, mapm2 is:";
6 _: s9 N4 V: M4 s# hfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ ), C; {& ^" Z7 |5 R% @
cout << " "<< m1_Iter ->second;
* E9 ?0 L6 |; z- ecout << "."<< endl; ) S [: J! g# M: B
// This is the specialized template version of swap; ~- l0 R- Y7 w* V4 V
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";9 @/ p$ P5 w- I- K
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )2 d8 g5 K2 D( Q r# S- |
cout << " "<< m1_Iter ->second;
$ y4 A7 t N4 m1 p. r9 T9 `cout << "."<< endl;* }7 k# A0 t9 F, z. I8 X Y) p' r
} 6. map的sort问题:
: D3 B" l( A8 J$ C7 C5 hMap中的元素是自动按key升序排序,所以不能对map用sort函数:
b: a& _4 ?5 z/ \ J1 OFor example:: C$ J, Y" L# F
#include<map>
# N' Z: x/ F2 {! T; k$ Q2 |4 L#include<iostream> usingnamespace std; int main( )% a$ [4 Q% P4 z% R5 D6 D) k
{
% a0 i1 h9 ]- L t/ e4 {map<int, int> m1;! k1 Q2 t$ N- f4 k! v4 _2 b
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );" O$ ~7 @" e" _" L; ]& M/ @* D: [
m1.insert ( pair<int, int> ( 4, 40) );/ u7 O/ M* n) G! |) z2 B( g
m1.insert ( pair<int, int> ( 3, 60) );' ?% V( Y7 X: j; L! O6 G8 V. r
m1.insert ( pair<int, int> ( 2, 50) );
4 o! ?/ H5 v9 X) {: d# @2 Gm1.insert ( pair<int, int> ( 6, 40) );
$ Q7 s/ r, i! m) Wm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
0 m. L$ H1 \, d4 p$ C: Ffor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ ); G+ o; H: u# H- M7 L% b7 G2 s
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
! S. G; G& B: t8 s0 X3 F7 x( u+ F7 g6 U! X2 _0 q
}. A% M6 [6 A" F- }
The original map m1 is:4 M, g" ^' G5 l* v% R. t
1 20
9 q4 ]* v8 {: R! u4 V+ G2 50/ g( ~( k7 f- e, U3 I0 F
3 609 g/ q: b& S; w6 l/ c. l9 F) ]7 a
4 40, ?1 W3 P: p" I* [+ p
6 40& m# W% i8 e( E0 X# {2 \% E- P' ]# b
7 30 7. map的基本操作函数:
2 o& K# g3 \1 r R% z5 B" CC++Maps 是一种关联式容器,包含“关键字/值”对$ V1 r0 D( F! W6 ]9 }- x4 u
begin() 返回指向map头部的迭代器( N/ P1 _' G# n+ `" t8 J
clear() 删除所有元素
% [% F% x8 b6 lcount() 返回指定元素出现的次数: q- V* Q, u' E- a! L
empty() 如果map为空则返回true2 ?6 D. m# p1 @! {4 `) \# j# I; D* n
end() 返回指向map末尾的迭代器9 H& X2 L, q ?* ]$ W. E- @/ \% G4 d
equal_range() 返回特殊条目的迭代器对4 h+ ]* N7 ?9 @# Q E E5 m/ H8 {
erase() 删除一个元素$ `0 _$ U' Y) \
find() 查找一个元素
4 r5 g- W" q3 Z) W( h( Aget_allocator() 返回map的配置器& V [, X6 a3 J/ }4 [
insert() 插入元素, _ B: Q$ N+ C6 n- a: F
key_comp() 返回比较元素key的函数
/ ^" V" r, M- L2 T! G( F+ ylower_bound() 返回键值>=给定元素的第一个位置
- g3 d3 D. J# ~! h" Bmax_size() 返回可以容纳的最大元素个数% u" R/ b- S4 ?! b: d4 W3 K
rbegin() 返回一个指向map尾部的逆向迭代器
+ d: f2 K8 m. l& ?7 X0 S: \rend() 返回一个指向map头部的逆向迭代器4 {( k# s, V# d q' N) @
size() 返回map中元素的个数
9 K) z7 h! j6 n- F/ |# V6 H) gswap() 交换两个map+ a7 O4 E$ n0 ?1 y, g/ _
upper_bound() 返回键值>给定元素的第一个位置" l- _# L5 m' }! [
value_comp() 返回比较元素value的函数 - F' F3 c m7 y$ F
|