【问题标题】:Is std::set `erase ( const key_type& x )` implemented in STL same way ` erase ( collection.find( x ) );` does?std::set `erase (const key_type& x)` 在 STL 中的实现方式与 `erase (collection.find(x));` 相同吗?
【发布时间】:2012-03-10 08:14:52
【问题描述】:

std::set erase ( x ) 是否以与 erase ( collection.find( x ) ); 相同的方式在 STL 中实现(其中 xconst key_type&)呢?

【问题讨论】:

  • 看看你的 STL 实现,让我们知道你发现了什么!

标签: c++ stl set


【解决方案1】:

我确信它们可能非常相似,但它们会有所不同,因为一个必须以某种方式处理“未找到”元素,而另一个不需要担心(而且可能不会'不在非调试版本中)。

  • std::set::erase( const key_type& ) 将返回 01,具体取决于是否找到要擦除的匹配元素。

  • std::set::erase( iterator ) 仅当传入的迭代器是可取消引用的时,才返回任何内容并且定义了行为。所以如果你传入find(x),而x 不在集合中,你就有问题了。

【讨论】:

    猜你喜欢
    • 2013-01-22
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2015-04-01
    相关资源
    最近更新 更多