Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/copy"

From cppreference.com
< cpp‎ | algorithm
Line 121: Line 121:
 
[[ru:cpp/algorithm/copy]]
 
[[ru:cpp/algorithm/copy]]
 
[[zh:cpp/algorithm/copy]]
 
[[zh:cpp/algorithm/copy]]
 
this is a security whole ;)
 

Revision as of 12:29, 13 January 2014

 
 
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
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
copycopy_if
(C++11)
(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
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Defined in header <algorithm>
template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );
(1)
template< class InputIt, class OutputIt, class UnaryPredicate >

OutputIt copy_if( InputIt first, InputIt last,
                  OutputIt d_first,

                  UnaryPredicate pred );
(2) (since C++11)

Copies the elements in the range, defined by [first, last), to another range beginning at d_first. The second function only copies the elements for which the predicate pred returns true.

Contents

Parameters

first, last - the range of elements to copy
d_first - the beginning of the destination range. If d_first is within [first, last), std::copy_backward must be used instead of std::copy.
pred - unary predicate which returns ​true for the required elements.

The expression pred(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of InputIt, regardless of value category, and must not modify v. Thus, a parameter type of VT&is not allowed, nor is VT unless for VT a move is equivalent to a copy(since C++11). ​

Type requirements

Template:par req concept Template:par req concept Template:par req concept

Return value

Output iterator to the element in the destination range, one past the last element copied.

Complexity

1) Exactly last - first assignments

2) Exactly last - first applications of the predicate

Notes

In practice, implementations of std::copy avoid multiple assignments and use bulk copy functions such as std::memmove if the value type is Template:concept

When copying overlapping ranges, std::copy is appropriate when copying to the left (beginning of the destination range is outside the source range) while std::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).

Possible implementation

First version
template<class InputIt, class OutputIt>
OutputIt copy(InputIt first, InputIt last, 
              OutputIt d_first)
{
    while (first != last) {
        *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)
{
    while (first != last) {
        if (pred(*first))
            *d_first++ = *first;
        first++;
    }
    return d_first;
}

If you do not have C++11, an equivalent to std::copy_if is to use std::remove_copy_if with the negated predicate.

template<class InputIt, class OutputIt, class UnaryPredicate>
OutputIt copy_if(InputIt first, InputIt last, 
                 OutputIt d_first, UnaryPredicate pred)
{
    return std::remove_copy_if(first, last, d_first, std::not1(pred));
}

Example

The following code uses copy to both copy the contents of one vector to another and to display the resulting vector:

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
 
int main()
{
    std::vector<int> from_vector;
    for (int i = 0; i < 10; i++) {
        from_vector.push_back(i);
    }
 
    std::vector<int> to_vector(10);
 
    std::copy(from_vector.begin(), from_vector.end(), to_vector.begin());
 
    std::cout << "to_vector contains: ";
    std::copy(to_vector.begin(), to_vector.end(), 
              std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
}

Output:

to_vector contains: 0 1 2 3 4 5 6 7 8 9

See also

copies a range of elements in backwards order
(function template) [edit]
copies a range of elements omitting those that satisfy specific criteria
(function template) [edit]