小编lxn*_*ndr的帖子

map.find() 返回看似随机的 map.end()

我在 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)

c++ stdmap find comparator

2
推荐指数
1
解决办法
52
查看次数

标签 统计

c++ ×1

comparator ×1

find ×1

stdmap ×1