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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;
9 A' ]8 X1 a. [% T: Cmap<string ,int>mapstring; map<int,string >mapint;0 f& l! M" x: C: K3 X3 R
map<sring,char>mapstring; map< char ,string>mapchar;
. g) i, l6 I  gmap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;
% n3 ~& f6 r$ c) ~& u  E1 Q1. maplive.insert(pair<int,string>(102,"aclive"));
$ |* f) ~' O4 g2. maplive.insert(map<int,string>::value_type(321,"hai"));
9 G. k' ^  |; G2 \) R; {3. maplive[112]="April";//map中最简单最常用的插入添加!
, g+ q1 h8 Y$ _# U% U0 F+ h

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; 5 Q+ a# p+ [* ?6 l* Y! k; e
l_it=maplive.find(112);//返回的是一个指针
% H( j- ^9 Y4 \. Nif(l_it==maplive.end())) }4 H7 \. W0 N
cout<<"we do not find112"<<endl;/ b( h) A  ^# i, z
elsecout<<"wo find112"<<endl;$ b4 E2 n! U- v* }# F8 F5 F4 L


  A+ e+ _5 h0 q: @

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
: ~$ e8 Y+ W: O3 ]

4. map中元素的删除:
( M' U( [# M7 V( D& I如果删除112;
8 }+ ~! u& r+ I& K; d8 _# R  w1 V9 Hmap<int ,string>::iterator l_it;;. o5 d! Q9 f. {) p7 T, [
l_it =maplive.find(112);) B" t. T0 {$ c. [
if( l_it == maplive.end())9 S& p# e; Y6 \+ J8 ?& D
cout<<"we do not find112"<<endl;
: }5 s+ a4 x& Eelse maplive.erase(l_it);//delete 112;9 \  q* J, I- x7 N

5. map中 swap的用法:
1 ?' o$ i6 p1 O1 ]) q; d# K$ q2 UMap中的swap不是一个容器中的元素交换,而是两个容器交换;( D; p) K+ u8 I! w/ q- C( @
For example:' O" s8 W" a) f7 o/ ^
#include<map>* m3 c; `, f' J! _" O
#include<iostream>

usingnamespace std;

int main(), J* s" I1 Y& S
{
, x, E" Z* s( h2 }& P+ y. {$ s9 `map <int, int> m1, m2, m3;
/ M& s9 @  d; J! y5 q9 xmap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
. z+ M$ @% q+ y2 dm1.insert ( pair <int,int> ( 2, 20 ) );
/ c7 ^3 B9 [' d6 P5 Xm1.insert ( pair <int,int> ( 3, 30 ) );
. x6 B# p7 d4 e5 Dm2.insert ( pair <int,int> ( 10, 100 ) );. r; l# i/ m4 [0 k, C* [
m2.insert ( pair <int,int> ( 20, 200 ) );
0 {/ U& S& k9 ?6 @1 mm3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";( b  l2 ]! T1 J& I
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
5 q% t: L0 l. _cout << " "<<m1_Iter->second;. N) v5 n+ Y' c$ ]1 W5 `
cout << "."<< endl;

// This isthe member function version of swap
0 D% d5 U  k, ]' M" f// m2 is said to be theargument map; m1 the target map
; a4 Q1 g/ m; @$ Jm1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
  B% U" O! T! J6 U3 \for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
+ I5 K2 |. X0 x. M* ^cout << " "<< m1_Iter ->second;
5 `0 h# `7 n4 s$ e3 Jcout << "."<< endl;

; x# H8 o1 ]2 j3 I# P; C
cout << "After swapping with m2, mapm2 is:";
" v! w; }3 E! n" @2 xfor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )$ y  F! A. @3 c, E. B
cout << " "<< m1_Iter ->second;$ e, k( F* v/ B5 N7 Y% F
cout << "."<< endl;

  w- \1 p# i3 i$ V
// This is the specialized template version of swap
0 e9 M0 s% X3 mswap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
% n. F  t! K! R) O% z; G2 wfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
& O) H- n) @, j, K6 m( ecout << " "<< m1_Iter ->second;  i7 j" f, _. `( f0 z. t
cout << "."<< endl;
; t. Q9 ~* i& v5 ~9 k7 M! E) I}

