如何提取unordered_map::emplace返回的pair的值?
How to extract the value of the pair retuned by unordered_map::emplace?
我正在尝试缩短我的代码 1 行,这是一项崇高的事业。我有这张无序地图
std::unordered_map<std::string, int> um;
我想将整数分配给同一行上的一个变量,我将一对放入无序映射中,就像这样
int i_want_132_here = um.emplace("hi", 132).first.???;
问题是,我不知道如何处理 unordered_map::emplace] 的 [return 值。first
在调试器中我可以看到 "first" 包含 ("hi", 132) 但我如何访问这些值?
emplace
returns一个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->
。
我正在尝试缩短我的代码 1 行,这是一项崇高的事业。我有这张无序地图
std::unordered_map<std::string, int> um;
我想将整数分配给同一行上的一个变量,我将一对放入无序映射中,就像这样
int i_want_132_here = um.emplace("hi", 132).first.???;
问题是,我不知道如何处理 unordered_map::emplace] 的 [return 值。first
在调试器中我可以看到 "first" 包含 ("hi", 132) 但我如何访问这些值?
emplace
returns一个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->
。