PLM之家PLMHome-工业软件践行者

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;) E0 _- }/ u2 g! }$ p- L
map<string ,int>mapstring; map<int,string >mapint;. K4 Z6 v' T; ~) q
map<sring,char>mapstring; map< char ,string>mapchar;
& y$ }/ g6 ?1 V- H2 s. L7 Qmap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;
& V6 z/ `; ^/ V0 B0 T  X1. maplive.insert(pair<int,string>(102,"aclive"));
9 J1 D2 Q1 y# d& k% l. h! K( k4 V2. maplive.insert(map<int,string>::value_type(321,"hai"));" f, U- t- z/ Y4 z0 W1 q
3. maplive[112]="April";//map中最简单最常用的插入添加!
0 a/ a/ Y4 D, z

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;;
) Y2 n% r4 U# ?/ Y9 _9 K4 sl_it=maplive.find(112);//返回的是一个指针
; L5 H% q2 n1 ]# ]; S  Y; Nif(l_it==maplive.end())
2 ?: s% X* `. R9 ^. N# T1 `( _6 k; ~cout<<"we do not find112"<<endl;3 D- z* X0 @/ Q) N! W: e
elsecout<<"wo find112"<<endl;
" r8 P, S4 {$ i) e) L2 C

8 f& d7 ^. i5 L. z2 v/ B0 {6 M. N

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;8 W: E/ z1 n& Q; H+ E& E4 ?* F

4. map中元素的删除:
( c) ]8 A& u/ [; z如果删除112;
/ v; E7 }$ l8 y% q0 Ymap<int ,string>::iterator l_it;;
7 U( R& [7 Z2 I3 ll_it =maplive.find(112);
4 {( P8 I* Z' b2 C5 r# d8 Q1 [if( l_it == maplive.end())
; U3 V* p! ]8 Bcout<<"we do not find112"<<endl;1 s7 R# A$ o6 T6 E
else maplive.erase(l_it);//delete 112;
! F) ~0 I$ k7 g9 @2 c( z$ h

5. map中 swap的用法:
- W, E; T: L. C: c& n* m! P# hMap中的swap不是一个容器中的元素交换,而是两个容器交换;
. N1 C; X1 S9 d7 \For example:
2 O6 D7 w$ U' S4 {#include<map>2 |  H+ t0 W( W. e" Y
#include<iostream>

usingnamespace std;

int main()( r# ?" T- u3 Q/ O2 j
{8 e$ }) n8 q; Y
map <int, int> m1, m2, m3;* e2 ?5 K7 r3 h1 S
map <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );+ F0 j" m$ c5 d- v& K
m1.insert ( pair <int,int> ( 2, 20 ) );2 {7 l( b1 K+ _; u4 F
m1.insert ( pair <int,int> ( 3, 30 ) );
+ A2 G' u, s& b# l4 g8 Um2.insert ( pair <int,int> ( 10, 100 ) );& s7 [, S8 l4 `2 P" S7 ~. v
m2.insert ( pair <int,int> ( 20, 200 ) );
6 q: w$ I1 ~- q1 x; [/ e$ Am3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";
, d) c' z  ]! t2 C; O" x6 c- Zfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
" ~, l+ X+ R) Pcout << " "<<m1_Iter->second;
" U2 u/ ?; m8 [4 D1 [. gcout << "."<< endl;

// This isthe member function version of swap
3 i: c1 H4 H* ~9 \/ F& @7 I// m2 is said to be theargument map; m1 the target map" B4 E+ [/ G7 H: C: _: _. Y% z5 D
m1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";0 B/ Q0 n' L* }+ x
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )
, U% _9 y/ j: f- W2 vcout << " "<< m1_Iter ->second;/ N3 }# g% x2 E; s4 J9 G; W* A9 T
cout << "."<< endl;

! o0 s/ y' I/ V% _% _
cout << "After swapping with m2, mapm2 is:";1 q1 l. W: T- Z( f: ^# Z
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )
& x% ~2 a/ s& T, x& ecout << " "<< m1_Iter ->second;. H0 T0 _+ n4 M: K% D, N
cout << "."<< endl;

3 [7 J- P5 J' s. h- D5 m
// This is the specialized template version of swap8 m* |3 }7 O, p, N9 u; N. W
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";
5 |: }8 g7 I1 dfor ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )3 F1 p3 R2 d/ X' c
cout << " "<< m1_Iter ->second;
9 Z. @. ]+ `$ Y4 a& ]cout << "."<< endl;1 x  Y  ?/ T& M4 O6 [
}

6. map的sort问题:, ?4 ^% |! N/ v1 `! T& p
Map中的元素是自动按key升序排序,所以不能对map用sort函数:
2 V4 y- h! y7 \# ~/ u: RFor example:
1 r0 G/ Q/ `+ U) H0 X$ ~$ n9 X#include<map>2 p( m0 J7 e( \: X2 F- b) |
#include<iostream>

usingnamespace std;

int main( )7 A* j' b. ~2 I8 p: x; D
{1 {0 L6 q* `& \  @+ l
map<int, int> m1;
) r& i2 c% Y$ rmap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
! Z; S2 j5 i0 o) I% a5 fm1.insert ( pair<int, int> ( 4, 40) );, A( K# n% }1 [, V# U6 _/ N' S. ?' ]
m1.insert ( pair<int, int> ( 3, 60) );. I5 W. O5 n( H8 |" X. \: o  Y
m1.insert ( pair<int, int> ( 2, 50) );
' \7 w8 t$ n8 L2 N9 x% o9 @' w8 im1.insert ( pair<int, int> ( 6, 40) );
  Q$ Z5 x6 w/ {* H! z0 |8 e* ]m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
3 p  [* M" s7 B9 Dfor ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )$ f' I' V9 ?; Y+ i
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;8 F& d4 T# l4 x- E, V6 C

2 o2 r' F- s/ C, B7 k! P9 a* g}" q, a8 {' G" _- ?. V: P

The original map m1 is:
8 O- N# }* m9 F4 _1 20
3 i0 m$ r5 K8 r- e7 i5 W1 L2 50
1 I& x1 e/ t; I6 x& f$ K; k4 D: Y3 60. s/ s" m( i3 D
4 40' l9 _5 C8 v+ O' [) X! f
6 40) T7 y6 h  P" i1 a& \  Q$ X, t" h
7 30

7. map的基本操作函数:
9 d; }3 r/ ^8 ]3 rC++Maps 是一种关联式容器,包含“关键字/值”对# y6 o, g* p0 D2 Q4 \7 {" V5 B
begin() 返回指向map头部的迭代器
: `) |2 k1 w! x* D4 r# Y; g' Oclear() 删除所有元素
4 U1 Y: a4 U( C  }' ]8 k4 M6 gcount() 返回指定元素出现的次数! `$ }7 ]; H) m# H# x
empty() 如果map为空则返回true
9 p% x6 q6 k! y4 j. Qend() 返回指向map末尾的迭代器
1 d' B& R) K9 E. y: X" |2 uequal_range() 返回特殊条目的迭代器对! V7 z) u* G( B
erase() 删除一个元素
1 q5 p; s# z' d8 Zfind() 查找一个元素) ~( z1 Y: M- L
get_allocator() 返回map的配置器1 ?; ?' c5 F0 ?" H8 X
insert() 插入元素
: b: l. c' K9 |$ Pkey_comp() 返回比较元素key的函数
5 x2 ~8 P/ `/ ]2 r% h4 M# Wlower_bound() 返回键值>=给定元素的第一个位置5 E8 Z  S8 R# F- c$ R3 y- F3 g
max_size() 返回可以容纳的最大元素个数
1 T6 U. H- b7 K2 A' krbegin() 返回一个指向map尾部的逆向迭代器% k) b) e, J( m- i, I* H+ @
rend() 返回一个指向map头部的逆向迭代器
, N0 ^( N+ H9 X8 f9 nsize() 返回map中元素的个数
, C) ]1 }# A! S/ ?; Bswap() 交换两个map4 G8 u4 }! }' c) U
upper_bound() 返回键值>给定元素的第一个位置" @1 W, G; p7 g: q, ?$ j
value_comp() 返回比较元素value的函数

  S2 @3 F" l& ^* D: I! K) Z9 W, p
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了