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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;* _. C( N2 b, c2 e+ y
map<string ,int>mapstring; map<int,string >mapint;8 i4 }1 {1 U& w8 H
map<sring,char>mapstring; map< char ,string>mapchar;
7 q7 c4 |( R$ A/ R7 J7 B% mmap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;1 u. n! Y) W; K
1. maplive.insert(pair<int,string>(102,"aclive"));' R4 `  g. {% B1 @8 Y: T# L$ ]/ V
2. maplive.insert(map<int,string>::value_type(321,"hai"));
% ~& t$ [: ~+ r; O# ?$ m3. maplive[112]="April";//map中最简单最常用的插入添加!
0 A) {6 Q" S# w; O, k

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;;
' Q7 G7 S8 n4 y. f0 J4 L4 hl_it=maplive.find(112);//返回的是一个指针
; ?! d7 w$ u; b4 ^$ h- C$ u. kif(l_it==maplive.end())5 S3 j, M9 I' `5 q" C4 f. |
cout<<"we do not find112"<<endl;
: g) a( Q7 ?& r4 Pelsecout<<"wo find112"<<endl;$ v! V( {; @& T3 ]  ]% d

; \" O# ~) M: b! U; k

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;' l; O$ E' x5 b5 H4 x1 d5 |

4. map中元素的删除:
- l  X( }0 ?' @9 d如果删除112;
3 b6 ]" [$ L- @& h* vmap<int ,string>::iterator l_it;;
% g2 A" f# g9 El_it =maplive.find(112);
) ?4 n( q2 k9 D+ x, M, P; Bif( l_it == maplive.end())
* B+ t, E9 s' Ycout<<"we do not find112"<<endl;% i( \2 X! q! X: X
else maplive.erase(l_it);//delete 112;
, q$ P7 u! `( a7 k+ Y- J

5. map中 swap的用法:
1 f, N3 P2 b0 ~, [4 YMap中的swap不是一个容器中的元素交换,而是两个容器交换;
/ u8 ^6 g! O( M8 o6 [. _  e9 a; qFor example:
9 P, ]7 Q& W! k+ H1 l5 A% Z* R; \#include<map>3 X6 f& o7 v5 t  [; J* c
#include<iostream>

usingnamespace std;

int main()0 ~; @) ~& v; D: v
{
& p6 `6 ]) a( w$ d; Q7 k* z5 O) hmap <int, int> m1, m2, m3;
: o! ~9 a$ p2 t/ T# Q7 Xmap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
8 P- l6 @# e/ B! Om1.insert ( pair <int,int> ( 2, 20 ) );
  H* [& a% ]( K" W; I9 _' W4 i4 Om1.insert ( pair <int,int> ( 3, 30 ) );
! g; ]3 [: w" y7 Q% {) t8 Jm2.insert ( pair <int,int> ( 10, 100 ) );
( }5 W3 |) d/ e: ]; x% Fm2.insert ( pair <int,int> ( 20, 200 ) );9 X% O# i# ~/ e6 C  C1 p- z
m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
; N) [+ e  ]; Ffor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
, v/ L: y3 g4 |4 ^& x$ gcout << " "<<m1_Iter->second;$ ~9 Y9 q$ `3 |0 P! D
cout << "."<< endl;

// This isthe member function version of swap
0 L3 ^( @8 F  q: d; |+ l% B4 K// m2 is said to be theargument map; m1 the target map
2 ^( P7 {7 c6 p! r9 U  cm1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";  X9 g  G9 \1 Z/ y7 _1 J% D9 W
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )* E7 V  }) @6 W9 }- M2 @( K
cout << " "<< m1_Iter ->second;# b% t% U* |" o3 u8 g3 ?
cout << "."<< endl;

' V: W. B$ ~( @8 l, P5 j- w
cout << "After swapping with m2, mapm2 is:";
: o1 U5 W6 U% n: |& mfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )! L) ~) }8 X) W% K4 b
cout << " "<< m1_Iter ->second;8 ?0 H7 u3 e3 L
cout << "."<< endl;

/ q2 e5 k9 c5 S8 x
// This is the specialized template version of swap7 }; Z% r7 A# n" ~' _1 _4 A
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
/ L% p4 B1 \; I" Q: M& ]for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )0 ]! q- F+ W$ Q$ W0 C# m
cout << " "<< m1_Iter ->second;. |, [# m# ^; c1 Y
cout << "."<< endl;+ D4 w/ {+ X7 y8 t0 |9 Z. Z
}

6. map的sort问题:
9 T0 f2 `1 F* C' e8 Y8 n% bMap中的元素是自动按key升序排序,所以不能对map用sort函数:, m% \5 K1 j. v5 M3 ]8 l
For example:
( I3 t& `$ J' ]#include<map>9 Z# H0 o. s' j6 s0 D
#include<iostream>

usingnamespace std;

int main( )- k  a; ]' t  T" u; A9 E
{7 d8 I: u% S' s* i8 r
map<int, int> m1;
1 h, m% a9 Q6 x. o4 ^& emap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );+ D6 w8 M2 [; _2 y: L0 f
m1.insert ( pair<int, int> ( 4, 40) );
  C/ M/ X' X& c: J2 Bm1.insert ( pair<int, int> ( 3, 60) );
- k6 T5 X* E+ Y$ V) f- q( \m1.insert ( pair<int, int> ( 2, 50) );
) N1 p+ {; @% c2 y% hm1.insert ( pair<int, int> ( 6, 40) );6 M0 x9 S* \1 w2 P
m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
- |. g# q! g6 a" X# D' efor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )  r0 t- @+ k: }/ x9 h
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
: z' W' U) y" L) `/ N0 b
2 W5 ]- }! i1 }. T}
; h/ I& D* W3 _, f

