由 www.169it.com 搜集整理

在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序。在通过vector+sort进行排序时,所有元素需要先存入vector容器中,sort在排序时又需要将元素全部取出来再进行排序。multimap底层实现为红黑树,因此元素在插入的过程中就实现了排序。那么到底哪一种排序速度更快呢?

下面有一个测试程序:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

#include <vector>

#include <set>

#include <algorithm>

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/time.h>

using namespace std;

double time() {

  struct timeval tv;

  if (gettimeofday(&tv, NULL) != 0) return 0.0;

  return tv.tv_sec + tv.tv_usec / 1000000.0;

}

struct Score {

  string name;

  double score;

  bool operator <(const Score& right) const {

    return score < right.score;

  }

};

int main(int argc, char** argv) {

  vector<Score> src;

  for (int i = 0; i < 10000000; i++) {

    int num = rand();

    char buf[32];

    sprintf(buf, "%d", num);

    Score score = { buf, num };

    src.push_back(score);

  }

  {

    double stime = time();

    vector<Score> res;

    for (vector<Score>::const_iterator it = src.begin();

         it != src.end(); ++it) {

      res.push_back(*it);

    }

    sort(res.begin(), res.end());

    double etime = time();

    printf("vector: %f\n", etime - stime);

  }

  {

    double stime = time();

    multiset<Score> res;

    for (vector<Score>::const_iterator it = src.begin();

         it != src.end(); ++it) {

      res.insert(*it);

    }

    double etime = time();

    printf("multiset: %f\n", etime - stime);

  }

  return 0;

}

程序运行结果为:

1

2

3

         time

vector   4.776060

multiset 10.761023

在这个测试中,vector+sort排序比multiset(multimap实现基于multiset)快多了。快速排序是目前已知的所有排序算法中最快的排序算法,因此它比基于堆排序的multiset快。

在这个测试结果出来之前,大多数人都会毫无疑问地认为multiset排序要更快。这也是有原因的,快速排序速度虽然快,但是在实际的运行过程中,它需要大量地拷贝元素,其拷贝操作的时间复杂度为o(NlogN),而基于红黑树的multiset在排序的过程中则避免了元素的拷贝。如果元素的内存占用空间比较大,那么multiset排序的速度将比vector+sort快。为了测试这个结果,将上面测试程序中的结构体改为:

1

2

3

4

5

6

7

8

9

10

struct Score {

  string name1;

  string name2;

  string name3;

  string name4;

  double score;

  bool operator <(const Score& right) const {

    return score < right.score;

  }

};

然后重新编译运行测试程序,测试结果为:

1

2

3

         time

vector   12.955739

multiset 11.368364

这个测试结果和我们的预期一致。

总之,我们在使用STL的排序算法时,需要根据不同的元素构造来进行合适的选择,如果都是比较简单的元素,那么适合选择vector+sort来进行排序,对于由字符串构成的占用了比较大的空间的复杂元素,应该使用multiset。如果排序的元素的总个数比较少,那么选择任意一种都可以。

STL vector+sort排序和multiset/multimap排序比较的更多相关文章

  1. STL的sort函数是使用什么排序算法的?

    先占坑,大概就是主要快速排序+插入排序+堆排序的合体

  2. [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...

  3. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  4. stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list

    stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...

  5. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  6. STL中sort排序算法第三个参数_Compare的实现本质

    关于C++ STL vector 中的sort排序算法有三种自定义实现,它们本质上都是返回bool类型,提供给sort函数作为第三个参数. 重载运算符 全局的比较函数 函数对象 我认为从实现方式看,重 ...

  7. [STL]vector与排序算法

    vector与算法 头文件中包含大量与 vector 相关的算法,这些算法同样适用于其它容器,比如 std::list 等. 排序(Sort) 相关函数: std::sort :普通排序 defaul ...

  8. c++STL之sort排序

    排序算法为竞赛中最常用的算法之一,我们可以利用C++自带的库函数进行排序.                                                                ...

  9. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

随机推荐

  1. eclipse中异常的快捷键

    选中你要try的代码,alt+shift+z 就会弹出一个菜单,里面有个try 选项

  2. CustomerSOList

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. Visual Studio 调用 Delphi DLL 会退出的解决方案

    新写了一个 Delphi 的 dll 供 C# 程序调用,却发现在使用 Visual Studio 2012 进行调试时,程序会在调用后自动退出. 经过对比,只需要将工程属性中“调试”一页下的“启用 ...

  4. 如何防止ListView控件闪烁

    如何防止ListView控件闪烁 beginupdate()和endupdate()之间写代码   ListView1.Items.BeginUpdate;ListView1.Items.Add('A ...

  5. QProcess调用外部程序方式的差异

    众所周知QProcess类的作用是启动一个外部的程序并与之交互它有三种方式调用外部程序: 1. execute 2. start 3. startDetached 从调用上看: execute是阻塞调 ...

  6. [Javascript] Monads

    Monads allow you to nest computations. They are a pointed functor that adds mjoin and chain function ...

  7. mysql用变量存储插入的id

    INSERT into a(value) values ('test');#set @last_id = LAST_INSERT_ID();set @last_id = (select max(id) ...

  8. Online Schema Upgrade in MySQL Galera Cluster using TOI Method

    http://severalnines.com/blog/online-schema-upgrade-mysql-galera-cluster-using-toi-method     As a fo ...

  9. Linux下vim配置详解

    转自http://www.cnblogs.com/witcxc/archive/2011/12/28/2304704.html

  10. NopCommerce 数据库初始化

    NopCommerce数据库初始化比较复杂,我简化了,只初始化创建一张表,不多说,直接上代码: //数据实体 /// <summary> /// Represents an affilia ...