我在 C++ 中使用自定义比较器map。不幸的是,map.find()通常不会在地图中找到所需的条目。重现这一点的代码非常简单:
#include <array>
#include <iostream>
#include <map>
using namespace std;
typedef struct DATA_t {
array<int, 4> data;
} DATA_t;
struct DATA_cmp {
bool operator()(const DATA_t& a, const DATA_t& b) const {
for (int i = 0; i < a.data.size(); ++i)
if (a.data[i] < b.data[i]) return true;
return false;
}
};
int main(int argc, char** argv) {
map<DATA_t, int, DATA_cmp> index;
DATA_t data1 = {1, 2, 4, 8};
DATA_t data2 = {1, 3, 5, 7}; …Run Code Online (Sandbox Code Playgroud)