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 3158 0

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;
! M# p% ^% S& V+ bmap<string ,int>mapstring; map<int,string >mapint;
6 |; i6 j8 }9 }& amap<sring,char>mapstring; map< char ,string>mapchar;
4 C1 F' }5 r# Q  z" rmap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;
: X- ~9 ~+ Z/ n2 t3 K9 h- a1. maplive.insert(pair<int,string>(102,"aclive"));$ c# ], u; p; U1 h( H
2. maplive.insert(map<int,string>::value_type(321,"hai"));
9 j, }- U+ o+ r+ p3. maplive[112]="April";//map中最简单最常用的插入添加!
) b; z% p8 @5 _; [6 B! \, I

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; " [4 F5 a# v) J) Y2 L2 X
l_it=maplive.find(112);//返回的是一个指针
$ }1 b$ }. l8 ?( Xif(l_it==maplive.end())
! W0 s  w" k7 i; j; ?. icout<<"we do not find112"<<endl;
7 i% M( a9 \5 W9 H7 l$ o% J8 ]+ \elsecout<<"wo find112"<<endl;9 X$ @% @6 s2 f1 Y


1 W2 z7 {3 t6 b0 U7 J$ v- f

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
7 f; M, }6 C  K) P' T! D

4. map中元素的删除:
5 K9 S" S9 @2 x, i' u7 |5 t如果删除112;# U# Q& a/ n$ F1 [
map<int ,string>::iterator l_it;;
: @& H7 y3 p+ I2 V: `& @l_it =maplive.find(112);& ^/ K8 m0 L! H7 c5 ]8 b
if( l_it == maplive.end())6 _2 n5 e# y5 E% r* V" f
cout<<"we do not find112"<<endl;
' ^$ o1 r9 G1 ^, Relse maplive.erase(l_it);//delete 112;
# S2 k4 n" u% ]$ t# b* [

5. map中 swap的用法:, E( _7 L; M- T8 x) O6 Y7 w. b
Map中的swap不是一个容器中的元素交换,而是两个容器交换;8 P3 K2 r# m- T& X
For example:
2 T/ N7 [# A/ x3 Y: Q7 D# e* Z#include<map>
* G, t* y8 [1 ^- q/ ]% M#include<iostream>

usingnamespace std;

int main()3 Z4 B9 v' p! f4 X2 S
{
3 Q) W; O  Y8 m0 L3 f7 C* mmap <int, int> m1, m2, m3;  X2 d1 i; Q; T( Q
map <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );3 f7 {* x5 }/ L' c
m1.insert ( pair <int,int> ( 2, 20 ) );/ i* U* c% A. f3 i9 l" d6 K
m1.insert ( pair <int,int> ( 3, 30 ) );
1 n& _# T1 K: }: J8 G0 R7 y% dm2.insert ( pair <int,int> ( 10, 100 ) );2 X9 j0 B8 X5 A0 S& u& |) o" y
m2.insert ( pair <int,int> ( 20, 200 ) );. {. c4 T0 E7 K3 N2 P' i: |
m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
/ j0 L, `0 T/ T7 C' A: kfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
, x3 g) c$ [1 Z# Icout << " "<<m1_Iter->second;4 {# G8 `( U& b4 k
cout << "."<< endl;

// This isthe member function version of swap
3 U; ?  `6 D. ~; N6 W// m2 is said to be theargument map; m1 the target map: i+ V4 {# [$ ~6 S# v1 `# Q" k
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
8 {( X: ?, X2 u! }  \; Dfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
; w9 i! ~/ L, g( o7 T4 Tcout << " "<< m1_Iter ->second;: \+ h+ l9 p; X$ B/ g+ ~% D1 i
cout << "."<< endl;

2 L/ {( l. G* U1 d4 Y
cout << "After swapping with m2, mapm2 is:";7 F+ o: R7 J0 E! H9 Z
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
. {2 X& @/ v3 W- m) m+ gcout << " "<< m1_Iter ->second;& i+ l: @# H: K" X2 d6 F
cout << "."<< endl;

  z" }; V/ I7 ]# l
// This is the specialized template version of swap1 _! K6 C; G. _% E3 p
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";* P8 U$ F  w, f0 q& z
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
# \! p/ k' }% P3 e+ ?& j8 lcout << " "<< m1_Iter ->second;& O6 f% l/ |1 n2 Z! `' b! r
cout << "."<< endl;7 \! x& s5 n: X/ O% U) |
}

6. map的sort问题:6 I) _& S/ g8 B5 K) h% t
Map中的元素是自动按key升序排序,所以不能对map用sort函数:1 \8 e# M3 C7 n) f, k
For example:: Q( U  `2 @; m( w! o  ]$ |) P8 C
#include<map>
) M: Z% H# f8 T" W#include<iostream>

usingnamespace std;

int main( )6 ~/ {( a* N; U$ I9 [: f
{0 ?& {( [1 d+ k$ v8 q. b
map<int, int> m1;
1 X& V$ _8 e5 K5 z1 @5 nmap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
) a8 A2 ~* n, a1 om1.insert ( pair<int, int> ( 4, 40) );# I3 c2 L) q: E
m1.insert ( pair<int, int> ( 3, 60) );
9 J. [% d9 a/ Tm1.insert ( pair<int, int> ( 2, 50) );. ^" M2 b. A: O
m1.insert ( pair<int, int> ( 6, 40) );
( k$ [  s" s  W' {3 m+ Y" {0 zm1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;5 x5 i2 k% j! A& T# v, W+ F( D6 o7 Q
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )8 U- p& e! G( Q! i  p5 k, w. m
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;: ?7 t6 W  N. \* Y; J# X- D0 [

# [8 Z6 ^7 x- g4 m}# p# P8 c4 |6 J0 W7 G" q8 c! x

The original map m1 is:
- ]) W% v6 l3 U1 K4 J/ L1 20' L6 N* E, H+ S* F/ k8 L
2 50' {6 w3 o+ ~4 ]3 @+ Z* X
3 60' A1 I: E8 Z# q1 ?( d
4 40
* V+ |- ^5 k  W6 40: e$ E+ F# O9 m
7 30

7. map的基本操作函数:2 Y6 A; G  |7 r5 ]* g* z' {
C++Maps 是一种关联式容器,包含“关键字/值”对* J( ]. B% Y9 ]( t  V2 u/ ], s
begin() 返回指向map头部的迭代器7 v) L3 J! n( {" P4 w! a% \6 B( U
clear() 删除所有元素
& c( B4 E$ _$ b  }- W$ j" X& @7 V" ecount() 返回指定元素出现的次数
9 ^# ]; P) z) g  a' p( L' j- f, cempty() 如果map为空则返回true" H/ m8 I, R: X* b; V
end() 返回指向map末尾的迭代器
' `9 ]7 c0 q! r8 `, Y  g* V8 ]9 wequal_range() 返回特殊条目的迭代器对% C# s0 r! w! {2 w$ V+ c
erase() 删除一个元素" |$ y% x: n4 s3 k2 ^5 c1 y- i4 H5 G
find() 查找一个元素
, c/ g- c' ?3 g7 ~5 I9 j3 eget_allocator() 返回map的配置器
. k8 }+ z1 G; W7 sinsert() 插入元素  W( i  X2 L/ E1 n, Z8 U
key_comp() 返回比较元素key的函数6 q* y) p. O) H" o4 p* D- Q: ]
lower_bound() 返回键值>=给定元素的第一个位置) w. V" J0 ~) v0 e; E3 X# Z1 a$ d- Z
max_size() 返回可以容纳的最大元素个数/ K, w( U9 h; C6 @# L2 I
rbegin() 返回一个指向map尾部的逆向迭代器5 j) g( `* S; r& ~$ d6 X9 ^& }
rend() 返回一个指向map头部的逆向迭代器, N# w+ u* T9 V  a
size() 返回map中元素的个数
( h* B! \+ `1 ?- T7 lswap() 交换两个map/ U& |0 s* v, E1 n) \: p% x
upper_bound() 返回键值>给定元素的第一个位置
4 I, s- q2 S& ~- }/ C) _. p3 lvalue_comp() 返回比较元素value的函数


5 y$ W: d6 `! k& e; \! H
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了