加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:54:52 所属栏目:百科 来源:网络整理
导读:给定一个std :: vector std :: unique_ptr SomeType 是合法的使用 remove_if就可以了换句话说,给出这个代码: std::vectorstd::unique_ptrSomeType v;// fill v,all entries point to a valid instance of SomeType...v.erase( std::remove_if( v.begin(),v.
给定一个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 unspeci?ed state,because the algorithms can
eliminate elements by swapping with or moving from elements that were
originally in that range.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读