PLM之家PLMHome-工业软件践行者

[转载电子书] 最全的c++map的用法

[复制链接]

2016-8-29 20:25:18 3608 0

mildcat 发表于 2016-8-29 20:25:18 |阅读模式

mildcat 楼主

2016-8-29 20:25:18

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;, o, i, ~3 y% Y
map<string ,int>mapstring; map<int,string >mapint;. S" Y/ T$ t) S; w+ ?: a9 n# q
map<sring,char>mapstring; map< char ,string>mapchar;% m. G, q  J9 e, s8 s. p; f" o
map<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;; n7 P1 O$ Y% n: q% @# T' z
1. maplive.insert(pair<int,string>(102,"aclive"));
/ W5 R- p5 o9 x+ u0 n* @; E2. maplive.insert(map<int,string>::value_type(321,"hai"));6 o9 e& Q$ d% X! l
3. maplive[112]="April";//map中最简单最常用的插入添加!
3 C( b! L7 Y2 L. f/ e3 ^

3. map中元素的查找:

find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。

map<int ,string >::iteratorl_it;; , ^: D3 f$ S5 W# Q$ C- I. T1 j
l_it=maplive.find(112);//返回的是一个指针5 w4 @# m" T: ^' ~# ^" T
if(l_it==maplive.end()); {% M3 _3 f3 C1 t
cout<<"we do not find112"<<endl;
; h4 V4 U$ S" [elsecout<<"wo find112"<<endl;
( I2 }0 ~) A+ V  k: A% t& X6 n


# a& x: u7 ^5 u  k0 d# x5 a5 T

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;$ o8 V5 I$ n) o& p6 k. n: H; }" S, [

4. map中元素的删除:
  m/ O( m& X% b! V如果删除112;: n7 ?) f0 C0 q( I  J0 S
map<int ,string>::iterator l_it;;
: B8 [% P3 ^0 e( m, Y9 w8 |l_it =maplive.find(112);, S- G( S1 j  p. L0 O
if( l_it == maplive.end())
; M5 }9 L, N0 a# `+ z) i% m" n  J, `cout<<"we do not find112"<<endl;
3 W+ H$ Z# K& H; T2 }* M9 _+ Zelse maplive.erase(l_it);//delete 112;
2 r2 b7 ~; T( P

5. map中 swap的用法:
: P5 D& _( I( Q! G& q. EMap中的swap不是一个容器中的元素交换,而是两个容器交换;# B; I# X" n7 W3 ~: y
For example:
0 z0 B# n% s# d7 D#include<map>% R/ r" C# e* K' z1 q
#include<iostream>

usingnamespace std;

int main(): I# R4 e0 c7 _, B$ R) f/ `' `" L
{
$ S% r2 B9 Y& i5 J, Zmap <int, int> m1, m2, m3;
. [# V6 l/ Y! X( Mmap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
8 ?# ?4 f' P, s9 t" X$ I8 y* pm1.insert ( pair <int,int> ( 2, 20 ) );
% |1 M/ ^9 g) k3 W' ]/ _7 tm1.insert ( pair <int,int> ( 3, 30 ) );; t1 J3 C/ [$ e; `
m2.insert ( pair <int,int> ( 10, 100 ) );8 Q9 M( s& ]0 u4 s1 g3 \
m2.insert ( pair <int,int> ( 20, 200 ) );0 Z1 F" S' b+ D- }
m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";6 J" Z! o; I* Y6 W7 Y% }
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )! p9 Z! w/ z% y; x
cout << " "<<m1_Iter->second;
4 f. L4 k8 o, }! D: P6 M9 T4 E6 I. ocout << "."<< endl;

// This isthe member function version of swap/ X" z8 {: t" `) e' i4 u
// m2 is said to be theargument map; m1 the target map( t2 r8 M/ Z1 [7 e) G# q# `( x
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
; v6 V3 s9 P  K/ W. M& i/ U! Rfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
8 S- v7 T( x. |+ rcout << " "<< m1_Iter ->second;, v  G7 Z5 E/ w/ ^
cout << "."<< endl;


# D' ]7 m, W* O- c! ^/ Ocout << "After swapping with m2, mapm2 is:";& r" n! O; y( R( r8 N: m
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )5 g. f/ N" \1 l4 f
cout << " "<< m1_Iter ->second;
  ~4 g/ C9 N6 Zcout << "."<< endl;


' B. a8 d1 r& c6 _6 G+ W# Z// This is the specialized template version of swap9 d* @% `3 `# Y) o
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
/ N: ]$ k/ q- G- kfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
  N/ M$ Q3 I6 i4 K1 M7 Icout << " "<< m1_Iter ->second;  T9 H1 j$ z+ }
cout << "."<< endl;6 x( V! M  r. |- u; o. k+ g
}

6. map的sort问题:
& j: F+ k+ o  o# ^9 i: T8 DMap中的元素是自动按key升序排序,所以不能对map用sort函数:
5 _/ j" t3 t/ b8 [, e  rFor example:3 M: P. `4 f. U6 x, R- [8 c1 p5 Y
#include<map>
8 ^, t5 j! a" r" \( {3 ]+ R#include<iostream>

usingnamespace std;

int main( )* s, V- |7 p' b- E* h
{- \, ]& U2 b; W6 z2 A- E# V
map<int, int> m1;
: H; c% ?/ j! z) k/ G- q! ymap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
9 r! F" J, ^! ^% D; S. ^' x& i* hm1.insert ( pair<int, int> ( 4, 40) );
$ Q2 _8 N6 h# ~m1.insert ( pair<int, int> ( 3, 60) );  h% X: J0 l1 a
m1.insert ( pair<int, int> ( 2, 50) );
7 \/ h5 ~6 k  q* z3 [m1.insert ( pair<int, int> ( 6, 40) );
7 O7 V: j! j0 ^5 S9 E9 K4 Tm1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
6 X2 Q" P" Y( L* d$ L1 \for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )+ v8 t0 U) H* P7 n$ G/ |; |6 \
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
8 h( f$ \6 ~/ n, Z0 p% h$ B0 D; S7 o2 A; L% W% z2 r
}3 a3 _) X7 }. A! d/ H& ^) a% A

