结构体使用sort算法时,重载operator<(..)。如果我们按下面这样写

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <stdint.h>
#include <algorithm>
#include <string.h>
using namespace std;
 
typedef struct SidInterface {
    SidInterface() {
        memset(this, 0, sizeof(SidInterface));
    }
    SidInterface(uint64_t s, uint32_t i) {
        sid = s;
        interface = i;
    }
    uint64_t sid;
    uint32_t interface;
 
    bool operator<(const SidInterface& tmp) const {
        if (sid > tmp.sid) {
            return false;
        } else if (sid <= tmp.sid) {
            return true;
        } else {
            return interface < tmp.interface;
        }
    }
 
    bool operator==(const SidInterface& tmp) const {
        return (sid == tmp.sid && interface == tmp.interface);
    }
} SidInterface_T;
 
 
int main()
{
    vector<SidInterface_T> vec {
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
            SidInterface(4504699140857627, 4001),
            SidInterface(4511296210624283, 4001),
 
    };
 
    sort(vec.begin(), vec.end());
    auto uni = unique(vec.begin(), vec.end());
    vec.erase(uni, vec.end());
}

我们在运行的时候,就会产生一个core dump;仔细去看也没有发现哪里不对,是不是很郁闷?
其实问题就在这里:

else if (sid <= tmp.sid) {

这种写法是错误的,应该是

else if (sid < tmp.sid) {

这里为什么会出错呢?这里牵扯到“Strict Weak Ordering",什么是Strict Weak Ordering?

1.必须是”反对称的(antisymmetric)" : 如果x < y 为真,则y < x为假

2.必须是“可传递(transitive)" :如果x<y为真且y<z为真,则x < z为真

3.必须是“非自反的(irreflexive): x < x永远为假

基于"Strict Weak Ordering",也就是所如果 x < y 为假且y < x为假,那么x == y.用程序表示即是:

!(x<y) && !(y<x)
...

而sort算法恰恰就必须遵循"Strict Weak Ordering".

于是 else if (sid <= tmp.sid) 这样写就会出现一件特别cool的事情:

sort在比较的时候,我们简单一些,假如10出现两次,为了好区分,第一次叫10A,第二次叫10B,

于是在比较两个10是否相等的时候,就会出现!(10A <= 10B) && !(10B <= 10A),这样结果就是false,也就是说10A!=10B,这是多么的COOL

所以不能这样写。

我们参见Effective STL 21条款,永远让比较函数对相等的值返回false

请参见:

www.sgi.com/tech/stl/StrictWeakOrdering.html

Effictive STL 条款21:永远让比较函数对相等的值返回false

Effictive STL 条款31:了解你的排序选择

STL使用sort注意的问题的更多相关文章

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

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

  2. STL 源码分析《1》---- list 归并排序的 迭代版本, 神奇的 STL list sort

    最近在看 侯捷的 STL源码分析,发现了以下的这个list 排序算法,乍眼看去,实在难以看出它是归并排序. 平常大家写归并排序,通常写的是 递归版本..为了效率的考虑,STL库 给出了如下的 归并排序 ...

  3. STL vector+sort排序和multiset/multimap排序比较

    由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...

  4. 转:详细解说 STL 排序(Sort)

    详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...

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

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

  6. STL List::sort() 解析

    看侯捷翻译那本<STL源码剖析>中list内置sort的算法,书中注释说是quick sort,看了半天没看明白, template <class T, class Alloc> ...

  7. 详细解说 STL 排序(Sort)(转)

    作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...

  8. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  9. STL之sort函数的用法

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! STL封装了一个排序算法,该算法相应的头文件为#include<algorithm>,我们能够依据须要对一个数组进行排序或者降序. so ...

随机推荐

  1. Java多线程之DaemonThreadFactory

    通过DaemonThreadFactory创建后台线程池 另外:如果是后台线程创建的线程,将都是后台线程. package wzh.daemon; import java.util.concurren ...

  2. Android开发者需要面对的8大挑战

    移动开发变得越来越受欢迎,但移动开发者正面临着一系列挑战.本文将介绍的是Android开发者需要面对的8个不利因素,例如缺乏硬件标准化,以及软件碎片.为Android OS开发app,给予了开发人员极 ...

  3. 第一个APP:IOS做简单运算的计算器

    步骤: 1.打开Xcode,单机Creat a new Xcode project 2.左边选择ios下Application,右边选择single view Application 3.填写项目名称 ...

  4. java web session监听销毁跳转

    1.了解如何使用HttpSessionListener监听session的销毁. 2.了解如何使用HttpSessionBindingListener监听session的销毁. 一. 使用HttpSe ...

  5. C++学习38 string字符串的增删改查

    C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...

  6. Highcharts 对数组的要求

    function Reflush(phaid,proid) { $.post('GetProjectSummer.ashx', { proid: proid, phaid: phaid }, func ...

  7. Library Cache: Lock, Pin and Load Lock

    What is "Library cache lock" ? This event controls the concurrency between clients of the ...

  8. IOS开发-跨域访问DWR方法

    用Phonegap做手机客户端,服务器用spring+DWR,需要在手机端访问服务器的方法,需要做以下配置,可以参见http://www.iteye.com/topic/337460: 服务器DWR配 ...

  9. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

  10. C++ 之旅:前言

    日前,拿起了C++教材开始学习. 在大学二年级的时候,其实C++已经是我们的必修课程.然而,那时的我刚从C语言的噩梦中逃出来,对C++也不甚喜爱.刚接触编程的我当时实在无法理解譬如下面这段 int x ...