Original address: http://blog.csdn.net/qq844352155/article/details/39136169function template <algorithm> STD: copy_if
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred);
Copy certain elements of range
Copies the elements in the range[first,last)
For whichPREDReturnstrue
To the range beginningResult.
Copy the elements that meet the requirements (call Pred to return true to the element) to the target array.
The behavior of this function template is equivalent:
12345678910111213
|
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred){ while (first!=last) { if (pred(*first)) { *result = *first; ++result; } ++first; } return result;}
|
|
Parameters
-
First, last
-
Input iterators to the initial and final positions in a sequence. The range copied is
[first,last)
, Which contains all the elements FirstAnd Last, Including the element pointed FirstBut not the element pointed Last.
InputiteratorShall point to a type assignable to the elements pointed Outputiterator.
The range of the sequence to be copied.
-
Result
-
Output iterator to the initial position of the range where the resulting sequence is stored. The range between des as specified elements
[first,last)
.
Start to overwrite.
-
PRED
-
Unary function that accepts an element in the range as argument, and returns a value convertible
bool
. The value returned indicates whether the element is to be copied (if
true
, It is copied ).
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
A one-dimensional function that accepts a parameter and returns a bool value.
The ranges shall not overlap.
Return valuean iterator pointing to the element that follows the last element written in the result sequence.
Returns an iterator pointing to the last overwritten element and the next element.
Example:
# Include <iostream> # include <algorithm> # include <vector> # include <array> using namespace STD; void copyif () {vector <int> V1, 9,10, 14,15}; vector <int> V2 {99,88}; cout <"at first, V2 ="; for (Int & I: V2) cout <I <""; cout <Endl; v2.resize (10); // resize (10 )!! Auto it = copy_if (v1.begin (), v1.end (), v2.end ()-5, [] (int I) {return I % 2 = 0 ;}); // if it is an even number, true cout <"after copy_if (v1.begin (), v1.end (), v2.end ()-5, [] (int I) is returned) {return I % 2 = 0 ;}) "<Endl; cout <" V2 = "; for (Int & I: V2) cout <I <"; cout <Endl; cout <" The return it is: * It = "<* It <Endl ;}
Run: (V2 resize (10) before copy_if ))
Example
12345678910111213141516171819
|
// copy_if example#include <iostream> // std::cout#include <algorithm> // std::copy_if, std::distance#include <vector> // std::vectorint main () { std::vector<int> foo = {25,15,5,-5,-15}; std::vector<int> bar (foo.size()); // copy only positive numbers: auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} ); bar.resize(std::distance(bar.begin(),it)); // shrink container to new size std::cout << "bar contains:"; for (int& x: bar) std::cout << ‘ ‘ << x; std::cout << ‘\n‘; return 0;}
|
Edit & run |
Output:
Complexitylinear in the distance FirstAnd Last: Applies PREDTo each element in the range and performs at most that has assignments.
Data racesthe objects in the range
[first,last)
Are accessed.
The objects in the range ResultAnd the returned value are modified.
Predictionsthrows if any PRED, The element assignments or the operations on iterators throws.
Note that invalid arguments causeUndefined behavior.
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: [email protected]
Yu gdut
------------------------------------------------------------------
STL algorithm copy_if (8)