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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;
* A' n/ ]0 H: c* y! T4 m" N  Z3 X0 Nmap<string ,int>mapstring; map<int,string >mapint;2 K) N. C5 p2 F; D% x; \
map<sring,char>mapstring; map< char ,string>mapchar;
. w% i% p3 f7 ~map<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;; i. s" q- b* q7 t1 [3 B
1. maplive.insert(pair<int,string>(102,"aclive"));, ~: Q/ Y' r0 N3 n3 n+ b- S
2. maplive.insert(map<int,string>::value_type(321,"hai"));
4 }' a4 p+ S8 b9 S2 E; V% @; Y3. maplive[112]="April";//map中最简单最常用的插入添加!
7 B/ q, }% y! f

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; " ~8 Y/ c6 _4 c
l_it=maplive.find(112);//返回的是一个指针* `1 j$ @5 I8 ^" `- B$ `
if(l_it==maplive.end())  u  h) F8 r  `2 ]7 C
cout<<"we do not find112"<<endl;
- V# ]6 D( M: M+ relsecout<<"wo find112"<<endl;' Y' C) [5 R& m& [


- u; G- {% f7 V- o& A0 W& Q& ^0 Z

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
; ~1 x- R& j5 _7 p6 `, S, \$ Z) f

4. map中元素的删除:
$ L" o: l$ ~* C3 B9 S5 A如果删除112;
: m0 D; d0 W2 D" e$ M- p' Q5 Q+ f" Cmap<int ,string>::iterator l_it;;& L2 Y/ t2 ?  `: v! W( g- W
l_it =maplive.find(112);! v4 }' s: C+ H% [8 R/ @/ M
if( l_it == maplive.end())
" I9 u$ v  P& ]4 Z- ncout<<"we do not find112"<<endl;, V7 i3 P; I5 O7 h, h- f
else maplive.erase(l_it);//delete 112;' W% M5 N: V5 m! r; @

5. map中 swap的用法:; s2 T# n  ~) D) x' a8 w5 R
Map中的swap不是一个容器中的元素交换,而是两个容器交换;8 ?3 c& n6 L: E  Q# f' S7 q" P
For example:
/ w( l# Q4 E, }' c- Y# m#include<map>4 O. h, }6 F5 f
#include<iostream>

usingnamespace std;

int main()* c" p. q( v3 N
{" R/ u" z+ h5 y; S- V* z9 `
map <int, int> m1, m2, m3;3 R, k( _  w; [% j! Y1 p. _5 Q% t
map <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
; N' ]8 o% X7 vm1.insert ( pair <int,int> ( 2, 20 ) );8 H' u, \: L! j9 n1 c. ~; d8 u; ^
m1.insert ( pair <int,int> ( 3, 30 ) );( i* i1 P  u; t7 T# t
m2.insert ( pair <int,int> ( 10, 100 ) );2 |7 H+ `5 ~3 u* P% q, o
m2.insert ( pair <int,int> ( 20, 200 ) );
3 C/ b2 y: C+ B: n& d+ km3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
; M9 g) `- t: {# |  Q) v) \for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
% e$ H7 z' t- n; w2 mcout << " "<<m1_Iter->second;
: i' S1 t" A: f2 J+ M$ Qcout << "."<< endl;

// This isthe member function version of swap
. O* t+ w9 L& n2 z: Y8 M// m2 is said to be theargument map; m1 the target map
7 b9 S6 p" A6 xm1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
# h! O/ J' e, {* @, m9 Q, h. Tfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
) U+ N1 L5 h: o' P6 x+ t. P5 _$ ~cout << " "<< m1_Iter ->second;& ~# n$ G! p, R! M
cout << "."<< endl;

* ~8 W2 k1 h- f7 @- A; U
cout << "After swapping with m2, mapm2 is:";
' T0 @. j" Q6 H3 u' ]' e! ifor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )3 D. o9 }7 T7 J& a
cout << " "<< m1_Iter ->second;
* z5 y  T' R6 T" |( [3 \8 Rcout << "."<< endl;

5 c, m: I7 t$ x" Y
// This is the specialized template version of swap
. \: O# {( D' y' b' h# }, Bswap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";1 @4 Z- v+ R: A$ b* i7 Z+ ]4 [
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ ). a8 V, A& M6 R+ q4 \0 U6 G
cout << " "<< m1_Iter ->second;! k) E, ^" W, I0 V
cout << "."<< endl;; u* r& [! T, w# z/ I, x
}

