为什么 std::map.find() 在以下情况下不起作用?
Why is the std::map.find() not working in following case?
snmp.h
头文件包含 AsnObjectIdentifier
结构的定义,不幸的是,该结构没有相等运算符重载。我希望 AsnObjectIdentifier
成为 std::map
的键,但问题是 find()
无法在地图中找到键。我定义了一个自定义比较器 AsnObjectIdentifierComparator
作为 std::map 声明的第三个模板参数。该场景的最小可重现代码如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef unsigned int UINT;
typedef struct {
UINT idLength;
UINT * ids;
} AsnObjectIdentifier;
struct AsnObjectIdentifierComparator {
bool operator()(const AsnObjectIdentifier& left, const AsnObjectIdentifier& right) const {
UINT* leftOidArr = left.ids, * rightOidArr = right.ids;
UINT smallerOidLen = (left.idLength < right.idLength ? left.idLength : right.idLength);
for (UINT i = 0; i < smallerOidLen; i++) {
if (leftOidArr[i] < rightOidArr[i]) {
return true;
}
}
if (smallerOidLen == left.idLength) {
return true;
}
return false;
}
};
typedef std::map<AsnObjectIdentifier, std::string, AsnObjectIdentifierComparator> MibMap;
int main(void){
MibMap map;
UINT expectedOID1ids[] = { 1, 3, 6, 1, 1, 1, 2, 1 };
AsnObjectIdentifier expectedOID1 = { 8, expectedOID1ids };
map.insert( std::pair<AsnObjectIdentifier, std::string>(expectedOID1, "present") );
cout << map.size() << endl;
if(map.find(expectedOID1) == map.end()) {
cout << "Not found" << endl;
}
}
AsnObjectIdentifierComparator
定义键在地图中的放置顺序是有道理的,但如果我们无法首先找到键,则没有用。在 std::map
的情况下没有更多的模板参数,并且它没有像 unordered_map
中那样的 keyequal
参数。此外,我无法控制 AsnObjectIdentifier
的定义,因为它已在其他头文件中定义。我该如何解决这种情况?
您的比较不符合严格的弱排序。
我建议在需要自定义时使用 stl 的帮助程序 std::tuple
(with std::tie
) or in your case std::lexicographical_compare
:
struct AsnObjectIdentifierComparator
{
bool operator()(const AsnObjectIdentifier& lhs, const AsnObjectIdentifier& rhs) const
{
return std::lexicographical_compare(lhs.ids, lhs.ids + lhs.idLength,
rhs.ids, rhs.ids + rhs.idLength);
}
};
snmp.h
头文件包含 AsnObjectIdentifier
结构的定义,不幸的是,该结构没有相等运算符重载。我希望 AsnObjectIdentifier
成为 std::map
的键,但问题是 find()
无法在地图中找到键。我定义了一个自定义比较器 AsnObjectIdentifierComparator
作为 std::map 声明的第三个模板参数。该场景的最小可重现代码如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef unsigned int UINT;
typedef struct {
UINT idLength;
UINT * ids;
} AsnObjectIdentifier;
struct AsnObjectIdentifierComparator {
bool operator()(const AsnObjectIdentifier& left, const AsnObjectIdentifier& right) const {
UINT* leftOidArr = left.ids, * rightOidArr = right.ids;
UINT smallerOidLen = (left.idLength < right.idLength ? left.idLength : right.idLength);
for (UINT i = 0; i < smallerOidLen; i++) {
if (leftOidArr[i] < rightOidArr[i]) {
return true;
}
}
if (smallerOidLen == left.idLength) {
return true;
}
return false;
}
};
typedef std::map<AsnObjectIdentifier, std::string, AsnObjectIdentifierComparator> MibMap;
int main(void){
MibMap map;
UINT expectedOID1ids[] = { 1, 3, 6, 1, 1, 1, 2, 1 };
AsnObjectIdentifier expectedOID1 = { 8, expectedOID1ids };
map.insert( std::pair<AsnObjectIdentifier, std::string>(expectedOID1, "present") );
cout << map.size() << endl;
if(map.find(expectedOID1) == map.end()) {
cout << "Not found" << endl;
}
}
AsnObjectIdentifierComparator
定义键在地图中的放置顺序是有道理的,但如果我们无法首先找到键,则没有用。在 std::map
的情况下没有更多的模板参数,并且它没有像 unordered_map
中那样的 keyequal
参数。此外,我无法控制 AsnObjectIdentifier
的定义,因为它已在其他头文件中定义。我该如何解决这种情况?
您的比较不符合严格的弱排序。
我建议在需要自定义时使用 stl 的帮助程序 std::tuple
(with std::tie
) or in your case std::lexicographical_compare
:
struct AsnObjectIdentifierComparator
{
bool operator()(const AsnObjectIdentifier& lhs, const AsnObjectIdentifier& rhs) const
{
return std::lexicographical_compare(lhs.ids, lhs.ids + lhs.idLength,
rhs.ids, rhs.ids + rhs.idLength);
}
};