C++标准模板(STL)(std::remove_copy, std::remove_copy_if)

定义于头文件 <algorithm>

算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

复制一个范围的元素,忽略满足特定判别标准的元素

std::remove_copy,
std::remove_copy_if
template< class InputIt, class OutputIt, class T >

OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first,

                      const T& value );
(1)(C++20 前)
template< class InputIt, class OutputIt, class T >

constexpr OutputIt remove_copy( InputIt first, InputIt last, OutputIt d_first,

                                const T& value );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T >

ForwardIt2 remove_copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                        ForwardIt2 d_first, const T& value );
(2)(C++17 起)
template< class InputIt, class OutputIt, class UnaryPredicate >

OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first,

                         UnaryPredicate p );
(3)(C++20 前)
template< class InputIt, class OutputIt, class UnaryPredicate >

constexpr OutputIt remove_copy_if( InputIt first, InputIt last, OutputIt d_first,

                                   UnaryPredicate p );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate >

ForwardIt2 remove_copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                           ForwardIt2 d_first, UnaryPredicate p );
(4)(C++17 起)

 复制来自范围 [first, last) 的元素到始于 d_first 的另一范围,省略满足特定判别标准的元素。源与目标范围不能重叠。

1) 忽略所有等于 value 的元素。

3) 忽略所有谓词 p 对其返回 true 的元素。

2,4) 同 (1,3) ,但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议。

参数

first, last-要复制的元素范围
d_first-目标范围的起始。
value-不复制的元素的值
policy-所用的执行策略。细节见执行策略。
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
- ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- UnaryPredicate 必须满足谓词 (Predicate) 的要求。

返回值

指向最后被复制元素的迭代器。

复杂度

准确应用 last - first 次谓词。

对拥有 ExecutionPolicy 的重载,若 ForwardIt1 的 value_type 非可移动构造 (MoveConstructible) 则有性能开销。

异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

  • 若作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。
  • 若算法无法分配内存,则抛出 std::bad_alloc 。

可能的实现 

版本一

template<class InputIt, class OutputIt, class T>
OutputIt remove_copy(InputIt first, InputIt last,
                     OutputIt d_first, const T& value)
{
    for (; first != last; ++first) {
        if (!(*first == value)) {
            *d_first++ = *first;
        }
    }
    return d_first;
}

版本二

template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt remove_copy_if(InputIt first, InputIt last,
                        OutputIt d_first, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (!p(*first)) {
            *d_first++ = *first;
        }
    }
    return d_first;
}

调用示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>

struct Cell
{
    int x;
    int y;
    Cell &operator+=(Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    bool operator ==(const Cell &cell)
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    auto func1 = []()
    {
        static Cell cell = {-2, -1};
        cell.x += 2;
        cell.y += 2;
        return cell;
    };

    std::vector<Cell> cells(5);
    std::generate_n(cells.begin(), cells.size(), func1);

    Cell cell{4, 5};
    cells.push_back(cell);

    std::cout << "original   vector<Cell>:    ";
    std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::vector<Cell> cells_copy;
    // 1) 忽略所有等于 value 的元素。
    std::remove_copy(cells.begin(), cells.end(), std::back_inserter(cells_copy), cell);

    std::cout << "cells_copy vector<Cell>:    ";
    std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "cells_copy vector<Cell>:    ";
    std::copy(cells_copy.begin(), cells_copy.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl << std::endl;


    auto func2 = [](const Cell & ocell, const Cell & cell)
    {
        return ocell.x == cell.x && ocell.y == cell.y;
    };

    std::generate_n(cells.begin(), cells.size(), func1);
    cell = {16, 17};
    std::cout << "original   vector<Cell>:    ";
    std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    cells_copy.clear();

    //3) 忽略所有谓词 p 对其返回 true 的元素。
    std::remove_copy_if(cells.begin(), cells.end(), std::back_inserter(cells_copy),
                        std::bind(func2, cell, std::placeholders::_1));

    std::cout << "cells_copy vector<Cell>:    ";
    std::copy(cells.begin(), cells.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "cells_copy vector<Cell>:    ";
    std::copy(cells_copy.begin(), cells_copy.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
}

输出

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这些都是C++ STL中的算法,用于对容器中的元素进行处理。下面分别给出每个算法的用法和示例: 1. std::remove: 从容器中删除指定的元素 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 2, 4, 2, 5}; auto it = std::remove(vec.begin(), vec.end(), 2); // 删除所有值为2的元素 vec.erase(it, vec.end()); // 删除多余的元素 for (auto x : vec) { std::cout << x << " "; // 输出:1 3 4 5 } return 0; } ``` 2. std::copy_if: 将符合条件的元素复制到新的容器中 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; std::vector<int> odd_vec; std::copy_if(vec.begin(), vec.end(), std::back_inserter(odd_vec), [](int x) { return x % 2 == 1; }); // 复制所有奇数到odd_vec中 for (auto x : odd_vec) { std::cout << x << " "; // 输出:1 3 5 } return 0; } ``` 3. std::partition: 将容器中的元素按照条件分为两部分,满足条件的在前面,不满足条件的在后面 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; auto it = std::partition(vec.begin(), vec.end(), [](int x) { return x % 2 == 1; }); // 将容器中的元素按照奇偶分为两部分 for (auto x : vec) { std::cout << x << " "; // 输出:1 5 3 4 2 } return 0; } ``` 4. std::unique: 删除容器中相邻的重复元素 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5}; auto it = std::unique(vec.begin(), vec.end()); // 删除相邻的重复元素 vec.erase(it, vec.end()); // 删除多余的元素 for (auto x : vec) { std::cout << x << " "; // 输出:1 2 3 4 5 } return 0; } ``` 5. std::unique_copy: 将容器中相邻的重复元素复制到新的容器中 ```cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 2, 3, 3, 3, 4, 5, 5}; std::vector<int> unique_vec; std::unique_copy(vec.begin(), vec.end(), std::back_inserter(unique_vec)); // 将相邻的重复元素复制到unique_vec中 for (auto x : unique_vec) { std::cout << x << " "; // 输出:1 2 3 4 5 } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值