请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数; A# M: I& J$ P5 V- P' X- ^6 }
map<string ,int>mapstring; map<int,string >mapint;6 C! K, B S; E/ X5 P- {* M
map<sring,char>mapstring; map< char ,string>mapchar;
; F' v- ^; S3 O, H6 `3 ^: tmap<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;
* _' ? ]" H. b% R1. maplive.insert(pair<int,string>(102,"aclive"));0 w% ^/ _; C2 f7 ~8 w0 X
2. maplive.insert(map<int,string>::value_type(321,"hai"));
$ s$ A @* v2 R0 ]/ J8 Q' [' _3. maplive[112]="April";//map中最简单最常用的插入添加!
# @( n% s# u1 n% k 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
$ K* X- X+ u4 P) N$ b0 ol_it=maplive.find(112);//返回的是一个指针* z' _! A+ d% C/ |( r8 K5 X
if(l_it==maplive.end())/ V3 T% x5 |3 i
cout<<"we do not find112"<<endl;
$ ]. O) C; J1 \. S" ~3 helsecout<<"wo find112"<<endl;
0 y0 b% n# {0 ^ , N5 p9 R; t$ W+ l* p
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
" l) w% J' K W/ W9 D 4. map中元素的删除:, ~7 u' v" C. N8 Q
如果删除112;6 I- `8 Y2 X( H0 ^7 X' L+ i
map<int ,string>::iterator l_it;;
+ z, N, r- N6 o0 T& ~0 ]l_it =maplive.find(112); E3 z/ Q1 x- O1 V7 F, O$ H) q
if( l_it == maplive.end())5 ?" v. ~; W4 Z* Y# _
cout<<"we do not find112"<<endl;
! U# v; y' A- i5 y7 x) [! L( Jelse maplive.erase(l_it);//delete 112;
0 Q! @9 ^7 ~7 a' x6 V7 o 5. map中 swap的用法:
* E- F( \ v+ {3 s# t) v# gMap中的swap不是一个容器中的元素交换,而是两个容器交换;
: m4 o- k/ q9 ?1 C( p$ a' {For example:
9 h- V8 T+ k( J% @#include<map>
' d0 p- r; {6 A. n#include<iostream> usingnamespace std; int main()* h) P4 F$ i) p# b) M
{
( |2 y# S3 |( C) m7 w$ |/ gmap <int, int> m1, m2, m3;
! |3 L1 ]( D8 ?# `' @map <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );( A9 l! p4 f" ~) C( u7 W4 T; d
m1.insert ( pair <int,int> ( 2, 20 ) );
% I& T0 h m2 M: E9 J* I6 @m1.insert ( pair <int,int> ( 3, 30 ) );
6 \ h$ q. F% D1 o# k4 j* p' Lm2.insert ( pair <int,int> ( 10, 100 ) );
" \- w6 F3 y; J) H) nm2.insert ( pair <int,int> ( 20, 200 ) );
' |7 X) ^1 G/ @1 N* c' o+ im3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";/ Y6 A+ ?/ ?1 N7 W9 i9 b) @6 G* u
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
q7 _2 N8 A+ a$ Vcout << " "<<m1_Iter->second;
7 m2 G7 ?% R8 t( [2 Y6 Kcout << "."<< endl; // This isthe member function version of swap2 ~* h# x4 @" ]; ]* `& s
// m2 is said to be theargument map; m1 the target map
# r- H. R+ Z' y r2 l# f" ym1.swap( m2); cout << "Afterswapping with m2, map m1 is:";8 @1 E( a1 W( q9 ? N( j* j
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )3 m( k" W, a: b% r' _& h
cout << " "<< m1_Iter ->second;# C0 D" I9 \$ {5 ~) Q% n
cout << "."<< endl; ; |+ G0 P# S) f8 u+ v( c
cout << "After swapping with m2, mapm2 is:";- B" M' o/ y1 b9 T C4 d1 r
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
$ [3 o! o7 x& e U1 K1 W2 xcout << " "<< m1_Iter ->second;
; Q2 L3 G: R% w; r% e% D0 ~cout << "."<< endl; / j0 k" c- {% [* |1 e
// This is the specialized template version of swap; Y. b3 P, B$ Q! r/ S
swap( m1, m3 ); cout << "Afterswapping with m3, map m1 is:";
7 Y) [: X8 E" T6 Nfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
9 b0 w7 j- y1 y+ o7 X2 ]cout << " "<< m1_Iter ->second;/ k) k# \1 b; C2 V5 {
cout << "."<< endl;
. E8 f. K, ?5 T+ m; u} 6. map的sort问题:
* W; i7 ]$ U% DMap中的元素是自动按key升序排序,所以不能对map用sort函数:
" [ S# D0 x+ o+ XFor example:& M: i7 R! N& p, J
#include<map>
8 Q0 [; K+ K$ n! H( O! m y: z) q. S#include<iostream> usingnamespace std; int main( )4 X0 X" w( j3 N% n$ ^
{
8 ?4 H- C! D9 \0 Lmap<int, int> m1;' I0 P" a8 u% v" c( Y' _
map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );) d& x# h6 E( x# l/ _
m1.insert ( pair<int, int> ( 4, 40) );
' p$ ~5 N2 d; V& I) x. G" S+ ym1.insert ( pair<int, int> ( 3, 60) );2 J8 a6 }3 f# h, Z* y- Q" g0 ^
m1.insert ( pair<int, int> ( 2, 50) );# z8 [8 w/ b2 y: `
m1.insert ( pair<int, int> ( 6, 40) );
- `; G1 U7 u6 h; B, [& o3 ~ sm1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;; e" C* H7 o# l* S' F: @5 B
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
" l0 j: J6 y6 D$ p; {cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
2 E3 j$ y9 S5 m9 a' P' U. ^
8 S% P' a+ D* V}
( r4 b7 E9 \) C" z" D5 R8 W! F The original map m1 is:( G8 Y2 k6 I. d! @
1 20
, T- C8 e2 M) {* h v) u2 50' l5 W! v; c# [/ y& {
3 60
7 v0 C& K( _$ H: b4 40* l5 d3 A1 ?6 q2 k0 S1 ]+ c
6 400 f8 j/ D. ]+ S
7 30 7. map的基本操作函数:
. G2 Q+ `4 ? F$ e. oC++Maps 是一种关联式容器,包含“关键字/值”对
: j9 W- B5 O5 x4 T$ M% b5 ubegin() 返回指向map头部的迭代器' g9 d) W. p) [5 P0 f
clear() 删除所有元素
' O: y( ]) N' F- i$ @count() 返回指定元素出现的次数
$ H2 u# d- f0 Z" v( eempty() 如果map为空则返回true8 H* @6 \( I, G
end() 返回指向map末尾的迭代器
7 C' {6 K5 Z) f K6 S# Q* Mequal_range() 返回特殊条目的迭代器对6 w2 ?- A) F5 k8 p, @2 J
erase() 删除一个元素7 M5 m2 K& c' c' x5 d' _% o
find() 查找一个元素
6 _% B# m! n$ j! `5 ^* [9 dget_allocator() 返回map的配置器
5 G: L: b9 R6 O. j$ L- ainsert() 插入元素
( T3 Y! f* R4 c6 p0 ]. b, J: ]key_comp() 返回比较元素key的函数* Q- z& M* O& r; z ~
lower_bound() 返回键值>=给定元素的第一个位置: o8 V( E: u5 U
max_size() 返回可以容纳的最大元素个数
6 l- r8 T2 {- [rbegin() 返回一个指向map尾部的逆向迭代器
9 u. j( E; k( i3 prend() 返回一个指向map头部的逆向迭代器
1 j" N0 e" U# g& W8 T% N$ l4 `size() 返回map中元素的个数( B% ?4 [$ W7 B# {0 ?' ]! H8 J# T1 Q
swap() 交换两个map
8 @2 D* W6 y, m' @upper_bound() 返回键值>给定元素的第一个位置
) j+ N3 s6 A8 U! q7 N7 Fvalue_comp() 返回比较元素value的函数 : i# e; m$ o- x' D
|