C++ 使用具有更大\u相等比较器类的STL优先级\u队列

C++ 使用具有更大\u相等比较器类的STL优先级\u队列,c++,stl,priority-queue,C++,Stl,Priority Queue,运行以下示例时,我在标记行中收到一个调试断言 std::priority_queue<int, std::vector<int>, std::greater_equal<int>> queue_int; queue_int.push(1); queue_int.push(2); queue_int.push(1); // Debug Assertion Failed: Expression invalid comparator std::priority\u

运行以下示例时,我在标记行中收到一个调试断言

std::priority_queue<int, std::vector<int>, std::greater_equal<int>> queue_int;
queue_int.push(1);
queue_int.push(2);
queue_int.push(1); // Debug Assertion Failed: Expression invalid comparator
std::priority\u queue\u int;
队列内部推送(1);
队列内部推送(2);
队列内部推送(1);//调试断言失败:表达式无效的比较器

有什么提示吗?非常感谢您的帮助

当您将STL数据结构与比较器一起使用时,比较器始终需要严格控制,并且如果它接收到相同的对象进行比较,则永远不会返回true

想象一下,当两个相等的对象进行比较、交换时,下一次比较将再次在相同的两个对象之间进行。在这种情况下,STL排序步骤永远不会停止


当您将STL数据结构与比较器一起使用时,请始终尝试
std::greater
而不是
std::greater_equal

,如果比较器接收到要比较的相等对象,则该比较器需要严格且从不返回true

想象一下,当两个相等的对象进行比较、交换时,下一次比较将再次在相同的两个对象之间进行。在这种情况下,STL排序步骤永远不会停止


请尝试
std::greater
而不是
std::greater_equal

无法在以下情况下复制:

#include <stdio.h>
#include <vector>
#include <queue>

int main(int argc, char **argv)
{
    std::priority_queue<int, std::vector<int>, std::greater_equal<int>> queue_int;
    queue_int.push(1);
    queue_int.push(2);
    queue_int.push(1); // Debug Assertion Failed: Expression invalid comparator
    return 0;
}

请指定使用的确切编译标志

无法与以下内容一起复制:

#include <stdio.h>
#include <vector>
#include <queue>

int main(int argc, char **argv)
{
    std::priority_queue<int, std::vector<int>, std::greater_equal<int>> queue_int;
    queue_int.push(1);
    queue_int.push(2);
    queue_int.push(1); // Debug Assertion Failed: Expression invalid comparator
    return 0;
}

请指定使用的确切编译标志

您有未定义的行为。您的实现很好,并在检测到时断言。与
std::priority_queue
一起使用的比较器必须满足指定的要求
std::greater_equal
不会,因为如果传递相等的值,它将返回true

从相关文件中

类型
T
满足比较条件

给定

  • comp
    ,类型为
    T
要求

  • 对于所有
    a
    comp(a,a)=false

你有未定义的行为。您的实现很好,并在检测到时断言。与
std::priority_queue
一起使用的比较器必须满足指定的要求
std::greater_equal
不会,因为如果传递相等的值,它将返回true

从相关文件中

类型
T
满足比较条件

给定

  • comp
    ,类型为
    T
要求

  • 对于所有
    a
    comp(a,a)=false

我理解你的解释,但我还是想知道std::greater_equal?你能给出一个什么时候使用它的例子吗?我不认为std::greater_equal曾经用于STL数据结构(但可能我错了)。几乎所有的时候,你都会在STL中发现比你严格需要的更多的函数,因为有人可以在个人任务中使用它们。像这样的任务:计算数组中的元素>=0(在这里找到示例:)我理解你的解释,但我仍然想知道std::greater_equal?你能给出一个什么时候使用它的例子吗?我不认为std::greater_equal曾经用于STL数据结构(但可能我错了)。几乎所有的时候,你都会在STL中发现比你严格需要的更多的函数,因为有人可以在个人任务中使用它们。诸如:计算数组中的元素>=0(在此处找到示例:)之类的任务感谢您的帮助!谢谢你的帮助!