The original map m1 is:4 X, w7 L* r. F
1 20
! M5 L. W6 R! W! H9 U2 50, W1 [! Q# |+ k! }
3 60  _/ a* ]$ T* z( }
4 407 n3 {" F2 |) r# ]
6 40% a' M: h9 u9 `
7 30

7. map的基本操作函数:
  R+ c) H- ?+ {3 W# RC++Maps 是一种关联式容器,包含“关键字/值”对
% ?3 _: [+ b: nbegin() 返回指向map头部的迭代器
% `. Q- G1 p2 Q! q4 d9 G/ P3 `clear() 删除所有元素
( c8 u# t9 m+ |! r( d& Bcount() 返回指定元素出现的次数. V: R4 q* I+ l  x
empty() 如果map为空则返回true6 o" U8 P9 h, E1 l) A) `
end() 返回指向map末尾的迭代器
/ o% W7 L$ E5 Wequal_range() 返回特殊条目的迭代器对
* \8 S1 m4 Y+ D! z5 C  Ierase() 删除一个元素
0 w9 ?/ b* M4 u& u* j+ w2 Wfind() 查找一个元素4 \, Z3 x% A' }# K
get_allocator() 返回map的配置器
/ v- S8 i3 Y0 Finsert() 插入元素, t# s- x" h7 c6 p
key_comp() 返回比较元素key的函数' F8 J2 `3 w6 a. r5 ]
lower_bound() 返回键值>=给定元素的第一个位置+ g0 F4 |& s/ O) q) q0 r
max_size() 返回可以容纳的最大元素个数
' M3 s. L6 Z1 E2 _' P; Drbegin() 返回一个指向map尾部的逆向迭代器
* H9 n+ ?5 j1 k. Krend() 返回一个指向map头部的逆向迭代器
) F0 C) x7 x3 j0 w% csize() 返回map中元素的个数5 r. o' _, d# x/ s5 S# f
swap() 交换两个map
5 O) C: _, q( m, v; F- \upper_bound() 返回键值>给定元素的第一个位置
& e+ |' N5 e$ L& }( bvalue_comp() 返回比较元素value的函数


1 W3 l/ [  r8 O1 m* f' o( o! y
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了