标签: strict-weak-ordering

总的,弱的,部分的排序 - 完整的定义

有什么区别

  • 严格/非严格的订购,
  • 弱/非弱排序,和
  • 部分/总排序?

language-agnostic strict-weak-ordering

4
推荐指数
1
解决办法
989
查看次数

不要求排序需要严格的弱排序吗?

来自http://stdcxx.apache.org/doc/stdlibref/less-equal.html

-

您可以将less_equal对象传递给任何需要二进制函数的算法.例如,sort()算法可以接受二进制函数作为备用比较对象来对序列进行排序.less_equal将以下列方式用于该算法:

vector<int> vec1;
sort(vec1.begin(), vec1.end(),less_equal<int>());
Run Code Online (Sandbox Code Playgroud)

-

现在我很困惑,上面的文档是否正确?

c++ stl equality equivalence strict-weak-ordering

3
推荐指数
1
解决办法
1319
查看次数

如何通过对地图严格的弱排序对数学向量进行排序?

我尝试编写一个std :: map <Vector3D,double>,其中colinear(并行或反并行)向量应该共享相同的密钥.

作为比较函数,我使用以下函数(在isEqualEnough()中具有1e-9容差),这是我在std :: map使用(数学)向量创建的

struct Vector3DComparator 
{ 
    bool operator() (const Vector3D& lhsIn, const Vector3D& rhsIn) const
    {
        Vector3D lhs = lhsIn.absolute(); // make all members positive
        Vector3D rhs = rhsIn.absolute(); 

        if ((lhs.z < rhs.z)) 
            return true;

        if ((isEqualEnough(lhs.z, rhs.z)) 
            && (lhs.y < rhs.y)) 
            return true;

        if ((isEqualEnough(lhs.z, rhs.z)) 
            && (isEqualEnough(lhs.y, rhs.y))
            && (lhs.x < rhs.x))
            return true;

        return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

当我将一个立方体的法线插入我的地图时,我应该得到3个不同的值(因为我不关心方向)但我得到4:

  • x = 1 y = 0 z = 0
  • x = …

c++ stdmap strict-weak-ordering

3
推荐指数
1
解决办法
82
查看次数

std::sort 崩溃 - 没有严格弱排序的排序

我正在尝试对项目向量进行排序。正如代码注释中提到的,顺序应该是:

行动分较多(mAp)的参与者先行。当平局时,mDisposition与战斗发起者 ( )性格相同 ( ) 的参与者mBattleInitiator先行。

以下代码(简化示例)在 macOS 上崩溃,可能是由于我的排序实现不正确:

#include <QtCore>

class AiComponent
{
public:
    enum Disposition {
        Friendly,
        Hostile
    };

    AiComponent(Disposition disposition) : mDisposition(disposition) {}
    ~AiComponent() { qDebug() << "Destroying AiComponent"; }

    Disposition mDisposition;
};

class BattleManager
{
public:
    BattleManager() : mBattleInitiator(AiComponent::Hostile) {}

    class Turn {
    public:
        Turn() : mAp(1) {}

        Turn(QSharedPointer<AiComponent> aiComponent) :
            mAiComponent(aiComponent),
            mAp(1)
        {
        }

        Turn(const Turn &rhs) :
            mAiComponent(rhs.mAiComponent),
            mAp(1)
        {
        }

        QSharedPointer<AiComponent> mAiComponent;
        int mAp;
    };

    void …
Run Code Online (Sandbox Code Playgroud)

c++ sorting strict-weak-ordering

2
推荐指数
1
解决办法
1756
查看次数

严格的弱序排列混乱

我对严格的弱排序以及如何在定义operator <时使用它感到困惑.我有几个结构:

struct Plane
{
    std::string name;

    int xrudder;
    int yrudder;

    int wingwidgets;

    bool hasLegacyEngine;
};


struct Airport
{
    bool securityCheck;
    unsigned __int64 capacity;

    std::vector<Plane> planes;
};
Run Code Online (Sandbox Code Playgroud)

我想创建一个std::set机场.我需要定义operator <,它使用严格的弱排序,但我不确切地知道这意味着什么和/或如何做.

struct cmpless
{
bool operator()(const Airport& left, const Airport& right)
    {
        //?
    }
}; 

std::set<Airport, cmpless> airportSet;
Run Code Online (Sandbox Code Playgroud)

一个机场"小于"另一个机场没有意义.只有机场根据他们的统计数据相等才有意义.

我如何确定我对operator <的定义将遵循严格的弱排序?我如何开始考虑operator<在这种情况下进行定义?

如果可能的话,一个解释的例子会很棒!

c++ operator-overloading set strict-weak-ordering

1
推荐指数
1
解决办法
3358
查看次数

具有非唯一键排序但唯一比较的 std::map

考虑以下代码:

#include <iostream>
#include <map>
#include <utility>


struct Key{
    int attr1, attr2;
    Key(int attr1, int attr2) : attr1(attr1), attr2(attr2) {}

    friend bool operator== (const Key& s1, const Key& s2);
    friend bool operator< (const Key& s1, const Key& s2);
};

bool operator== (const Key& s1, const Key& s2){
    return ((s1.attr1 == s2.attr1) && (s1.attr2 == s2.attr2));
}

bool operator< (const Key& s1, const Key& s2){
    return (s1.attr1 < s2.attr1);
}

int main(void){
    std::map<Key, int> mmap;
    mmap.insert(std::make_pair(Key(10, 10), 5));
    mmap.insert(std::make_pair(Key(10, 20), 5)); …
Run Code Online (Sandbox Code Playgroud)

c++ stl key strict-weak-ordering

1
推荐指数
1
解决办法
57
查看次数

为什么STL中的priority_queue不遵循严格的弱排序?

我一直在玩STL容器和它们支持的比较函数/仿函数,但是我发现priority_queue不遵循通常的严格弱序,我试图理解可能是什么原因但不能弄明白,任何指针会有所帮助.

它在本博客中还提到priority_queue不遵循严格的弱排序.在此输入链接描述

#include "STL.h"
#include "queue"
#include "vector"
#include "iostream"
#include "functional"
using namespace std;

typedef bool(*func)(const int& val1 , const int& val2);

bool strict_weak_order_function(const int& val1 , const int& val2){
    return val1 > val2;
}

bool comparer_function(const int& val1 , const int& val2){
    return !strict_weak_order_function(val1 , val2);
}

struct Compaper_functor{
    bool operator()(const int& val1 , const int& val2){
        return !strict_weak_order_function(val1 , val2);
    }
};


void runPriorityQueue(void){
    //priority_queue<int , vector<int> , func > pq(comparer_function);
    priority_queue<int , vector<int> , Compaper_functor …
Run Code Online (Sandbox Code Playgroud)

c++ stl priority-queue strict-weak-ordering

0
推荐指数
1
解决办法
243
查看次数