PLM之家PLMHome-国产软件践行者

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;
& \, d0 K% d( n% r8 S" h7 umap<string ,int>mapstring; map<int,string >mapint;
+ I$ S% f! i3 E$ W) ^map<sring,char>mapstring; map< char ,string>mapchar;
, c$ q9 V+ g6 f8 J* w9 Umap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;9 {7 |/ G0 \) ]) |3 Z, d+ R
1. maplive.insert(pair<int,string>(102,"aclive"));6 V( J& W. r' t' G# i9 D8 A4 b0 ^
2. maplive.insert(map<int,string>::value_type(321,"hai"));
# H$ X  e( R# A8 l3. maplive[112]="April";//map中最简单最常用的插入添加!4 p+ C% n# `" O6 \6 N

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;;
4 K# u' W9 T/ M. B% fl_it=maplive.find(112);//返回的是一个指针
  h7 T+ f# h% U" y# lif(l_it==maplive.end())' |: z1 F; Q5 q4 J) v" Z
cout<<"we do not find112"<<endl;; {1 I. m1 j  ?
elsecout<<"wo find112"<<endl;# |) {" q3 \' h3 C& o


' I' Z9 @& q" l0 D  S: i

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;5 D: v. u* s$ u+ f# a! U& d3 L

4. map中元素的删除:
# }; j0 a# h9 F& o" w如果删除112;& [' V8 x$ q/ f) K: A
map<int ,string>::iterator l_it;;
+ a9 ~; i- z! E) n! O) `l_it =maplive.find(112);
; s6 O  ?3 E$ {, ]8 ?& c: kif( l_it == maplive.end())- O" i0 e6 ?; P; P# Z* i
cout<<"we do not find112"<<endl;
( l* t, u# V% Uelse maplive.erase(l_it);//delete 112;" }' g5 }  j9 Q

5. map中 swap的用法:, k; n. @& ?8 p) d* ]: q5 L
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
8 Y5 s* V) U& _1 `For example:- \  Q6 G8 n, {* S. x) v
#include<map>
$ g' r. a, T: C$ A2 S9 |# |- e) Z#include<iostream>

usingnamespace std;

