Or login with:
#include <algorithm> template < class InputIterator, class OutputIterator, class Type > OutputIterator replace_copy( InputIterator first, InputIterator last, OutputIterator result, const Type& oldVal, const Type& newVal );Parameters:
| Parameter | Description |
|---|---|
| first | An input iterator pointing to the position of the first element in the range from which elements are being replaced |
| last | An input iterator pointing to the position one past the final element in the range from which elements are being replaced |
| result | An output iterator pointing to the first element in the destination range to where the altered sequence of elements is being copied |
| oldVal | The old value of the elements being replaced |
| newVal | The new value being assigned to the elements with the old value |
oldVal values by the value newVal into it. It uses operator= to make the copy and operator== to compare elements.last - first comparisons for equality and last - first assignments.#include <vector> #include <list> #include <algorithm> #include <iostream> using namespace std; int main() { vector <int> vec1; list <int> lst1 (15); vector <int>::iterator Iter1; list <int>::iterator lstIter; int i,j; for (i = 0; i <= 9; i++) vec1.push_back(i); for (j = 0; j <= 3; j++) vec1.push_back(7); cout <<"The original vector vec1 data is:\n"; for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout <<*Iter1<<" "; cout <<endl; random_shuffle(vec1.begin(), vec1.end()); int k; for (k = 0; k <= 15; k++) vec1.push_back(1); cout <<"The original random shuffle vector vec1 with appended data is:\n"; for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout <<*Iter1<<" "; cout <<endl; // replace elements in one part of a vector with a value of 7 // with a value of 70 and copy into another part of the vector replace_copy(vec1.begin(), vec1.begin() + 14, vec1.end( )-15, 7, 70); cout <<"The vector vec1 data with a replacement value of 7 is:\n"; for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++) cout <<*Iter1<<" "; cout <<endl; // replace elements in a vector of a value 70 with a value of 1 and copy into a list replace_copy(vec1.begin(), vec1.begin() + 14, lst1.begin(), 70, 1); cout <<"The list copy lst1 of vec1 with the value 0 replacing the 7 is:\n"; for (lstIter = lst1.begin(); lstIter != lst1.end(); lstIter++) cout <<*lstIter<<" "; cout <<endl; return 0; }
You must login to leave a messge