Sort / stable_sort自定义比较功能会引起一些奇怪的问题 [英] Sort/stable_sort custom compare function cause some strange issues

查看:194
本文介绍了Sort / stable_sort自定义比较功能会引起一些奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对sort / stable_sort API的自定义功能具有非常基本的经验。

以下是我在Windows Visual Studio 2017下运行的源代码。

请帮助分析有什么问题,我会错过任何事情还是背后的理论是什么?感谢您的帮助

I have very basic experience of custom function for sort/stable_sort API.
Below is the source code I am running under Windows Visual Studio 2017.
Please help analyze what is the problem, do I miss anything or what is the theory behind? Thanks for the help

// 10_3_1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
#define MAX_INPUT_NO    (10u)

typedef bool(* sort_func_t)(const int input_a, const int input_b);

bool is_shorter(const int input_a, const int input_b)
{
#if 0
    //this portion will show the iteration overlap
    if (input_a > input_b)
        return false;
    else
        return true;
#else
    //both below works
    //return input_a < input_b;
    return input_a > input_b;

#endif
}

void elimDups(vector<int> &dat, sort_func_t func)
{
    vector<int>::iterator temp = dat.begin();
    std::stable_sort(dat.begin(), dat.end(), func);
    //sort(dat.begin(), dat.end());

    temp = unique(dat.begin(), dat.end());
    dat.erase(temp, dat.end());
}

void print_vec(vector<int> &dat)
{
    vector<int>::const_iterator index = dat.cbegin();
    int i = 0;
    for (; index < dat.cend(); index++)
    {
        cout << "dat[" << i << "] = " << dat.at(i++) << endl;
    }
}

int main()
{
    vector<int> a;
    int ia[MAX_INPUT_NO] = {0, 1, 2, 1, 2, 3, 1, 2, 4, 5};
    a.assign(ia, ia + MAX_INPUT_NO);
    print_vec(a);
    elimDups(a, is_shorter);
    print_vec(a);

    getchar();
    return 0;
}

但是在使用if-else时我会遇到问题

But the problem I face when I play with if-else portion, it gives me the invalid comparator assert error.



  1. 如果我定义了自定义函数像下面那样使用if-else模式,它可以正常工作。




bool is_shorter(const int input_a, const int input_b)
{
#if 1
    //this portion will show the iteration overlap
    if (input_a > input_b)
        return true;
    else
        return false;
#else
    //below works
    return input_a > input_b;
#endif
}

以下是我得到的结果。

Below is the result I get.

预期项目1的结果



  1. 如果我定义了自定义比较器功能,如下所示,它也使用if-else模式,它将因无效比较器错误而失败。




bool is_shorter(const int input_a, const int input_b)
{
#if 1
    //this portion will show the iteration overlap
    if (input_a > input_b)
        return false;
    else
        return true;
#else
    //below works
    return input_a > input_b;
#endif
}

以下是我得到的错误消息:

Below is the error message I get:

Visual Studio 2017中的错误消息

error message from visual studio 2017



  1. 我只使用return,然后在两个方向上都可以正常工作。




bool is_shorter(const int input_a, const int input_b)
{
#if 0
    //this portion will show the iteration overlap
    if (input_a > input_b)
        return false;
    else
        return true;
#else
    //both below works
    //return input_a < input_b;
    return input_a > input_b;

#endif
}


推荐答案

此代码:

if (input_a > input_b)
    return false;
else
    return true;

是一种复杂的说法:

return !(input_a > input_b);

更大的否定则小于或等于

return input_a <= input_b; // logically same as before

问题是您不能使用小于或等于用于排序的关系,因为它不提供算法要求的严格弱排序。您可以使用更少的内容:

The problem is you cannot use less or equal relation for sorting as it does not provide strict weak ordering the algorithm requires. You can use less though:

return input_a < input_b;

或大于在代码工作时所使用的值。

or greater then as you used in your code when it works.

这篇关于Sort / stable_sort自定义比较功能会引起一些奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