可以使用 std::find_if 算法和 lambda 表达式来查找:

int key = -1; // 初始化为无效键
auto it = std::find_if(keyMap.begin(), keyMap.end(), [&](const auto& pair) {
    return pair.second == '要查找的键的值';
});
if (it != keyMap.end()) {
    key = it->first;
}

如果找到了,key 就是对应的键值;否则 key 仍然为无效键 -1。

C++ std::map 使用技巧:通过值查找键

原文地址: https://www.cveoy.top/t/topic/ntKu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录