PLM之家PLMHome-工业软件与AI结合践行者

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

[复制链接]

2016-8-29 20:25:18 3618 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+ ?0 X  [0 _) B" d
map<string ,int>mapstring; map<int,string >mapint;. {! ?  V# F, [8 b1 d/ d
map<sring,char>mapstring; map< char ,string>mapchar;
8 r* }& l9 F4 E  ?map<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;& P" |0 O3 S/ s$ I; l
1. maplive.insert(pair<int,string>(102,"aclive"));
" o7 @# D9 R" s" M' Q3 ?2. maplive.insert(map<int,string>::value_type(321,"hai"));
/ |7 ]  ~/ ]5 F2 b' C3. maplive[112]="April";//map中最简单最常用的插入添加!9 t  N) B+ v2 H" V; k

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; # [! G' O( P* e" ]. `& @3 B4 k
l_it=maplive.find(112);//返回的是一个指针. a, [0 O! u& _, m9 o
if(l_it==maplive.end())
3 q* l- g  H" g2 K7 Y- Qcout<<"we do not find112"<<endl;
2 E, J2 b! s) Selsecout<<"wo find112"<<endl;+ c, R& r3 n4 I1 X7 ^# h/ k

* y$ ~$ r3 _: B- S3 P

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
+ i% F& H/ D  u8 C8 _: `6 P$ z0 G

4. map中元素的删除:1 A9 o6 |% x3 I+ L( Z; z9 p/ N
如果删除112;0 u. V" V- k3 z: D+ D
map<int ,string>::iterator l_it;;8 Q# o. `) s& \' W  H
l_it =maplive.find(112);
, u" w% ]8 ~' i% ^6 a7 C( K2 C) O) Vif( l_it == maplive.end())( s5 s8 o/ K& e
cout<<"we do not find112"<<endl;8 L: _; c. J- {. B: `% l& }
else maplive.erase(l_it);//delete 112;
; P5 k* H; l) h+ ~

5. map中 swap的用法:* W" n; @% W) i
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
3 b$ ~( N# ?% C, `" {& nFor example:
6 x0 Z0 `/ ]. G! ^, i' _7 Q#include<map>( B3 x+ K8 d+ b4 u$ h: N4 }
#include<iostream>

usingnamespace std;

int main()- V% ~% S; w# w; E1 Q5 H
{9 Y3 S/ `3 ^% O/ `# J# ^5 P) J& y
map <int, int> m1, m2, m3;
6 \. \- M$ B, x( xmap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
" M5 G9 t; _5 N. o! A1 X5 am1.insert ( pair <int,int> ( 2, 20 ) );
- U% @: Z7 d1 L8 X4 t  g' hm1.insert ( pair <int,int> ( 3, 30 ) );
" K" Q& c( Y$ nm2.insert ( pair <int,int> ( 10, 100 ) );% t- K5 K5 Q$ ?0 f5 R
m2.insert ( pair <int,int> ( 20, 200 ) );% G5 |7 m; B; B3 e* n
m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
9 ?6 X2 f+ N  D1 Z5 qfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )# n: B- q# O% y% d% u! I+ K
cout << " "<<m1_Iter->second;
! [" @9 C& H9 f$ N3 Hcout << "."<< endl;

// This isthe member function version of swap. ^; n' s! K  ]) R( u6 t4 \' t3 ?
// m2 is said to be theargument map; m1 the target map" S0 f: p: W( |+ c
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";. N1 o, J5 i+ O3 L7 t
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
& C6 E& U9 l- y; X) vcout << " "<< m1_Iter ->second;7 ?% @& E( A3 ]2 M* n3 m
cout << "."<< endl;


' M2 m% X5 |3 ^* ~' k) {6 kcout << "After swapping with m2, mapm2 is:";
6 S- a5 K0 m1 f: X! Q1 g6 I- Rfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
7 i4 F! c9 r4 T  T( t' G9 Ucout << " "<< m1_Iter ->second;  V. u4 [6 a- K2 r# i9 y9 m
cout << "."<< endl;

0 S0 a+ W. M0 Y5 [8 f$ Z8 z* X/ V
// This is the specialized template version of swap
& L0 i) E9 V. Yswap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
; v1 a% q6 D' q1 p! ?for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
+ m& I+ n9 M; w$ Ccout << " "<< m1_Iter ->second;- l, Q$ j, n' F* T+ a
cout << "."<< endl;; l" ?# w1 z1 I
}

6. map的sort问题:+ _2 l7 P4 R+ T* O$ F
Map中的元素是自动按key升序排序,所以不能对map用sort函数:$ p; y- z/ o' f6 M
For example:
: R! j0 p2 A1 F#include<map>$ D5 H( w0 D; V- X6 A9 i
#include<iostream>

usingnamespace std;

int main( )% A0 c$ V+ K& \! r4 O5 _) @" W& _
{
/ I& q* k* ~3 |" B  p5 J- Qmap<int, int> m1;
  d5 @; f: C, d- j8 pmap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );$ U  c1 U0 j2 x' }' B
m1.insert ( pair<int, int> ( 4, 40) );" B$ h2 k& L. e* m& U4 Z
m1.insert ( pair<int, int> ( 3, 60) );; G' m: }& z, k6 o, S/ b! E7 p
m1.insert ( pair<int, int> ( 2, 50) );
9 [7 ^  j4 h  l- u" r. @m1.insert ( pair<int, int> ( 6, 40) );
. z# I, |0 j: {: Gm1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
/ V: Y! X4 \, Z( Q6 h7 |for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )6 b% P2 ^( n/ z" W
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
: ?8 Q9 W$ [, Z8 ^/ x# s; r% Z0 p- h1 M) i' ~" ^0 h. U. C# }
}6 v7 H' M7 ]# Y8 [. t

The original map m1 is:1 ~: {2 O$ E+ T4 R
1 20$ D( U2 }% ?* i0 A7 u
2 509 T3 h% g% ^' m3 b+ Z* \. M3 y
3 60
% n. m. o% \5 L4 B4 40
3 g; W& T+ f8 B6 40
( V. W; X; ~4 h2 S# y" ~/ W- V7 30

7. map的基本操作函数:1 i3 r5 G3 Y* U4 i4 o# c
C++Maps 是一种关联式容器,包含“关键字/值”对$ A! g# x+ ~8 m2 o% p
begin() 返回指向map头部的迭代器8 J) k, W9 ?  |2 t$ B; {$ B3 E
clear() 删除所有元素
0 Q1 |( B( F' z6 w* d0 g1 @# Gcount() 返回指定元素出现的次数; S3 R) I8 n; B; t; ]! ~7 p
empty() 如果map为空则返回true$ k- S$ l; y% B' e
end() 返回指向map末尾的迭代器9 O- k: \6 t6 ~
equal_range() 返回特殊条目的迭代器对
5 N! H( A- L8 C' Serase() 删除一个元素2 w. }8 F  ?! u3 U3 c6 {
find() 查找一个元素' V! g4 J9 _$ @) l/ I" b
get_allocator() 返回map的配置器
. k4 C* i% `: [, q0 Yinsert() 插入元素
/ k# m+ l  X6 Lkey_comp() 返回比较元素key的函数0 }2 @# @* b6 x! K1 `! \% r
lower_bound() 返回键值>=给定元素的第一个位置3 L% g+ N1 u+ s3 ^+ R7 x
max_size() 返回可以容纳的最大元素个数7 c8 F, Y. U* `
rbegin() 返回一个指向map尾部的逆向迭代器
" c$ e- s+ o# u" f3 ~! qrend() 返回一个指向map头部的逆向迭代器
% Y6 V: [" m2 I5 z$ t1 |size() 返回map中元素的个数
! f- ?* A% {+ z7 U4 O/ Bswap() 交换两个map& B) H, k4 z- }* a4 y, ~% Z
upper_bound() 返回键值>给定元素的第一个位置
/ {/ d# ~- ^6 J# {* ~1 R: Evalue_comp() 返回比较元素value的函数

+ S! l: U- k5 J; }
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了