int main()) ], t3 w4 t6 |) L# D0 @. p; W6 P
{
' u! s; \1 V* K  l; H6 Imap <int, int> m1, m2, m3;
: d& y9 s3 ?: d( e8 Ymap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
8 y9 @1 n" C  r- W' Ym1.insert ( pair <int,int> ( 2, 20 ) );
9 F( ?3 c9 z% Z3 Em1.insert ( pair <int,int> ( 3, 30 ) );
# x3 }: w/ ~" Q0 b! b2 u# U6 rm2.insert ( pair <int,int> ( 10, 100 ) );  o5 Q, G5 N( _
m2.insert ( pair <int,int> ( 20, 200 ) );
# X7 u+ D& r+ M/ [$ b, r) P! Xm3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
! b* s' |1 M3 L8 x6 R6 {7 u. O/ Ofor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
' |: K' P: `3 i! b/ |; Acout << " "<<m1_Iter->second;. C/ S# p4 \- }9 x
cout << "."<< endl;

// This isthe member function version of swap
+ V) s% @5 z! a0 A' ?4 h// m2 is said to be theargument map; m1 the target map& R. ?( h+ m( z+ \; U9 c8 G6 o
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";3 f) b2 d+ Y" z- I
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )9 u9 |) X# I* Y5 T7 q
cout << " "<< m1_Iter ->second;4 ?6 f! w# @5 ~$ I* `
cout << "."<< endl;

: t4 d3 z4 J: E. V( O2 Y. P
cout << "After swapping with m2, mapm2 is:";; e- \; C# f( I4 q
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )8 Q) c& R3 T, G& V$ U  l7 c
cout << " "<< m1_Iter ->second;8 `0 `; J& n+ ?
cout << "."<< endl;


: A' S9 C% n: Y: I! r// This is the specialized template version of swap
3 O8 G2 o. K% }! F: n# ]swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
5 v: {& M: G5 D9 {+ gfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
1 c. s( t1 K  Z; a0 n/ }5 {cout << " "<< m1_Iter ->second;
+ o" [" j* z$ B# Scout << "."<< endl;
+ k; \7 E2 z6 I) x; ~}

6. map的sort问题:- B) D6 e2 w- G4 X: ^' r8 E
Map中的元素是自动按key升序排序,所以不能对map用sort函数:" m8 |4 U7 S5 k& C
For example:
& x# b9 }7 c& Q: p8 _4 ~#include<map>6 U" f! a+ N, [5 V8 M% J
#include<iostream>

usingnamespace std;

int main( )" H& w; H* @8 W2 |
{5 j3 t2 d7 A) z0 |( D  j
map<int, int> m1;' k+ |, X0 R# E4 `8 d6 A; Q' j
map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
, n# M" s( p7 a5 Y. Mm1.insert ( pair<int, int> ( 4, 40) );5 |- u* ^1 b1 T  H+ w
m1.insert ( pair<int, int> ( 3, 60) );
" F% @5 |+ R$ Lm1.insert ( pair<int, int> ( 2, 50) );
- T( G; J( i1 [) o& y9 K( Om1.insert ( pair<int, int> ( 6, 40) );! a7 `$ Q' M4 p$ c0 q" R
m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
" p& W( s; F& Gfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
3 s/ R3 R0 g  f) J. [cout << m1_Iter->first<<""<<m1_Iter->second<<endl;2 R6 f: H% O- F4 F# z

. {9 {  e' ~, I}
& l, D, S: f3 F4 e( y/ K' K

The original map m1 is:4 V2 r- a5 s4 H( L4 [3 o! t
1 20
: H" }# V; j' }( w( k- m2 505 j6 B- e; P3 e
3 60
8 k, e# T( i- X" |4 40
2 }  k" f! a. E6 40
9 V6 ~  q. w5 r; N2 {' I7 30

7. map的基本操作函数:
- j7 @. m' ^: a$ LC++Maps 是一种关联式容器,包含“关键字/值”对
9 K' j4 y# ]3 E, G  ]begin() 返回指向map头部的迭代器
6 K% d4 Y) W3 w# Pclear() 删除所有元素
* v' t2 Y; b0 D8 F- s) m" I* Gcount() 返回指定元素出现的次数7 A  h! Y$ M( _4 D) Z) Z
empty() 如果map为空则返回true; F8 X+ F8 u; @, G; }# I/ U0 t
end() 返回指向map末尾的迭代器% J- U/ b5 ~$ _  g- W7 D. [- K
equal_range() 返回特殊条目的迭代器对
7 u2 r, c: I/ R6 \0 b, werase() 删除一个元素2 x8 p. ^" d/ W( A( P+ O5 J
find() 查找一个元素
$ R; J( Y9 x7 Y! k7 J1 G( Oget_allocator() 返回map的配置器
/ Y* _" C/ e' u: M! F7 ?; Q0 d2 Xinsert() 插入元素
: g, s/ |& g! F; s9 d5 Q" Vkey_comp() 返回比较元素key的函数
& v& @5 c3 ]5 C/ i. A, e" D3 |/ Ilower_bound() 返回键值>=给定元素的第一个位置9 e; ]8 G5 y0 [3 u& z( ~
max_size() 返回可以容纳的最大元素个数
" E/ s# Y6 Y/ i! _rbegin() 返回一个指向map尾部的逆向迭代器
4 h2 _) a; @. l& f1 krend() 返回一个指向map头部的逆向迭代器
( c) g. D& K% t" msize() 返回map中元素的个数
5 p2 b, |0 b0 M) c+ B2 ~0 rswap() 交换两个map
0 ]! M2 ?! a+ e6 ^upper_bound() 返回键值>给定元素的第一个位置
0 y+ w9 A+ H. |( Lvalue_comp() 返回比较元素value的函数


* j4 }1 [2 W. B4 T% y
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了