C++ 如何提取无序映射::emplace返回的对的值?
我正试图缩短我的代码1行,这是一项崇高的事业。我有这张无序的地图C++ 如何提取无序映射::emplace返回的对的值?,c++,unordered-map,C++,Unordered Map,我正试图缩短我的代码1行,这是一项崇高的事业。我有这张无序的地图 std::unordered_map<std::string, int> um; 问题是,我不知道如何处理[unordered_map::emplace的返回值]。首先 在调试器中,我可以看到“first”包含(“hi”,132),但如何访问这些值?emplace返回一个对 所以你应该: int i_want_132_here = (*um.emplace("hi", 132).first).second; 替代语
std::unordered_map<std::string, int> um;
问题是,我不知道如何处理[unordered_map::emplace的返回值]。首先
在调试器中,我可以看到“first”包含(“hi”,132),但如何访问这些值?
emplace
返回一个对
所以你应该:
int i_want_132_here = (*um.emplace("hi", 132).first).second;
替代语法:
int i_want_132_here = um.emplace("hi", 132).first->second;
一般来说,我更喜欢(*it)
表单,而不是it->
emplace
返回对
所以你应该:
int i_want_132_here = (*um.emplace("hi", 132).first).second;
替代语法:
int i_want_132_here = um.emplace("hi", 132).first->second;
一般来说,我更喜欢(*it)
表单而不是it->
inti\u want\u 132\u here=um.emplace(“hi”,132)。第一->第二
@Borgleader na.second在调试器中显示为“true”,根据文档,它应该表示安放成功,当然我不知道如何将“true”作为布尔值访问either@RetiredNinja对@用户81993是的,我错过了,这就是我删除评论的原因,我正要发布,但是一个忍者打败了我。你也可以用同样的方式访问bool<代码>bool inserted=um.emplace(“hi”,132)。秒代码>int i_want_132_here=um.emplace(“hi”,132)。第一->第二
@Borgleader na.second在调试器中显示为“true”,根据文档,它应该表示安放成功,当然我不知道如何将“true”作为布尔值访问either@RetiredNinja对@用户81993是的,我错过了,这就是我删除评论的原因,我正要发布,但是一个忍者打败了我。你也可以用同样的方式访问bool<代码>bool inserted=um.emplace(“hi”,132)。秒代码>对不起,我修正了我的答案为什么不->
。@riv迭代器的语法与指针类似it->value
和(*it)。value
是一样的。@riv我知道箭头操作符->
也可以使用,但一般来说我更喜欢(*it)
形式抱歉,我修正了我的答案为什么不->
。@riv迭代器的语法类似于指针it->value
和(*it)。value
是一样的。@riv我知道箭头操作符->
也可以使用,但通常我更喜欢(*it)
表单