我想使用复杂类型的 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/