微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 你可以在`std :: unique_ptr`的容器上使用`std :: remove_if`?

给定一个std :: vector< std :: unique_ptr< SomeType> >是合法的使用
remove_if就可以了换句话说,给出这个代码
std::vector<std::unique_ptr<SomeType> > v;
//  fill v,all entries point to a valid instance of SomeType...
v.erase( std::remove_if( v.begin(),v.end(),someCondition ),v.end() );

我保证擦除之后仍然在v中的所有指针
有效.我知道给出直观的实现
std :: remove_if,并给出了我所看到的所有实现,
他们会.我想知道标准中有没有什么
保证它;即std :: remove_if不允许复制
任何有效条目,无需将副本重新复制到其最终
位置.

(我当然是假设条件不复制,如果
条件有如下特征:

struct Condition
{
    bool operator()( std::unique_ptr<SomeType> ptr ) const;
};

那么当然所有的指针都将无效
的remove_if.)

解决方法

N3290中的25.3.8说明删除功能

Requires: The type of *first shall satisfy the MoveAssignable
requirements (Table 22).

Note: each element in the range [ret,last),where ret is the returned
value,has a valid but unspecified state,because the algorithms can
eliminate elements by swapping with or moving from elements that were
originally in that range.

这意味着它取决于你的谓词操作符.由于您的谓词不创建副本,因此元素不会被复制.

原文地址:https://www.jb51.cc/c/113384.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