简体   繁体   English

如何提取unordered_map :: emplace重新调整的对的值?

[英]How to extract the value of the pair retuned by unordered_map::emplace?

I'm trying to make my code 1 line shorter, a noble cause. 我试图将我的代码1行缩短,这是一个高尚的原因。 I have this unordered map 我有这张无序的地图

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

and I want to assign the integer to a variable on the same line where I emplace a pair into the unordered map like so 并且我想将整数分配给同一行上的变量,在该行上将一对放入无序映射中,如下所示

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

problem is, I have no idea what to do with [return value of unordered_map::emplace].first 问题是,我不知道该如何处理[unordered_map :: emplace的返回值]。

In the debugger I can see that "first" contains ("hi", 132) but how do I access those values? 在调试器中,我可以看到“ first”包含(“ hi”,132),但是如何访问这些值?

emplace returns a pair<iterator, bool> . emplace返回pair<iterator, bool>

So you should do: 因此,您应该执行以下操作:

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

alternative syntax: 替代语法:

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

In general I prefer (*it) form instead of it-> . 通常,我更喜欢(*it)形式,而不是it->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM