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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;
% ]( @; w& B3 O. _* @map<string ,int>mapstring; map<int,string >mapint;
5 r# }" \9 w8 A, L7 Fmap<sring,char>mapstring; map< char ,string>mapchar;
- {& [9 G6 ]# @) Imap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;
" y4 F7 N( F# I1. maplive.insert(pair<int,string>(102,"aclive"));
6 C6 c( C! z' D/ u5 Q! U8 H2 C2. maplive.insert(map<int,string>::value_type(321,"hai"));8 C; i" ~& O: c+ _: L% r4 w: Z
3. maplive[112]="April";//map中最简单最常用的插入添加!0 Z" V' c4 S* W; D

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; 2 M, T! F" E( P0 ]+ A4 j
l_it=maplive.find(112);//返回的是一个指针7 v  G! D9 i* D& U
if(l_it==maplive.end())
1 \5 @% U- l; [, m+ Bcout<<"we do not find112"<<endl;* `0 z( H9 V) G" t2 _* Y
elsecout<<"wo find112"<<endl;
. t6 ?! A# E0 ^' g: J" r, ]


/ ?) R, U7 ~! y& L8 M

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
/ y6 ]4 f# |7 ^, ^4 W3 A

4. map中元素的删除:" c* G$ e8 }' {/ v
如果删除112;4 I% v6 p% L' S- E" \
map<int ,string>::iterator l_it;;
' a( e/ d8 Y9 D( _/ il_it =maplive.find(112);/ }( S! O; K* }# i& v6 e
if( l_it == maplive.end())
4 m- Q5 F* V9 Y; C# P9 |cout<<"we do not find112"<<endl;
3 l7 Q# q( Y! H/ u% belse maplive.erase(l_it);//delete 112;
/ M5 x! i4 x5 S5 H; {

5. map中 swap的用法:8 Y) G, I, \: N8 y9 f0 w/ ?& p0 V
Map中的swap不是一个容器中的元素交换,而是两个容器交换;/ }4 N$ f9 D* v& J) t
For example:
% |" [, x* z0 {0 ?3 S/ @+ T% H: A$ g#include<map>
5 f& U" m1 X3 C9 y#include<iostream>

usingnamespace std;

int main()
& {2 x- M6 S6 {{
: }- Y  Q1 @4 a3 j" x& }2 H. pmap <int, int> m1, m2, m3;
8 Y* [/ n* V* W1 k+ ^/ G' _& Xmap <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
1 s* p" N" T, i# Y, r4 mm1.insert ( pair <int,int> ( 2, 20 ) );
! t0 U$ b! d5 c/ x: e  Am1.insert ( pair <int,int> ( 3, 30 ) );! ^, a# f1 H9 R2 m5 d, p5 {
m2.insert ( pair <int,int> ( 10, 100 ) );
3 G, L+ L! i4 u$ U5 A' Xm2.insert ( pair <int,int> ( 20, 200 ) );
# C* N4 Y4 b, M9 x$ A, cm3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";) I. Q& V5 @& `/ X. I# Y
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
# q# E% j7 ?3 A% H" g/ G7 U3 i5 Qcout << " "<<m1_Iter->second;
( I: `" \7 ?5 P4 g% G) L% p5 Fcout << "."<< endl;

// This isthe member function version of swap$ K+ j4 ~! C! j; o) Y
// m2 is said to be theargument map; m1 the target map
3 N! A2 p4 d/ I' rm1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
: E+ s. `+ w6 q; h% t. x4 bfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
* l' P0 Y! \/ P; ccout << " "<< m1_Iter ->second;
5 d" Y# i) w1 [+ f" e0 y  M! tcout << "."<< endl;

# p# d! O8 e0 d; `
cout << "After swapping with m2, mapm2 is:";' h1 z' U, y& ?
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ ). D( M4 x' Y& S7 C/ w
cout << " "<< m1_Iter ->second;
; |$ x/ ^: [3 u4 vcout << "."<< endl;


/ m: T; \5 ]0 A4 E8 d/ h// This is the specialized template version of swap
! }" K/ h1 j1 I7 K9 T& c& jswap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
1 g1 N8 W( ]- hfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )
) L' M- f" o" N0 Ycout << " "<< m1_Iter ->second;
2 o% Z' F2 \0 Icout << "."<< endl;
# F/ k  ]7 |4 I# P* J}

6. map的sort问题:
  q: |# T5 M. f' Q6 n- ^2 g! aMap中的元素是自动按key升序排序,所以不能对map用sort函数:' P+ A: Y( Z! e" ?2 N
For example:7 O2 T- o( ]. z2 g
#include<map>
3 ^1 `' M7 i3 Y) H#include<iostream>

usingnamespace std;

int main( )! o% T4 [1 e, K* C8 d/ ^
{
! |/ X* ?3 o! z) M# u) Qmap<int, int> m1;
/ C1 D# m. s. \+ g* d  _map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );  L$ p) `& J( E0 N9 g5 p4 \' Q# c
m1.insert ( pair<int, int> ( 4, 40) );
/ r5 P! x8 U% |8 w6 h4 Zm1.insert ( pair<int, int> ( 3, 60) );
3 V5 j3 N' F3 m. b' rm1.insert ( pair<int, int> ( 2, 50) );
9 \9 x3 J+ X0 X. Zm1.insert ( pair<int, int> ( 6, 40) );2 J& P7 Y- [, G, H2 Y
m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
4 t0 m2 e, w: C' ?; A$ z& u! Zfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
5 J  c9 ~- w- a# |& Pcout << m1_Iter->first<<""<<m1_Iter->second<<endl;6 G7 o7 S* V: O& x; X6 |5 k
! M$ l3 `  V1 G8 t7 r4 `  f6 d4 s
}
) [% O0 F6 B9 e' _% T( n1 x1 ?

The original map m1 is:
& B: w. L# O! c) q. L1 201 V! E+ B( s/ l7 I$ f6 T- j
2 50
- c4 b1 Z! N" t! _3 60
/ O' f2 l( @1 `9 L4 404 V5 }7 c% [  O4 V+ w
6 40
& L3 q2 s; J: f# e+ ]7 30

7. map的基本操作函数:
' B3 O$ w' Y3 m8 w+ H& W; k& \2 UC++Maps 是一种关联式容器,包含“关键字/值”对
: c4 B5 a1 G5 _$ H5 X9 Ebegin() 返回指向map头部的迭代器
9 m/ S4 R; U9 ]% I6 sclear() 删除所有元素
. I' W/ t; e- T; F0 x- Q, S; ccount() 返回指定元素出现的次数4 n/ v, W' ^4 N; V9 i7 h
empty() 如果map为空则返回true
: c3 h! c& w5 y8 |# S/ c% u% |end() 返回指向map末尾的迭代器/ w: x- Q4 F* \! k  r$ D
equal_range() 返回特殊条目的迭代器对4 W8 ~4 S0 a# b, o$ i; L+ L
erase() 删除一个元素1 K( e; p7 V, K+ l4 S  \
find() 查找一个元素
. _8 L1 \% f* e) D: v9 H* d& pget_allocator() 返回map的配置器
! D$ B5 J9 M3 Einsert() 插入元素* x4 p. S- S! d& V7 ?# M
key_comp() 返回比较元素key的函数
0 w! x  F2 t7 [8 h+ M" tlower_bound() 返回键值>=给定元素的第一个位置
) u. N4 T3 ?2 _+ X3 w8 ]max_size() 返回可以容纳的最大元素个数
& i- w  ~) t3 }+ j- P& brbegin() 返回一个指向map尾部的逆向迭代器; C* S& N3 u3 a5 u/ I- m; |! u
rend() 返回一个指向map头部的逆向迭代器
  L# \1 p6 r9 Y* `9 |7 zsize() 返回map中元素的个数
4 P0 H6 n! ~4 {6 rswap() 交换两个map5 a  l7 s- g4 ^) P  d
upper_bound() 返回键值>给定元素的第一个位置
0 R2 Q, q9 a' ^7 n' l0 Gvalue_comp() 返回比较元素value的函数


& x/ ^% }  R- J" d; m2 ]# x4 D" A3 K) O
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了