std::list与std::map的排序比较

排序:

std::list用其自身的sort方法

std::map用插入排序

 1 // stl_test.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5 #include <Windows.h>
6 #include <list>
7 #include <string>
8 #include <map>
9
10 struct _NpcDis
11 {
12 DWORD dwNpcId;
13 INT nDistance;
14
15 bool operator <(const _NpcDis& _data)
16 {
17 return nDistance < _data.nDistance;
18 }
19 };
20
21
22 struct _MapSortLess
23 {
24 bool operator()(const _NpcDis& _left, const _NpcDis& _right)
25 {
26 return _left.nDistance < _right.nDistance;
27 }
28 };
29
30 INT N = 1000;
31
32 int _tmain(int argc, _TCHAR* argv[])
33 {
34 _NpcDis _one;
35
36 DWORD dwStart = GetTickCount();
37 printf("---------List sort test----------\n");
38 std::list<_NpcDis> _ListTest;
39 for (INT i = 0; i < N; i++)
40 {
41 _one.dwNpcId = i;
42 _one.nDistance = rand();
43 _ListTest.push_back(_one);
44 }
45 _ListTest.sort();
46 printf("elapsed time:%d(ms)\n", GetTickCount() - dwStart);
47
48 // std::list<_NpcDis>::iterator _LstItor = _ListTest.begin();
49 // for (; _LstItor != _ListTest.end(); _LstItor++)
50 // {
51 // printf("%d-%d\n", _LstItor->dwNpcId, _LstItor->nDistance);
52 // }
53
54 printf("---------Map sort test----------\n");
55 dwStart = GetTickCount();
56 std::map<_NpcDis, int, _MapSortLess> _MapT;
57 for (INT i = 0; i < N; i++)
58 {
59 _one.dwNpcId = i;
60 _one.nDistance = rand();
61 _MapT[_one] = 1;
62 }
63 printf("elapsed time:%d(ms)\n", GetTickCount() - dwStart);
64
65 // std::map<_NpcDis, int, _MapSortLess>::iterator _MapItor=_MapT.begin();
66 // for (; _MapItor != _MapT.end(); _MapItor++)
67 // {
68 // printf("%d-%d\n", _MapItor->first.dwNpcId, _MapItor->first.nDistance);
69 // }
70 return 0;
71 }


N=1000时:

---------List sort test----------

elapsed time:109(ms)

---------Map sort test----------

elapsed time:16(ms)

N=10000时:

---------List sort test----------

elapsed time:1188(ms)

---------Map sort test----------

elapsed time:140(ms)

结论:std::map的插入排序比std::list自身的排序快了好几倍。

转载于:https://www.cnblogs.com/coderyoyo/archive/2011/09/23/list_map_sort.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值