std::copy, std::copy_if

 

翻译:https://en.cppreference.com/w/cpp/algorithm/copy

定义在头文件 <algorithm>

函数声明

template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );    // C++ 20

template< class InputIt, class OutputIt >
constexpr OutputIt copy( InputIt first, InputIt last, OutputIt d_first ); // C++ 20

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 copy( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );    // C++ 17

template< class InputIt, class OutputIt, class UnaryPredicate >
OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,
                  UnaryPredicate pred );    //  C++ 11 ~ C++ 20

template< class InputIt, class OutputIt, class UnaryPredicate >
constexpr OutputIt copy_if( InputIt first, InputIt last,
                            OutputIt d_first,
                            UnaryPredicate pred );    // C++ 20

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate >
ForwardIt2 copy_if( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first,
                    UnaryPredicate pred );    // C++ 17

 

函数解释:

复制 [first, last) 范围内的元素到 d_first 开始中

1) 元素复制的范围是 [first,last),是从 first 开始,但是不包括 last。如果 d_first 这个位置是包含在 [first,last)中的话,那么这个行为(指的是复制这个动作)是未定义的。在这种情况下,考虑使用 std::copy_backward。

3)当 pred 返回是真时,才会复制元素。元素的复制相对的顺序是收到保护的,如果复制的源范围和目的范围有重叠的话,那么该行为是未定义的。

2)4)和 1)3)相同。。。。

 

函数参数:

first,last -- 复制的元素范围

d_first:复制的目的范围的起始位置

policy:使用执行策略

pred:当返回值为真时,才进行复制动作。

 

返回值:

返回带有复制结果的 OutputIt iterator。

 

函数实现:

// first version
template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, 
              OutputIt d_first)
{
    for (; first != last; (void)++first, (void)++d_first) {
        *d_first = *first;
    }
    return d_first;
}
// second version
template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
{
    for (; first != last; ++first) {
        if (pred(*first)) {
            *d_first = *first;
            ++d_first;
        }
    }
    return d_first;
}

 

例子:

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
 
int main()
{
    std::vector<int> from_vector(10);
    std::iota(from_vector.begin(), from_vector.end(), 0);
 
    std::vector<int> to_vector;
    std::copy(from_vector.begin(), from_vector.end(),
              std::back_inserter(to_vector));
// or, alternatively,
//  std::vector<int> to_vector(from_vector.size());
//  std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
// either way is equivalent to
//  std::vector<int> to_vector = from_vector;
 
    std::cout << "to_vector contains: ";
 
    std::copy(to_vector.begin(), to_vector.end(),
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << '\n';
 
    std::cout << "odd numbers in to_vector are: ";
 
    std::copy_if(to_vector.begin(), to_vector.end(),
                 std::ostream_iterator<int>(std::cout, " "),
                 [](int x) { return x % 2 != 0; });
    std::cout << '\n';
 
    std::cout << "to_vector contains these multiples of 3:\n";
 
    to_vector.clear();
    std::copy_if(from_vector.begin(), from_vector.end(),
                 std::back_inserter(to_vector),
                 [](int x) { return x % 3 == 0; });
 
    for (int x : to_vector)
        std::cout << x << ' ';
    std::cout << '\n';
}

 

 

输出:

to_vector contains: 0 1 2 3 4 5 6 7 8 9
odd numbers in to_vector are: 1 3 5 7 9
to_vector contains these multiples of 3:
0 3 6 9
posted @ 2022-06-06 15:44  王清河  阅读(663)  评论(0编辑  收藏  举报