【发布时间】:2013-04-28 01:16:26
【问题描述】:
我有由 3 个整数组合唯一标识的数据。
例如:
项目 #1:10,20,1
项目 #2:10,21,0
项目 #3:0,14,13
项目#4:103,324,78
我的结构:
struct structureKeyID
{
int keyA;
int keyB;
int keyC;
// Comparison operator for table sorting.
bool operator<(const structureKeyID& param) const
{
if (keyA < param.keyA) return true;
if (keyB < param.keyB) return true;
if (keyC < param.keyC) return true;
return false;
}
};
map <structureKeyID, classDataRecord> tableRecords;
我发现如果我添加一个键(0,0,1):
structureKeyID keyID1;
keyID1.keyA = 0;
keyID1.keyB = 0;
keyID1.keyC = 1;
tableRecords[keyID1] = <data>;
然后我检查键 (0,1,0) 是否存在:
structureKeyID keyID2;
keyID1.keyA = 0;
keyID1.keyB = 1;
keyID1.keyC = 0;
if (tableRecords.find(keyID2) != tableRecords.end())
然后会报错:
调试断言失败!
\include\xtree 行:1268
表达式:无效的运算符
然而,如果我检查 key (0,0,2) 或 key (10,0,2) 是否存在,它工作正常。
针对这种情况构建比较运算符的正确方法是什么?
非常感谢!
【问题讨论】:
-
您可能会发现这很有帮助 stackoverflow.com/questions/16340680/… 使用
std::tie。如果你没有 c++11,那么boost::tie可用。
标签: c++ dictionary key operator-keyword