找不到最新插入 std::map 的键

wen*_*eno 3 c++ oop stdmap std

我的问题是,无论我在 中插入的最后一个元素是什么std::map,我都找不到它。

我有以下地图,它将 Color 作为键并将其编码为某种对象类型(枚举)。

class ColorEncoder {
private:
    std::map<Color, Object::Type> validObjects;

public:
    ColorEncoder() {
        load(Color(76, 177, 34), Object::Player);
        load(Color(36, 28, 237), Object::Block);
     // load(Color(0, 111, 222), Object::PleaseDont); // uncomment this line and everything will work correctly,
                                                      // but the map will have one garbage value
    }

    void load(Color color, Object::Type type) {
        validObjects.insert(std::make_pair(color, type));
    }

    auto& getValidObjects() {
        return validObjects;
    }
};
Run Code Online (Sandbox Code Playgroud)

我也有一个array颜色。目标是验证每个数组元素是否确实存在于映射中。

因此,我遍历数组,每次检查当前数组元素是否作为映射中的键存在:

class BMP_Analyzer {
public:
    // assume data size is 12
    BMP_Analyzer(std::unique_ptr<Color[]>& data, std::map<Color, Object::Type>& validObjects) {
        for (int i = 0; i < 12; i++) {
            if (validObjects.find(data[i]) == validObjects.end()) {
                std::cout << "Not found in the map!\t";
                std::cout << "Blue: " << (uint16_t) data[i].blue << " " << " Green: "
                          << (uint16_t) data[i].green << " Red: " << (uint16_t) data[i].red
                          << " is not mapped!" << std::endl;
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

示例输出:

Not found in the map!   Blue: 36  Green: 28 Red: 237 is not mapped!
Not found in the map!   Blue: 36  Green: 28 Red: 237 is not mapped!
Run Code Online (Sandbox Code Playgroud)

但是,如果我取消注释:

// load(Color(0, 111, 222), Object::PleaseDont);
Run Code Online (Sandbox Code Playgroud)

然后它会正确检测到上面以前没有找到的颜色:(36, 28, 237)

对我来说,它看起来像是一个或一个,但老实说,我不知道潜在的错误在哪里。

颜色定义如下,重载operator<所以它可以作为一个键std::map

struct Color {
    uint8_t blue;
    uint8_t green;
    uint8_t red;

    Color() = default;

    Color(uint8_t blue, uint8_t green, uint8_t red)
        :blue{blue},
         green{green},
         red{red}
    {
    }

    bool operator<(const Color& other) const {
        return blue != other.blue || green != other.green || red != other.red;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何问题可能存在的提示都非常受欢迎,谢谢。

Die*_*ühl 8

a 中键的比较st::map必须是严格的弱顺序,即必须遵守以下规则:

  1. (a < a) == false
  2. (a < b) == true && (b < c) == true 暗示 (a < c) == true
  3. (a < b) == true 暗示 (b < a) == false
  4. (a < b) == false && (b < a) == false) && (b < c) == false && (c < b) == false) 暗示 (a < c) && (c < a) == false

实现结构的最简单方法是利用std::tuples 比较:

bool operator< (Color const& c0, Color const& c1) {
    return std::tie(c0.blue, c0.green, c0.red) < std::tie(c1.blue, c1.green, c1.red);
}
Run Code Online (Sandbox Code Playgroud)

这个比较运算符实际上定义了一个更强的顺序:全序。