Or login with:
#include <algorithm> template < class ForwardIterator, class Predicate, class Type > void replace_if( ForwardIterator first, ForwardIterator last, Predicate pred, const Type& val );Parameters:
| Parameter | Description |
|---|---|
| first | A forward iterator pointing to the position of the first element in the range from which elements are being replaced |
| last | An iterator pointing to the position one past the final element in the range from which elements are being replaced |
| pred | The unary predicate that must be satisfied is the value of an element is to be replaced |
| val | The new value being assigned to the elements whose old value satisfies the predicate |
pred predicate instead of operator==.last - first applications of pred and at most last - first assignments.#include <iostream> #include <vector> #include <algorithm> using namespace std; /* Determines if an integer is divisible by 3. n contains an integer. Returns true if n is divisible by 3, and otherwise false. */ bool isDivisibleByThree ( int n //in ) { return (n%3 == 0); } int main() { int a[] = {1, 2, 2, 3, 4, 5, 2, 6}; vector<int> v(a, a+8); cout <<"\nHere are the values in the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; cout <<"\nNow we replace all values divisble by 3 with 123."; replace_if(v.begin(), v.end(), isDivisibleByThree, 123); cout <<"\nHere are the revised contents of the vector:\n"; for (vector<int>::size_type i=0; i<v.size(); i++) cout <<v.at(i)<<" "; return 0; }
You must login to leave a messge