代码之家  ›  专栏  ›  技术社区  ›  user81993

如何提取无序映射::emplace返回的对的值?

  •  0
  • user81993  · 技术社区  · 7 年前

    std::unordered_map<std::string, int> um;
    

    我想把这个整数赋给同一行的一个变量,在同一行中,我在无序映射中放置了一对

    int i_want_132_here = um.emplace("hi", 132).first.???;
    

    在调试器中,我可以看到“first”包含(“hi”,132),但如何访问这些值?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Massimo Costa    7 年前

    emplace 返回 pair<iterator, bool> .

    所以你应该:

    int i_want_132_here = (*um.emplace("hi", 132).first).second;
    

    int i_want_132_here = um.emplace("hi", 132).first->second;
    

    总的来说我更喜欢 (*it) 形式而不是 it->