简体   繁体   English

更简单的std :: unordered_map :: insert形式?

[英]simpler form of std::unordered_map::insert?

Is there an easier way to check whether an std::unordered_map::insert call succeeded than writing this mammoth block of code? 是否有更简单的方法来检查std::unordered_map::insert调用是否成功,而不是编写这个庞大的代码块?

std::pair< T1, T2 > pair(val1, val2);
std::pair< std::unordered_map< T1, T2 >::const_iterator, bool> ret =
 _tileTypes.insert(pair);
if(!ret.second) {
    // insert did not succeed
}

How about just: 怎么样:

if(!_tileTypes.insert(std::make_pair(val1, vla2)).second) {
    // insert did not succeed
}
if (!_tileTypes.insert(pair).second)

?

Alternatively, typedefs can be useful to tidy this sort of thing up. 或者,typedef可以用来整理这类东西。

Also, if you're using C++11, then you can use the auto keyword to infer the type: 此外,如果您使用的是C ++ 11,则可以使用auto关键字来推断类型:

auto ret = _tileTypes.insert(pair);

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

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