Namespaces
Variants

std::ranges:: remove, std::ranges:: remove_if

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
(1)
template < std:: permutable I, std:: sentinel_for < I > S,

class T, class Proj = std:: identity >
requires std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >
constexpr ranges:: subrange < I >

remove ( I first, S last, const T & value, Proj proj = { } ) ;
(C++20 起)
(C++26 前)
template < std:: permutable I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
class T = std :: projected_value_t < I, Proj > >
requires std:: indirect_binary_predicate
< ranges:: equal_to , std :: projected < I, Proj > , const T * >
constexpr ranges:: subrange < I >

remove ( I first, S last, const T & value, Proj proj = { } ) ;
(C++26 起)
(2)
template < ranges:: forward_range R,

class T, class Proj = std:: identity >
requires std:: permutable < ranges:: iterator_t < R >> &&
std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr ranges:: borrowed_subrange_t < R >

remove ( R && r, const T & value, Proj proj = { } ) ;
(C++20 起)
(C++26 前)
template < ranges:: forward_range R,

class Proj = std:: identity ,
class T = std :: projected_value_t < ranges:: iterator_t < R > , Proj > >
requires std:: permutable < ranges:: iterator_t < R >> &&
std:: indirect_binary_predicate
< ranges:: equal_to ,
std :: projected < ranges:: iterator_t < R > , Proj > , const T * >
constexpr ranges:: borrowed_subrange_t < R >

remove ( R && r, const T & value, Proj proj = { } ) ;
(C++26 起)
template < std:: permutable I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
constexpr ranges:: subrange < I >

remove_if ( I first, S last, Pred pred, Proj proj = { } ) ;
(3) (C++20 起)
template < ranges::<

从范围 [ first , last ) 中移除所有满足特定条件的元素,并返回子范围 [ ret , last ) ,其中 ret 是指向范围新末尾的尾后迭代器。

1) 移除所有等于 value 的元素,使用 std:: invoke ( proj, * i ) == value 进行比较。
3) 移除所有满足 std:: invoke ( pred, std:: invoke ( proj, * i ) ) 返回 true 的元素。
2,4) (1,3) 相同,但使用 r 作为范围,如同以 ranges:: begin ( r ) 作为 first ,并以 ranges:: end ( r ) 作为 last

移除操作通过(借助移动赋值)移动区间内的元素来实现,使得不需要被移除的元素出现在区间的起始位置。保留元素的相对顺序保持不变,且容器的 物理 大小不变。指向新区间 逻辑 末尾与 物理 末尾之间元素的迭代器仍然可解引用,但元素本身具有未指定的值(符合 MoveAssignable 的后置条件)。

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待处理元素范围的 迭代器-哨位
r - 待处理的元素范围
value - 待移除元素的值
pred - 应用于投影元素的谓词
proj - 应用于元素的投影

返回值

{ ret, last } ,其中 [ first , ret ) 是移除操作后得到的子范围,而子范围 [ ret , last ) 中的所有元素均处于有效但未指定的状态,即 [ ret , last ) 是需要被擦除的子范围。

复杂度

恰好应用对应的谓词和任何投影 N 次,其中 N = ranges:: distance ( first, last ) ,且在最坏情况下进行 N - 1 次移动操作。

注释

ranges::remove 的调用通常紧随容器 erase 成员函数的调用,后者会擦除未指定的值并将容器的 物理 大小缩减至与其新 逻辑 大小相匹配。这两个调用共同构成了所谓的 擦除-删除惯用法 ,该惯用法可通过自由函数 std::erase (为所有标准 序列 容器提供 重载 )或 std::erase_if (为 所有 标准容器提供 重载 )来实现。

名称相似的容器 成员函数 list::remove list::remove_if forward_list::remove forward_list::remove_if 会擦除被移除的元素。

这些算法通常不能用于关联容器(例如 std::set std::map ),因为它们的迭代器类型解引用后不能得到 MoveAssignable 类型(这些容器中的键是不可修改的)。

由于 ranges::remove 接受 value 的引用参数,若该参数是范围 [ first , last ) 中元素的引用,可能导致预期外的行为。

可能的实现