The original map m1 is:
9 C* X$ H$ K% D" ?1 20
: D% f# P5 Y2 t/ r8 }+ l5 `" u8 a2 50% N8 k" R: w2 l4 J
3 60
6 {6 x3 i  w1 a' w' ?4 40
, q7 I! Q0 b, `7 |2 z% Q) D6 40/ R" U7 W7 j3 T, M/ @# K3 |7 t7 V2 }
7 30

7. map的基本操作函数:9 L. g! ]$ m$ n2 l2 N5 m
C++Maps 是一种关联式容器,包含“关键字/值”对
- T" D  h. |- ^# J+ `5 S6 vbegin() 返回指向map头部的迭代器
; [/ B1 F9 x, Y/ k' K# a8 i9 Dclear() 删除所有元素
9 }1 j/ }8 l# Kcount() 返回指定元素出现的次数$ S( f( l. O1 P" u. l8 F
empty() 如果map为空则返回true, j( c3 G( D# z) l& Q
end() 返回指向map末尾的迭代器
. g8 h) i- \; [1 {) l  cequal_range() 返回特殊条目的迭代器对
* b- M. S/ A5 l4 ]  A" w6 Werase() 删除一个元素
7 s( c5 c2 W2 ^2 D6 Lfind() 查找一个元素
) M! t$ ^  w* Mget_allocator() 返回map的配置器
0 m6 l) d' V( o/ winsert() 插入元素9 J  _6 H) D. K/ ?- a& q  N
key_comp() 返回比较元素key的函数
% G  u+ I5 k! ?. ], {2 t2 plower_bound() 返回键值>=给定元素的第一个位置
0 e# @2 R, g5 A3 z6 X4 }max_size() 返回可以容纳的最大元素个数
; R& a; w- p6 H0 E# Z5 drbegin() 返回一个指向map尾部的逆向迭代器
! s+ f( ]1 K( N0 n1 H  V* Zrend() 返回一个指向map头部的逆向迭代器
- d5 ?1 f9 h7 `# O2 E7 ^! x, \) Asize() 返回map中元素的个数9 ~$ }3 {' W2 f; S  i
swap() 交换两个map
/ ?: C: A, Y8 A( ^7 uupper_bound() 返回键值>给定元素的第一个位置4 ?) w/ p, h. ]" n: N3 n
value_comp() 返回比较元素value的函数

8 O2 u$ }  y% e7 ~7 x
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了