【发布时间】:2013-07-07 07:03:02
【问题描述】:
如果我将一对移动到地图中,但由于密钥已经存在而导致插入失败,那么之后我可以安全地使用这对吗?
//objects available: map, pair
auto insert_pair = map.insert(std::move(pair));
if (!insert_pair.second)
{
//can I safely access pair here?
}
标准中是否记录了这一点?
【问题讨论】:
-
如果标准中确实没有明确规定,作为一种变通方法,您可以使用
map::find来确定密钥是否存在,并在不存在时使用move它跨度> -
@Praetorian,可能值得使用
map::lower_bound或map::upper_bound,然后使用带有提示的插入来避免增加复杂性。
标签: c++ c++11 map move-semantics std-pair