6. map的sort问题:1 j5 L- p- V$ P  S( ~
Map中的元素是自动按key升序排序,所以不能对map用sort函数:7 Y: B6 J5 x6 O
For example:1 B1 {+ T% t) x( ?6 [
#include<map>
: o8 `$ Y7 M6 Z' W5 v#include<iostream>

usingnamespace std;

int main( )( D% [4 o' ]- `
{
7 d+ P. q+ r/ S" `6 d, \' J5 g$ wmap<int, int> m1;
3 t' E/ M3 c/ H, I* s/ amap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
. Z3 U' c9 D8 p9 X( fm1.insert ( pair<int, int> ( 4, 40) );' `2 _7 s( e1 u) x4 ^4 v
m1.insert ( pair<int, int> ( 3, 60) );
; _9 r  Y$ n+ A* nm1.insert ( pair<int, int> ( 2, 50) );0 S4 S( B- E. V7 I- U$ O
m1.insert ( pair<int, int> ( 6, 40) );) R: h8 U6 r7 E, v+ t
m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
# ]; Y7 P8 o0 S* U* hfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
; r) f, y9 z# Y( }% F) t% W( Qcout << m1_Iter->first<<""<<m1_Iter->second<<endl;
8 W( K; a' w3 D/ R* D6 R; N# M6 [& v: v
}: J% h6 ?3 G; {% i- |

The original map m1 is:+ `9 ~* D1 I: F7 i
1 20
; M+ h; O! U5 T: X2 50; u9 ^. d0 ?% l1 u) r4 T; J9 e9 b5 S
3 60: c) g. U4 ?& g5 t, a  [
4 40+ ~+ T5 r0 N' g7 x
6 40
9 F* j; y# a. R# F7 30

7. map的基本操作函数:+ Y' h5 G( B+ y, Z% o2 m+ U
C++Maps 是一种关联式容器,包含“关键字/值”对
4 q1 p( y; a/ E/ H- Rbegin() 返回指向map头部的迭代器: z& r) p* C* M+ P6 n. H" t
clear() 删除所有元素  ~) v5 G+ a5 k" e
count() 返回指定元素出现的次数( X. F" z. S4 `% p% v' t
empty() 如果map为空则返回true
( D$ T5 ^) I  Rend() 返回指向map末尾的迭代器3 w4 d- [0 H0 g
equal_range() 返回特殊条目的迭代器对$ p7 c2 a+ [. y, S) q9 S9 H+ m$ T
erase() 删除一个元素% \9 j, X7 }# T  T$ C. ?- Z
find() 查找一个元素
# Q4 V; `: s& M! c' I$ O8 f8 \get_allocator() 返回map的配置器/ X* x6 x; T8 h) [, [( ~1 H
insert() 插入元素7 a- E7 y& J7 e, Q! e: b
key_comp() 返回比较元素key的函数) g/ K+ a* J" A! F6 n
lower_bound() 返回键值>=给定元素的第一个位置) k" f. M- L. V& [
max_size() 返回可以容纳的最大元素个数
$ k% ?# H" G: [* n& V1 Vrbegin() 返回一个指向map尾部的逆向迭代器. X! e' L, t3 o$ `
rend() 返回一个指向map头部的逆向迭代器
4 R$ p3 V3 t" N6 R" Fsize() 返回map中元素的个数
) r  p* n5 w3 ]" n6 X3 R1 xswap() 交换两个map
1 H: G' V* j; ?4 V: c, ~& y2 wupper_bound() 返回键值>给定元素的第一个位置
  X' r6 }4 ~( }5 Fvalue_comp() 返回比较元素value的函数


1 Y) D' F) Q% |9 {
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了