c++ - std::priority_queue 复杂类型?

标签 c++ data-structures struct std priority-queue

我想使用复杂类型的 std::priority_queue:

typedef struct
{
uint8_t data;
    uint64_t moredata;
}myData;

typedef struct
{
    boost::mutex someQueueLock;
    std::priority_queue<myData> myQueue; //does not work
}

我不想使用充满指针的队列 (priority_queue),因为指针可能会变得无效。

这可能吗?或者我应该使用另一个 std 容器?

最佳答案

std::priority_queue使用 operator<默认情况下对元素进行排序,它要求元素是严格弱排序

你需要定义operator< myData 的功能类型

bool operator<(const myData& lhs, const myData& rhs)
{
    return lhs.data < rhs.data;
}

§ 23.6.4.1 类模板priority_queue

Any sequence container with random access iterator and supporting operations front(), push_back() and pop_back() can be used to instantiate priority_queue. In particular, vector (23.3.6) and deque (23.3.3) can be used. Instantiating priority_queue also involves supplying a function or function object for mak- ing priority comparisons; the library assumes that the function or function object defines a strict weak ordering (25.4).

§ 25.4

All the operations in 25.4 have two versions: one that takes a function object of type Compare and one that uses an operator<.

关于c++ - std::priority_queue 复杂类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906131/

相关文章:

Java到C++转换代码

data-structures - 树遍历 - 从只有父指针的叶子开始?

algorithm - 这是一棵完全二叉树吗?

c# - 将 C++ 二维固定长度 char 数组编码(marshal)为结构成员

C# - 值类型等于方法 - 为什么编译器使用反射?

c++ - 返回派生类型实例的 CRTP 和函数

c++ - Qt 创建者 : “inline function used but never defined” – why?

c++ - 如何将一个字符串放在另一个字符串中,而不是某个索引处的字符

java - 如何将列表映射的值转换为单个列表

c++ - 只写结构体的部分字段