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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;/ ?% E' b. H! |; Q$ @7 k4 D7 |- e3 i' M
map<string ,int>mapstring; map<int,string >mapint;& n( t' X* V& X) S6 S1 \5 F! F* A. \: ]
map<sring,char>mapstring; map< char ,string>mapchar;: D8 P8 \! B0 }/ z
map<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;
2 [; v( F$ t" ^, k& d$ ^1. maplive.insert(pair<int,string>(102,"aclive"));
$ A5 c1 S* N% @/ x" d' M2. maplive.insert(map<int,string>::value_type(321,"hai"));/ o9 x3 H) K/ |$ }7 E7 x" W! T; p8 `' t
3. maplive[112]="April";//map中最简单最常用的插入添加!
0 Y% {4 `- G# m7 b: I

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; " w; j' V5 u/ q
l_it=maplive.find(112);//返回的是一个指针% U: O+ N% p; H2 F: X( Q
if(l_it==maplive.end())
1 a( t: P8 p4 n' Hcout<<"we do not find112"<<endl;
' [/ m& T( Z# J4 e1 V" H4 ^+ ~elsecout<<"wo find112"<<endl;  B7 @  j6 P( ?0 C' f


  o+ D. I( J7 F# q% M! n5 T- m

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
% f( j% b# i4 i" d: c0 B* S

4. map中元素的删除:
- l5 F3 @$ O$ F! {如果删除112;' K0 x1 x7 F- s+ P, u3 O
map<int ,string>::iterator l_it;;
$ u" O2 B+ q4 s) W6 R/ u% W7 e% V0 zl_it =maplive.find(112);4 N9 `; z( b6 k) g$ @0 `1 _: O- p
if( l_it == maplive.end())# V8 a) z. R* _+ `2 Y6 U
cout<<"we do not find112"<<endl;
9 [1 l- c5 Q; f4 m( eelse maplive.erase(l_it);//delete 112;
4 [2 U9 M$ N; a3 g

5. map中 swap的用法:4 K0 I, Z! [( M! b# M6 e8 P+ T
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
. |4 ~: W5 l  r  R2 C9 B# ?For example:
9 ]+ D& g" v  h' s& ]9 S8 U- ^#include<map>3 H& l$ ^+ W" w8 n; h0 Q
#include<iostream>

usingnamespace std;

int main()  I( H) F( u. k
{+ S- J) l( |' ^+ v
map <int, int> m1, m2, m3;
! |, }4 k! z6 f3 O" smap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
" H3 j, {1 _9 I  G/ n* s$ v3 Jm1.insert ( pair <int,int> ( 2, 20 ) );) @$ f' X$ t/ a$ I0 u
m1.insert ( pair <int,int> ( 3, 30 ) );
/ u; ?' f% t& T2 m' B' f8 {m2.insert ( pair <int,int> ( 10, 100 ) );) O3 P- [5 {( Z( m
m2.insert ( pair <int,int> ( 20, 200 ) );  c, z" J, i) t) E3 H; Y
m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
; r7 E8 }$ }; j, e# h0 h( y0 M( ffor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
' ^# @& c' s( L3 D, x; }% t' Qcout << " "<<m1_Iter->second;! O7 ]( m2 B" ^0 E
cout << "."<< endl;

// This isthe member function version of swap
) L, b8 v6 S# V// m2 is said to be theargument map; m1 the target map: P9 n  L3 p5 j9 K* w
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
4 O. A+ R9 |( ]4 F5 p% j+ bfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
5 u2 i2 V( |" z  W0 Ccout << " "<< m1_Iter ->second;
; `" O  z2 e# }& Z( L+ M) S! Ncout << "."<< endl;


0 ^; ]7 c. r4 K1 @! D5 ccout << "After swapping with m2, mapm2 is:";1 S# ?. j7 Y- X  r8 I5 E
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
; v4 a* l; p/ p3 [7 Ccout << " "<< m1_Iter ->second;8 y2 o: K3 q' ]. i4 t' Y# R1 ~0 q
cout << "."<< endl;

5 j; V: Y( E3 C' O# T
// This is the specialized template version of swap+ b8 D2 P$ O. k" ?3 Z) \
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";9 q4 F8 j. i* D$ [% T: P& ~9 K
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
" q. W+ {$ ]5 y) Q$ `cout << " "<< m1_Iter ->second;& V0 {  O: a6 M
cout << "."<< endl;- K4 X# _4 b2 S0 d9 @& U2 S8 y9 Q2 }
}

6. map的sort问题:$ ^# e7 B  t0 v0 L
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
. D5 `( Z9 U, W) V8 Q. X' VFor example:, A0 X: w, G) A9 e2 O) t8 R' M
#include<map>8 O& g5 B/ `3 ~  `  A7 V7 I2 k
#include<iostream>

usingnamespace std;

int main( )
( z9 y- x$ h) S9 a{- f, O& I: M5 w+ b* H8 e. n
map<int, int> m1;1 H5 ~, h* N. \! v8 N4 ]  U' x
map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );9 @( C- y4 U  |+ N3 r
m1.insert ( pair<int, int> ( 4, 40) );
0 k: t- c: f/ m8 x. Ym1.insert ( pair<int, int> ( 3, 60) );
  s9 c9 Z5 }: y# x1 a0 E0 Gm1.insert ( pair<int, int> ( 2, 50) );
! j( |8 X4 s" a+ f# U( cm1.insert ( pair<int, int> ( 6, 40) );
1 a7 T- ^4 m8 C1 J7 gm1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;2 J% r: N3 w( h
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )% b% e) u* o3 k6 d4 r
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
! \8 j/ D& s* C- F) c: [) Q9 K9 ]/ w% m; H3 m& n
}
" `9 f6 ~9 @$ @# w

The original map m1 is:+ V  W9 @2 U- Z* c2 x
1 202 B- L* t  Z7 E9 k
2 50
# X7 i# w; H  b% n* V7 @3 60
- C$ T# [9 e1 M- u4 O% m4 40
3 b8 S1 `$ i: r+ O3 i; b& {7 c" d3 S6 40
* e1 L0 \; O8 g' \$ l3 G7 30

7. map的基本操作函数:9 @# A- G9 L5 V" |/ x
C++Maps 是一种关联式容器,包含“关键字/值”对# ^' [- `/ `& y
begin() 返回指向map头部的迭代器/ h+ `1 ^' A' Y; C
clear() 删除所有元素
1 ?6 e" H5 [  T3 }/ ^* y# r  H3 Zcount() 返回指定元素出现的次数  D/ w# A) e; c7 J
empty() 如果map为空则返回true
* }) _4 J3 J, `. h+ q( i% Cend() 返回指向map末尾的迭代器5 s' p" N8 M- [6 ^4 Q7 a
equal_range() 返回特殊条目的迭代器对
6 C  U7 v: X! verase() 删除一个元素- M- V: s+ ~7 o" T# B* ~9 I6 y
find() 查找一个元素
( C' U- ~; f  \  t+ \. Y  Iget_allocator() 返回map的配置器
. S5 m) b2 a5 ~: e0 E# {7 G; U% pinsert() 插入元素
* C3 T6 q+ Y9 u$ E7 gkey_comp() 返回比较元素key的函数
" @; [8 {% c' o' ylower_bound() 返回键值>=给定元素的第一个位置
( R/ v4 n/ h3 I4 {) S" wmax_size() 返回可以容纳的最大元素个数
' k8 S0 R7 L# j9 drbegin() 返回一个指向map尾部的逆向迭代器
; f; c+ O( f0 I" b% Urend() 返回一个指向map头部的逆向迭代器
" x7 e0 N  g9 Ksize() 返回map中元素的个数8 V3 [6 r  e+ |! R0 _$ k
swap() 交换两个map
, M. Z: w+ x; pupper_bound() 返回键值>给定元素的第一个位置9 r8 L( t1 _* |& Z* L1 T
value_comp() 返回比较元素value的函数

& u* K- e" U* K$ G' u
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了