remove (1,2)
struct remove_fn
{
    template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
             class T = std::projected_value_t<I, Proj>>
    requires std::indirect_binary_predicate
                 <ranges::equal_to, std::projected<I, Proj>, const T*>
    constexpr ranges::subrange<I>
        operator()(I first, S last, const T& value, Proj proj = {}) const
    {
        first = ranges::find(std::move(first), last, value, proj);
        if (first != last)
        {
            for (I i{std::next(first)}; i != last; ++i)
                if (value != std::invoke(proj, *i))
                {
                    *first = ranges::iter_move(i);
                    ++first;
                }
        }
        return {first, last};
    }
    template<ranges::forward_range R, class Proj = std::identity,
             class T = std::projected_value_t<ranges::iterator_t<R>, Proj>>
    requires std::permutable<ranges::iterator_t<R>> &&
             std::indirect_binary_predicate
                 <ranges::equal_to,
                  std::projected<ranges::iterator_t<R>, Proj>, const T*>
    constexpr ranges::borrowed_subrange_t<R>
        operator()(R&& r, const T& value, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), value, std::move(proj));
    }
};
inline constexpr remove_fn remove {};
remove_if (3,4)
struct remove_if_fn
{
    template<std::permutable I, std::sentinel_for<I> S, class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
    constexpr ranges::subrange<I>
        operator()(I first, S last, Pred pred, Proj proj = {}) const
    {
        first = ranges::find_if(std::move(first), last, pred, proj);
        if (first != last)
        {
            for (I i{std::next(first)}; i != last; ++i)
                if (!std::invoke(pred, std::invoke(proj, *i)))
                {
                    *first = ranges::iter_move(i);
                    ++first;
                }
        }
        return {first, last};
    }
    template<ranges::forward_range R, class Proj = std::identity,
             std::indirect_unary_predicate
                 <std::<span

注解

功能测试 标准 功能
__cpp_lib_algorithm_default_value_type 202403 (C++26) 列表初始化 用于算法 ( 1,2 )

示例

#include <algorithm>
#include <cassert>
#include <complex>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
int main()
{
    std::string v1{"No - Diagnostic - Required"};
    std::cout << std::quoted(v1) << " (v1, 大小: " << v1.size() << ")\n";
    const auto ret = std::ranges::remove(v1, ' ');
    std::cout << std::quoted(v1) << " (v1 执行 `remove` 后, 大小: " << v1.size() << ")\n";
    std::cout << ' ' << std::string(std::distance(v1.begin(), ret.begin()), '^') << '\n';
    v1.erase(ret.begin(), ret.end());
    std::cout << std::quoted(v1) << " (v1 执行 `erase` 后, 大小: " << v1.size() << ")\n\n";
    // 使用自定义一元谓词的 remove_if:
    auto rm = [](char c) { return !std::isupper(c); };
    std::string v2{"Substitution Failure Is Not An Error"};
    std::cout << std::quoted(v2) << " (v2, 大小: " << v2.size() << ")\n";
    const auto [first, last] = std::ranges::remove_if(v2, rm);
    std::cout << std::quoted(v2) << " (v2 执行 `remove_if` 后, 大小: " << v2.size() << ")\n";
    std::cout << ' ' << std::string(std::distance(v2.begin(), first), '^') << '\n';
    v2.erase(first, last);
    std::cout << std::quoted(v2) << " (v2 执行 `erase` 后, 大小: " << v2.size() << ")\n\n";
    // 创建指向被 `remove_if` 修改容器的视图:
    for (std::string s : {"Small Object Optimization", "Non-Type Template Parameter"})
        std::cout << std::quoted(s) << " => "
            << std::string_view{begin(s), std::ranges::remove_if(s, rm).begin()} << '\n';
    std::vector<std::complex<double>> nums{{2, 2}, {1, 3}, {4, 8}};
    #ifdef __cpp_lib_algorithm_default_value_type
        auto e = std::ranges::remove(nums, {1, 3}); // T 被自动推导
    #else
        auto e = std::ranges::remove(nums, std::complex&lt

参见

复制范围元素并忽略满足特定条件的元素
(算法函数对象)
移除范围中连续的重复元素
(算法函数对象)
移除满足特定条件的元素
(函数模板)