PLM之家精品课程培训,联系电话:18301858168 QQ: 939801026

  • NX二次开培训

    NX二次开培训

    适合初级入门或想深入了解二次开发的工程师,本培训结合ufun,NXOpen C++,大量的实例及官方内部的开发技术对于老鸟也值得借鉴!.

    NX CAM二次开发培训报名 NX二次开发基础培训报名
  • PLM之家Catia CAA二次开发培训

    Catia二次开发培训

    Catia二次开发的市场大,这方面开发人才少,难度大。所以只要你掌握了开发,那么潜力巨大,随着时间的积累,你必将有所用武之地!

  • PLM之Teamcenter最佳学习方案

    Teamcenter培训

    用户应用基础培训,管理员基础培训,管理员高级培训,二次开发培训应有尽有,只要你感兴趣肯学习,专业多年经验大师级打造!

  • PLM之Tecnomatix制造领域培训

    Tecnomatix培训

    想了解制造领域数字化吗?想了解工厂,生产线设计吗?数字化双胞胎,工业4.0吗?我们的课程虚位以待!

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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;2 I6 |- r# `$ u- t! B
map<string ,int>mapstring; map<int,string >mapint;
4 g# Y# M% C$ ]- dmap<sring,char>mapstring; map< char ,string>mapchar;
6 n9 |: P' f7 \8 o& {map<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;
! r. w; ]: h! [3 H1. maplive.insert(pair<int,string>(102,"aclive"));
, s1 Y" X9 R2 a3 h% r7 j* D4 v2. maplive.insert(map<int,string>::value_type(321,"hai"));
* v) c$ e7 k6 k" M* w' q3. maplive[112]="April";//map中最简单最常用的插入添加!$ t: ?2 p+ l: h8 D3 L3 B: b* z

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; ' A( |- O; ~; o$ [( J
l_it=maplive.find(112);//返回的是一个指针
9 {5 {1 Z9 Y. h( u0 N" C0 H" H9 Zif(l_it==maplive.end())0 s+ P/ |8 O  G7 O$ m2 Y
cout<<"we do not find112"<<endl;4 R! g7 r8 S/ g- ~7 d, j9 p1 \
elsecout<<"wo find112"<<endl;2 Z! v2 O) _* y& J! B0 I* s

; Y! O/ ]% C, ?4 j. |( h3 g2 Y

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
, b, N9 n" F) v  }: V9 x0 E

4. map中元素的删除:( K/ X) F) [4 c4 n9 R# d2 @
如果删除112;/ \1 b7 ~; V# [% Z" l& t
map<int ,string>::iterator l_it;;/ X( k5 x: E" G
l_it =maplive.find(112);
9 @8 O0 e  l8 r) f6 }if( l_it == maplive.end())
' J7 j& R7 g5 ]  L- y6 X1 @* xcout<<"we do not find112"<<endl;
% ^' A, c" h' W; X: H1 velse maplive.erase(l_it);//delete 112;
; C( l" K5 D: R

5. map中 swap的用法:
( L3 n" E4 v" z/ ^. XMap中的swap不是一个容器中的元素交换,而是两个容器交换;0 r# @* ]. c3 m
For example:  ?- |( {: a& \6 B3 J
#include<map>; S9 `# u' h) W9 s0 r3 N$ O
#include<iostream>

usingnamespace std;

int main()
2 m# r. J2 a# [{
& w$ H" g6 C3 b" D. ^% A/ jmap <int, int> m1, m2, m3;
, i8 i& }& H: U. l4 dmap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
" q2 c$ M, ^5 V1 m; N$ wm1.insert ( pair <int,int> ( 2, 20 ) );
  c2 @; T+ j+ i8 q- um1.insert ( pair <int,int> ( 3, 30 ) );& o$ h% B& y3 R/ l3 z
m2.insert ( pair <int,int> ( 10, 100 ) );
6 o( p1 G- |& H3 w) Km2.insert ( pair <int,int> ( 20, 200 ) );
+ F. s" ?. o. Y4 i; V1 Y1 Tm3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
8 d" x: m& y( Q" o- Jfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )1 ^' K( s3 |9 ?& ?8 h& K
cout << " "<<m1_Iter->second;
% U% e4 f/ R, \! }8 O1 p! icout << "."<< endl;

// This isthe member function version of swap
7 V/ s- D+ F( l// m2 is said to be theargument map; m1 the target map0 \5 [& ~  ?' D  |) W% m
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";- z; B# `4 s& w
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
4 |; X  ?5 F  i! ]* bcout << " "<< m1_Iter ->second;, W7 J. u2 P9 P/ M8 M5 a% y! @
cout << "."<< endl;

/ Z7 g* M; b- |/ }: L; K7 W  |2 \
cout << "After swapping with m2, mapm2 is:";; I0 N& ]1 r8 l" f5 D, Y. H
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )# Z2 R. O6 r* }$ M* A# R. k
cout << " "<< m1_Iter ->second;
0 S7 D5 F' I' q" Jcout << "."<< endl;

