Or login with:
#include <algorithm> template < class ForwardIterator, class Type > void replace( ForwardIterator first, ForwardIterator last, const Type& oldVal, const Type& newVal );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 | A forward iterator pointing to the position one past the final element in the range from which elements are being replaced |
| oldVal | The old value of the elements being replaced |
| newVal | The new value being assigned to the elements with the old value |
oldVal (using operator== of the iterator) by the value newVal in a range. The replacement is done by using operator= of the iterator.(last - first) comparisons for equality and at most (last - first) assignments of new values.#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a[] = {1, 2, 2, 3, 4, 5, 2, 3}; 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 copies of the value 2 with 222."; replace(v.begin(), v.end(), 2, 222); 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