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

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

[复制链接]

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

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

mildcat 楼主

2016-8-29 20:25:18

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

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

x
最全的c++map的用法

此文是复制来的0.0

1. map最基本的构造函数;
& E9 G5 G: ]2 b* {map<string ,int>mapstring; map<int,string >mapint;
2 W3 o! a9 B" c% nmap<sring,char>mapstring; map< char ,string>mapchar;
$ [+ W8 m% S  b% x; Omap<char,int>mapchar; map<int ,char>mapint;

2. map添加数据;

map<int ,string>maplive;" N# L. I5 J, _1 S2 K0 a
1. maplive.insert(pair<int,string>(102,"aclive"));- o5 @; S( S6 j' X6 R
2. maplive.insert(map<int,string>::value_type(321,"hai"));
) s, P$ ?) \* i: F+ S3. maplive[112]="April";//map中最简单最常用的插入添加!) E2 N, H: c) o

3. map中元素的查找:

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

map<int ,string >::iteratorl_it;; 3 B* U) k( I+ Y0 V& G
l_it=maplive.find(112);//返回的是一个指针0 x' w3 c  y. {6 k- m, z
if(l_it==maplive.end())- ~8 d' K8 }( L) i5 e
cout<<"we do not find112"<<endl;1 |: K/ Q$ }* P: I4 B$ z5 v# O
elsecout<<"wo find112"<<endl;
7 u3 F6 M& _9 `


- y; Z: e  o3 P3 e; N2 b

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;
' l9 T% G2 V. E# y: o

4. map中元素的删除:
  Z  F( G( T; `如果删除112;
* _, d' d, w2 S/ @6 i# C$ Qmap<int ,string>::iterator l_it;;
+ _1 r) A8 @2 k  Z9 R. D! hl_it =maplive.find(112);- J9 ?6 x  W# ^$ B
if( l_it == maplive.end())0 k3 `, W  B6 j* x3 {
cout<<"we do not find112"<<endl;2 ~  Q  A- R( F# _, N
else maplive.erase(l_it);//delete 112;) l3 A" q: A- D( F  w

5. map中 swap的用法:
# P: [1 [2 z0 Q) K6 n/ S% A1 AMap中的swap不是一个容器中的元素交换,而是两个容器交换;
1 P8 v+ P* R3 ?/ WFor example:6 `  q8 s& m* N, a
#include<map>
7 ]2 W9 @( Z( }3 t- q4 n) F#include<iostream>

usingnamespace std;