& b. ~0 H' ^3 D9 q5 ^/ K
// This is the specialized template version of swap# P9 e" H9 E1 Y1 k# n
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";- A; r' \! B1 [  a
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
. s( @: }- C+ w0 l6 G, t5 h; h6 n; Ncout << " "<< m1_Iter ->second;
( p8 N, a* V7 X" xcout << "."<< endl;$ ~' U8 z7 @2 e4 V+ l
}

6. map的sort问题:
  t8 Q' @1 k  K2 GMap中的元素是自动按key升序排序,所以不能对map用sort函数:
8 t; j, ~8 l4 r) j$ Q: h* ]& lFor example:
- j' H' a5 E3 D7 A$ r#include<map>
& I/ N1 m% P5 m; G& m* g  ^#include<iostream>

usingnamespace std;

int main( )
3 P; t0 k6 A; M6 s9 x' z/ H{
/ S* G" m/ j" Q- Z: Z1 l2 i1 }map<int, int> m1;8 ^, h# o5 }$ ~! U3 i6 Y3 P
map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
7 g# C% ]* b' `# nm1.insert ( pair<int, int> ( 4, 40) );$ h+ F" y. e  D& x1 g' y5 ]
m1.insert ( pair<int, int> ( 3, 60) );
7 e" ?% \  i2 b3 em1.insert ( pair<int, int> ( 2, 50) );
( C6 L4 h* H0 g1 \m1.insert ( pair<int, int> ( 6, 40) );, u/ L( C$ k4 W2 S7 w& A
m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
3 }) u2 [2 g8 I: ?4 Z! o; xfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ ): E% H( e" s- U/ I: \% ~9 n
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
# T7 d( {8 V6 c: ^' o5 _& C* u3 {" J1 O/ W
}: s0 H5 e/ i. H1 N9 v, l  L8 f. @

The original map m1 is:
! j* D' R, C- i5 f0 O+ g% W1 20) _% B. v3 P$ t
2 50  ]/ W8 [* I6 \: ]6 r% e
3 60
1 P) B7 T& d# b& V, ?! s, z" j4 40
* k' i! l) b- J6 40) X) E/ C; A: z9 c* c& z9 B
7 30

7. map的基本操作函数:4 D0 ]9 W# O0 Y/ ]
C++Maps 是一种关联式容器,包含“关键字/值”对# W0 O+ a! _1 `+ }4 o5 I) r8 E
begin() 返回指向map头部的迭代器
/ U& |2 d8 w  `& nclear() 删除所有元素
1 f/ E+ c) L5 g3 vcount() 返回指定元素出现的次数4 o5 N2 V( K$ ~- l
empty() 如果map为空则返回true# I. v/ z% n  c5 t+ T2 A$ u
end() 返回指向map末尾的迭代器" k9 S% `  A" t+ s5 N4 n
equal_range() 返回特殊条目的迭代器对
) l/ X0 L4 l- N" |: X/ c/ c; `erase() 删除一个元素. P6 h. x  I5 y& P* I
find() 查找一个元素
+ ^( j! y+ I, E/ z0 e  yget_allocator() 返回map的配置器
3 p! r; H5 I/ p- _insert() 插入元素
# y. c5 C2 i3 {2 Ekey_comp() 返回比较元素key的函数$ o9 l' t' s- K. f
lower_bound() 返回键值>=给定元素的第一个位置
6 i8 s  M1 y9 n% Omax_size() 返回可以容纳的最大元素个数
# Y: X! h& O0 l6 z' E5 z. V5 crbegin() 返回一个指向map尾部的逆向迭代器
. i( P) U. Z# I* }* p7 U; J3 Drend() 返回一个指向map头部的逆向迭代器* R1 X& {4 C8 D
size() 返回map中元素的个数
+ [& W- a2 T; a% x; N/ F! eswap() 交换两个map9 L5 c5 c0 i5 p8 y3 v+ z  H' a
upper_bound() 返回键值>给定元素的第一个位置
$ l; ^. h$ D# a, `/ Gvalue_comp() 返回比较元素value的函数


4 s. V7 P! @1 O3 f- c( |6 e
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了