map-排序

突然间发现,Map中的元素是自动按key升序排序的。

附代码:

View Code
 1 #include <map> 
 2 #include <iostream> 
 3 using namespace std;
 4  
 5 int main( ) 
 6 { 
 7 //    freopen("data.in","r",stdin);
 8 //    freopen("data.out","w",stdout);
 9     
10     map <int, int> m1;     
11     map <int, int>::iterator m1_Iter;     
12     m1.insert ( pair <int, int> ( 1, 20 ) );     
13     m1.insert ( pair <int, int> ( 4, 40 ) ); 
14     m1.insert ( pair <int, int> ( 3, 60 ) );     
15     m1.insert ( pair <int, int> ( 2, 50 ) );     
16     m1.insert ( pair <int, int> ( 6, 40 ) );     
17     m1.insert ( pair <int, int> ( 7, 30 ) );     
18     cout << "The original map m1 is:"<<endl;     
19     for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )     
20     cout << m1_Iter->first<<" "<<m1_Iter->second<<endl; 
21     return 0;
22 } 
23 
24 /*
25 结果:
26 The original map m1 is:
27 1 20
28 2 50
29 3 60
30 4 40
31 6 40
32 7 30
33 */

转载于:https://www.cnblogs.com/Hug-Sea/articles/2494690.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值