请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
最全的c++map的用法此文是复制来的0.0 1. map最基本的构造函数;/ z0 l# c$ d1 I0 U
map<string ,int>mapstring; map<int,string >mapint;- t" N1 [ C! o/ G: h
map<sring,char>mapstring; map< char ,string>mapchar;( N) m; f5 v: c* m5 {
map<char,int>mapchar; map<int ,char>mapint; 2. map添加数据; map<int ,string>maplive;3 a. k8 u: b; L( E+ f! ~9 `& k
1. maplive.insert(pair<int,string>(102,"aclive"));
* W5 p' K$ p/ T* j2. maplive.insert(map<int,string>::value_type(321,"hai"));
& M7 N O) ^& L/ k3 z! x3. maplive[112]="April";//map中最简单最常用的插入添加!
7 r8 d7 B2 T/ H% T! A9 {+ I 3. map中元素的查找: find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。 map<int ,string >::iteratorl_it;;
. X c: F6 u6 D) ?" Bl_it=maplive.find(112);//返回的是一个指针) k* h* M$ n( h. n
if(l_it==maplive.end())9 h% r+ B) `+ S4 x6 v% z* e- m/ S( X
cout<<"we do not find112"<<endl;
7 ?6 T9 W9 d7 ?" eelsecout<<"wo find112"<<endl;
( \) R9 j8 n( G6 S8 g0 e3 @' ~' H * s. |: ?* a- {$ m8 E( ~" B
map<string,string>m; if(m[112]=="") cout<<"we do not find112"<<endl;
1 X" p! i: u9 U 4. map中元素的删除:
4 R0 F+ f/ T0 a如果删除112;4 D; d5 H5 W' m+ K
map<int ,string>::iterator l_it;;/ v; H5 I* Z8 a& A' W9 C1 f4 J. c
l_it =maplive.find(112);8 D V6 r2 i9 c; R r
if( l_it == maplive.end())- a8 B& u- X& I
cout<<"we do not find112"<<endl;- p. |+ e% h* D1 r
else maplive.erase(l_it);//delete 112;
- K+ l3 k5 `+ |/ ~% d 5. map中 swap的用法:+ c& m+ C3 d9 n/ X3 l3 N& ^! I
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
* j9 B4 J+ f& X: t: G" B+ {- NFor example:% C* M7 B$ J8 ], V+ e
#include<map>
- T/ O( g: L% B2 P" N" A#include<iostream> usingnamespace std; int main()& {- G$ \# r( y, b1 j
{
, X( R9 F8 h4 ]! i' imap <int, int> m1, m2, m3;
6 a: x6 c( a+ n6 B8 Y1 Z( Qmap <int,int>::iterator m1_Iter; m1.insert( pair <int, int>(1, 10 ) );2 K2 r7 Y+ {" y% u; r
m1.insert ( pair <int,int> ( 2, 20 ) );
1 V( G! F% t+ y7 v" X8 Xm1.insert ( pair <int,int> ( 3, 30 ) );# n3 t3 j$ b: A2 a8 W/ x4 u
m2.insert ( pair <int,int> ( 10, 100 ) );
* v' Z9 l- T& ^8 ~m2.insert ( pair <int,int> ( 20, 200 ) );% i0 `# e: o/ H* c2 c
m3.insert ( pair <int,int> ( 30, 300 ) ); cout << "The original map m1is:";
/ x1 _. k& h; U4 D5 z. U. E; _1 nfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )8 \; J; u, u7 V$ H7 _6 O4 M6 ?
cout << " "<<m1_Iter->second;* v$ @* d. V% H) X
cout << "."<< endl; // This isthe member function version of swap
+ }4 B! R- ^0 d; r+ \" {* Q// m2 is said to be theargument map; m1 the target map) j. l+ A! q" \% v; ~1 q/ F( Q* i
m1.swap( m2); cout << "Afterswapping with m2, map m1 is:";' ^4 y( P* ]* E' |' F1 _ Z
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )- E; |, o$ G* a' }) Z
cout << " "<< m1_Iter ->second;
6 l4 _5 J9 \$ L8 Bcout << "."<< endl; # U* }. h6 N) ` E. v
cout << "After swapping with m2, mapm2 is:";3 k# h& e* v" v5 _7 l
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
# Y/ e F9 f8 |* ^# D# Pcout << " "<< m1_Iter ->second;8 w& v M& Y6 i- J) Y
cout << "."<< endl;
) d) ~0 O2 u+ U* m// This is the specialized template version of swap/ D6 q* \, v2 r, J* e2 u2 J8 P
swap( m1, m3 );
cout << "Afterswapping with m3, map m1 is:";& N; S! L7 A5 w5 j6 U
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )2 q M( T; W4 I3 v/ E+ X
cout << " "<< m1_Iter ->second;& w7 n/ S v9 D# n7 w0 l
cout << "."<< endl;- X2 ~, P" X* X% ?/ l
} 6. map的sort问题:+ ?4 d8 e T6 r( o, ^& n/ _% w
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
0 O+ B; ^/ \" k5 O* K8 A: XFor example:: F+ x0 O5 u& j5 p/ K
#include<map>! a2 @# T) H/ z
#include<iostream> usingnamespace std; int main( )
. u3 \# t- @5 n+ a; N2 |{
3 j# S3 p' g6 a& i- W2 D: E8 u% N9 Rmap<int, int> m1;
1 J, o8 K5 |$ S, X T: K$ w& y" {: C$ [map <int,int>::iterator m1_Iter; m1.insert (pair <int, int> (1, 20 ) );0 S8 D+ d- v* s$ ]8 L
m1.insert ( pair<int, int> ( 4, 40) );9 o3 O9 p9 c' l
m1.insert ( pair<int, int> ( 3, 60) );
+ ]/ H s/ w& N- F6 Wm1.insert ( pair<int, int> ( 2, 50) );
9 }' {* u9 L! v d7 qm1.insert ( pair<int, int> ( 6, 40) );6 o/ s3 g. X, V6 e) A/ ^9 z+ O1 S
m1.insert ( pair<int, int> ( 7, 30) ); cout<< "The original map m1is:"<<endl;
# i' Z( w6 t. Q- i Rfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
! V& C7 s8 O. e+ D0 N8 hcout << m1_Iter->first<<""<<m1_Iter->second<<endl;; H# H9 f9 p( W) b6 O1 W2 m
4 {1 G2 t% i( g7 r" |}: n5 a Y& q% i3 U) o
The original map m1 is:7 F3 X$ v6 x1 V: k" j/ }8 R
1 20" U# a4 h$ A8 H4 e# `& ?* I: J
2 501 z2 a1 D* o/ D# j7 Z& u: p& Q3 `
3 60
1 {! [" t' |! c" V1 ^4 40
5 W/ U8 t4 @- x# K6 j P1 S6 40
7 V w1 s& \! P/ W) ]0 O' d# \7 30 7. map的基本操作函数: `! b3 T' w$ L! t. K+ M/ q
C++Maps 是一种关联式容器,包含“关键字/值”对- ?; z/ N0 l; M" [
begin() 返回指向map头部的迭代器
a8 F/ S$ a3 _/ Y, f' Jclear() 删除所有元素2 l4 F0 u- R- e! c" I- L
count() 返回指定元素出现的次数( B* O7 S7 `2 n
empty() 如果map为空则返回true. J$ z+ Y" @! y p) D" {% G+ o1 h
end() 返回指向map末尾的迭代器4 A. s% R! c2 l' K
equal_range() 返回特殊条目的迭代器对
* C* g4 |* m& `6 c$ serase() 删除一个元素
- J j1 D2 {8 ?7 M6 t, x0 ufind() 查找一个元素
$ c+ C- |6 A# U- U: r2 Yget_allocator() 返回map的配置器
0 j5 j0 u2 ?2 y+ uinsert() 插入元素* o1 U$ p; L6 A3 S7 r1 @2 ^$ f- I
key_comp() 返回比较元素key的函数0 a; c% Z$ I% M, m* C
lower_bound() 返回键值>=给定元素的第一个位置
$ o2 }' G. ~1 e, z( p0 K! Dmax_size() 返回可以容纳的最大元素个数- A; V2 ^) k5 ]) c. A
rbegin() 返回一个指向map尾部的逆向迭代器2 O& V8 h6 _* H, {8 P% ?
rend() 返回一个指向map头部的逆向迭代器
' n X9 A- j/ z* s' z( I; D: N8 Zsize() 返回map中元素的个数, [* l& [* E2 X$ G: q
swap() 交换两个map; Y' `+ }; S1 X8 s* O% s8 P
upper_bound() 返回键值>给定元素的第一个位置
2 R0 m7 @+ J! g6 r* ovalue_comp() 返回比较元素value的函数 ' g+ j' R6 c2 n/ Y1 I8 r
|