6. map的sort问题:: x( F% i; o5 a% j) u6 W" O
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
% N* H9 P- L' D. q7 cFor example:: |, P, Q) n1 T; _, a
#include<map>
: P- z" k" E5 J. w8 p#include<iostream>

usingnamespace std;

int main( )6 S) A, s3 B& S* v
{
4 c0 J) Y  {9 e1 g( J$ }map<int, int> m1;5 E& {! ^# a) |8 `
map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
; B( v' b% C! [  R( z, O7 ]9 L5 hm1.insert ( pair<int, int> ( 4, 40) );
; ]7 j- U7 W# _, l8 R5 Cm1.insert ( pair<int, int> ( 3, 60) );
6 D# k1 D! `8 o1 e/ P& ^$ F0 s( bm1.insert ( pair<int, int> ( 2, 50) );
: T' I: d- k. \: lm1.insert ( pair<int, int> ( 6, 40) );
; B( j8 J0 P7 Y  R2 wm1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;5 F7 x1 A, K5 v; z2 R
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )( O7 ]; e% |8 f/ R5 `9 U) E
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
0 J/ @7 A9 ?  `/ C: i* X( v1 u+ T6 G1 {4 c+ F2 `6 L
}: u% E% T8 w$ f6 [& I

The original map m1 is:
/ A  b4 h2 n7 i1 20
3 P  ^5 B5 |# q, ]/ V( }2 50/ E: f8 d% \" F9 Q) X
3 600 M/ S4 P6 K2 p- q% X# k
4 40
/ }. l! b! h" a& ]6 40! w2 e6 v4 r$ v, w
7 30

7. map的基本操作函数:0 G$ }+ y. ^( ^" T- t8 X( c/ H
C++Maps 是一种关联式容器,包含“关键字/值”对  y3 Y, e$ w* e1 M  G7 t1 y5 F1 b
begin() 返回指向map头部的迭代器; r2 u. l( }# \7 O
clear() 删除所有元素1 h3 X+ E  ]6 E5 `6 T, z
count() 返回指定元素出现的次数
6 P4 Y( {& V9 F0 d7 n: f  G6 ^empty() 如果map为空则返回true: s& @8 J' P9 ]' D
end() 返回指向map末尾的迭代器. L! |! a! a4 N4 R1 @
equal_range() 返回特殊条目的迭代器对
! r! u$ P1 @+ F; Z0 N1 Ierase() 删除一个元素
3 h3 Z" }& r, ~3 }- D* n7 jfind() 查找一个元素
+ U$ X; E0 E! Z3 n4 A5 aget_allocator() 返回map的配置器
& p  f% f* }; Binsert() 插入元素
$ I# \8 s9 X( w" I. g/ Dkey_comp() 返回比较元素key的函数
) N, G: q. H% p5 z* M. W  rlower_bound() 返回键值>=给定元素的第一个位置
: W% n4 t6 [8 k; Hmax_size() 返回可以容纳的最大元素个数' u; e7 Q8 b. K- w  Z  c
rbegin() 返回一个指向map尾部的逆向迭代器$ h, S$ x" Y- a" y/ v
rend() 返回一个指向map头部的逆向迭代器
/ x" h: B, e# h& `8 csize() 返回map中元素的个数
( B- g5 u& d3 n( N  E9 o9 Rswap() 交换两个map% O) d" n7 A1 H3 G" H' x" C5 P
upper_bound() 返回键值>给定元素的第一个位置
! K; r" @( b8 Q; }# `# b. rvalue_comp() 返回比较元素value的函数

5 j+ p$ T/ F' g8 E! `1 _( h6 [
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了