首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::remove

Defined in header <algorithm>

?

?

template< class ForwardIt, class T > ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );

(1)

?

template< class ExecutionPolicy, class ForwardIt, class T > ForwardIt remove( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value );

(2)

(since C++17)

template< class ForwardIt, class UnaryPredicate > ForwardIt remove_if( ForwardIt first, ForwardIt last, UnaryPredicate p );

(3)

?

template< class ExecutionPolicy, class ForwardIt, class UnaryPredicate > ForwardIt remove_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryPredicate p );

(4)

(since C++17)

从范围中移除所有满足特定条件的元素。[first, last)并为范围的新结束返回一个过去的迭代器。

1%29删除所有等于value...

3%29移除谓词的所有元素。p回报true...

2,4%29与%281,3%29相同,但根据policy这些过载不参与过载解决,除非std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

移除是通过移动分配%29来完成的。范围中的元素的移动方式使不被移除的元素出现在范围的开头。保留元素的相对顺序,并且物理容器的大小不变。迭代器指向新元素之间的元素。逻辑物理范围的结束仍然是可取消引用的,但是元素本身有未指定的值%28MoveAssignable后状态%29。打电话给remove通常后面是对容器%27的调用。erase方法,它删除未指定的值并减少物理容器的大小以匹配其新的逻辑尺寸。

参数

first, last

-

the range of elements to process

value

-

the value of elements to remove

policy

-

the execution policy to use. See execution policy for details.

p

-

unary predicate which returns ?true if the element should be removed. The signature of the predicate function should be equivalent to the following: bool pred(const Type &a); The signature does not need to have const &, but the function must not modify the objects passed to it. The type Type must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type. ?

类型要求

---。

-取消引用前的类型必须符合可移动分配的要求。

-单数预测必须满足谓词的要求。

返回值

如果不是,则用于新值范围%28的过去结束迭代器。end,则指向未指定的值,迭代器指向此迭代器和end29%。

复杂性

一点儿没错std::distance(first, last)谓词的应用。

例外

带有名为ExecutionPolicy报告错误如下:

  • ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

注记

同名容器成员函数。list::remove,,,list::remove_if,,,forward_list::remove,和forward_list::remove_if删除删除的元素。

这些算法不能用于关联容器,例如std::setstd::map因为ForwardIt不取消对可移动赋值类型%28的引用,这些容器中的键不能修改为%29。

标准库还定义了std::removeconst char*,用于删除文件:std::remove...

可能的实施

第一版

*。

模板<类前进,类T>前进--删除%28 Forwardit First,Forwardit Lest,Const T&Value%29{First=std:查找%28first,Late,value%29;if%28first%21=last%29,%28 Forwardit i=first;++i%21=Lest;如果%28%21%28,查找%29%2AI==值%29%29%2A第一++=std::移动%28%2AI%29;先返回;}

第二版

模板<类向前,类预测>向前移除[医]如果%28 ForwardIt First,Forwardit Lest,UnaryPredicate p%29{first=std::find[医]如果%281,最后,p%29;如果%281%21=%28 Forwardit i=First;++i%21=Lest;如果%28,则%281%21=最后;如果%28%21 p%28,则%29%2AI%29%29%2A第一++=std::移动%28%2AI%29;先返回;}

实例

下面的代码移除字符串中的所有空格,方法是将所有非空格字符移至左侧,然后删除额外字符。这是一个例子擦除成语...

二次

代码语言:javascript
复制
#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
 
int main()
{
    std::string str1 = "Text with some   spaces";
    str1.erase(std::remove(str1.begin(), str1.end(), ' '),
               str1.end());
    std::cout << str1 << '\n';
 
    std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
    str2.erase(std::remove_if(str2.begin(), 
                              str2.end(),
                              [](char x){return std::isspace(x);}),
               str2.end());
    std::cout << str2 << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Textwithsomespaces
Textwithsomewhitespaces

二次

另见

remove_copyremove_copy_if

copies a range of elements omitting those that satisfy specific criteria (function template)

unique

removes consecutive duplicate elements in a range (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com