我如何定义一个以 struct 为键的 QMap
How I can define a QMap with struct as key
我尝试定义一个 QMap,它的关键是一个 C++ 结构。
struct ProcInfo_S
{
quint8 tech = 0;
quint8 direction = 0;
quint8 category = 0;
};
QMap<ProcInfo_S, uint64_t> G;
G[{2,3,4}] = 2;
但是当我编译这段代码时,出现以下编译错误:
error: no match for ‘operator<’ (operand types are ‘const MainWindow::MainWindow(QWidget*)::ProcInfo_S’
地图中元素的顺序通过调用键的 operator<
来确定。来自 documentation:
[...] The key type of a QMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.
实现它的一种方法是例如:
struct ProcInfo_S
{
quint8 tech = 0;
quint8 direction = 0;
quint8 category = 0;
bool operator<( const ProcInfo_S& other) const {
return std::tie(tech,direction,category) < std::tie(other.tech,other.direction,other.category);
}
};
我尝试定义一个 QMap,它的关键是一个 C++ 结构。
struct ProcInfo_S
{
quint8 tech = 0;
quint8 direction = 0;
quint8 category = 0;
};
QMap<ProcInfo_S, uint64_t> G;
G[{2,3,4}] = 2;
但是当我编译这段代码时,出现以下编译错误:
error: no match for ‘operator<’ (operand types are ‘const MainWindow::MainWindow(QWidget*)::ProcInfo_S’
地图中元素的顺序通过调用键的 operator<
来确定。来自 documentation:
[...] The key type of a QMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.
实现它的一种方法是例如:
struct ProcInfo_S
{
quint8 tech = 0;
quint8 direction = 0;
quint8 category = 0;
bool operator<( const ProcInfo_S& other) const {
return std::tie(tech,direction,category) < std::tie(other.tech,other.direction,other.category);
}
};