int main()6 y( U3 S8 D' ^* i" `
{' B) L( `0 ?. W
map <int, int> m1, m2, m3;* Z6 ^- Y) C+ ~6 G. K$ `' ~
map <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );
2 K9 o* V/ A/ lm1.insert ( pair <int,int> ( 2, 20 ) );" w4 P2 r; c& @# \* a2 I4 E
m1.insert ( pair <int,int> ( 3, 30 ) );8 x3 h! L& T5 M6 v+ l( n$ r1 f
m2.insert ( pair <int,int> ( 10, 100 ) );# H9 a+ A8 w0 N% F  S! E3 r. X1 {
m2.insert ( pair <int,int> ( 20, 200 ) );/ e. [% ]% C6 R: w% Y# [
m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";* d* q: E+ t; X' e. z) h( p
for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ ): Z2 p6 {+ u: r$ q
cout << " "<<m1_Iter->second;8 [/ e2 @4 m& a, K6 `; [% Y6 {
cout << "."<< endl;

// This isthe member function version of swap
7 D$ T! [9 ?+ H  A+ q// m2 is said to be theargument map; m1 the target map
' ^' m5 J0 W# y5 ^8 A$ y% D1 v2 Gm1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";
4 Q! _0 H; `2 O( F9 r& Qfor ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )- E/ A# u5 Y: Y3 p
cout << " "<< m1_Iter ->second;
" J& E7 Q* I% Ncout << "."<< endl;

4 Y1 a. e/ \+ F0 d
cout << "After swapping with m2, mapm2 is:";
# R# q: X* J, B1 ?: P( ifor ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )% U& I4 Q# c- }; _, b  W% [' q8 V
cout << " "<< m1_Iter ->second;
/ A1 w: }# w( @cout << "."<< endl;


1 O6 L7 T0 s& X" t// This is the specialized template version of swap4 i. j/ ]! `# V; ?7 O9 r
swap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";- N9 ^/ {% {/ G9 `
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )% y; h5 F6 j/ a3 D. h
cout << " "<< m1_Iter ->second;
+ T" I8 O; l# l: Dcout << "."<< endl;0 n+ S5 ^4 {4 h: |
}

6. map的sort问题:
3 i4 o1 T8 J: I  m4 g7 j# b! t1 g' a8 \Map中的元素是自动按key升序排序,所以不能对map用sort函数:
0 F% u! w. o0 P. ]9 M, GFor example:
; P3 r  x, ^1 t  T+ n! D#include<map>3 d2 V/ z; Z" w9 @
#include<iostream>

usingnamespace std;

int main( )# N4 R; c: U) i! f
{
+ D5 ?' F' w0 L$ Q# [map<int, int> m1;
9 L( {# ?! `- N* Q1 ymap <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );+ |. k; m# i# i2 }9 r
m1.insert ( pair<int, int> ( 4, 40) );
, l( a! [5 O+ s3 tm1.insert ( pair<int, int> ( 3, 60) );
9 K% @, q% }" _) H! y2 K0 Hm1.insert ( pair<int, int> ( 2, 50) );  b5 D% k) b% E1 W
m1.insert ( pair<int, int> ( 6, 40) );
3 {) \7 R6 m- H/ Cm1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
/ N9 A8 W( q  K6 }for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ ); l1 S9 |2 w3 q% H- d& _. y. c
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;
: r' K) W* R5 c) U. d. o  J0 A" M+ h" w6 G
}' q/ X, ?+ L- x3 B! C

The original map m1 is:
( {5 R; }2 d! I' G5 z1 209 x7 d4 {* S& E( r6 W
2 502 q; O! M( q0 j$ C1 v* B
3 60
8 B6 w. G5 J) f3 [+ j5 i4 40
. g9 z) t2 U6 j) g7 h6 40
6 V5 U% @8 u) ?& b) O" N/ @# f* Y7 30

7. map的基本操作函数:5 ?% c+ k) y2 C
C++Maps 是一种关联式容器,包含“关键字/值”对
7 r% y) r/ z6 s9 t. a; qbegin() 返回指向map头部的迭代器# a; U/ E+ ~4 i
clear() 删除所有元素+ J6 p- |+ q: y& v% z1 z' W5 |
count() 返回指定元素出现的次数5 e" o' E. ?- s% b3 B' ^% l
empty() 如果map为空则返回true
# S+ G9 s! S) k  Y* K0 h4 F2 wend() 返回指向map末尾的迭代器, h" L' w7 u9 ?: q1 K
equal_range() 返回特殊条目的迭代器对% n- C, S' H8 P" K; b, Z* c' e
erase() 删除一个元素
& E/ ~5 V+ i. w, L9 R3 j5 hfind() 查找一个元素9 V8 v. k1 a$ I; l# A; r- g
get_allocator() 返回map的配置器
0 P$ b0 K: h5 j, E& G7 {& I  V  Q; einsert() 插入元素
* X9 D& ]; g  l' W$ ekey_comp() 返回比较元素key的函数
7 T: ]) f- c2 H. v. M( C+ Nlower_bound() 返回键值>=给定元素的第一个位置% x5 \0 V  I/ f+ P
max_size() 返回可以容纳的最大元素个数' b  @7 Q7 ]. q$ ]
rbegin() 返回一个指向map尾部的逆向迭代器; E' v* P" v) K0 A( L( J4 f
rend() 返回一个指向map头部的逆向迭代器7 {" j$ L. ]" X" \
size() 返回map中元素的个数
; W+ j3 O: }" h( O& r5 gswap() 交换两个map
  ]' Q3 Z0 B: o5 |6 Z0 {4 o2 M* `upper_bound() 返回键值>给定元素的第一个位置
: q1 A: S7 t( q- I$ x+ b$ b# {value_comp() 返回比较元素value的函数


9 B: e% i2 K# ^
该会员没有填写今日想说内容.
回复

使用道具 举报

发表回复

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

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

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

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

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

    我知道了