C++ STL algorithm, replace_copy_if() program sample
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the C++ replace_copy_if() to examine each element in a source range and replaces it if it satisfies a specified predicate while copying the result into a new destination range in C++ programming
To show: How to use the C++ algorithm, replace_copy_if() to examine each element in a source range and replaces it if it satisfies a specified predicate while copying the result into a new destination range in C++ programming
// C++ STL algorithm, replace_copy_if()
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
bool greaterthan(int value)
{return value > 5;}
int main(void)
{
// vector and list containers
vector <int> vec1;
list <int> lst1 (13);
// vector and list iterators
vector <int>::iterator Iter1;
list <int>::iterator lstIter1;
int i, j, k;
// push data in range
for (i = 0; i <= 9; i++)
vec1.push_back(i);
for (j = 0; j <= 3; j++)
vec1.push_back(7);
// print the data
cout<<"The original vec1 vector data is: ";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
// shuffle the data randomly
random_shuffle(vec1.begin(), vec1.end());
for(k = 0; k <= 13; k++)
vec1.push_back(3);
cout<<"\nThe original vec1 vector randomly shuffled data with appended data is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
// replace elements with a value of 7 in the 1st half of a vector with a value of 72 and copy it into the 2nd half of the vector
replace_copy_if(vec1.begin(), vec1.begin() + 14, vec1.end()-14, greaterthan, 72);
cout<<"\nThe vec1 vector with values of 72 replacing those greater"
<<"\n than 5 in the 1st half & copied into the 2nd half is:\n";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
// replace elements in a vector with a value of 72 with a value of -8 and copy into a list
replace_copy_if(vec1.begin(), vec1.begin() + 13, lst1.begin(), greaterthan, -8);
cout<<"\nA list copy of vec1 vector with the value -8\n replacing "
<<"those greater than 5 is: ";
for(lstIter1 = lst1.begin(); lstIter1 != lst1.end(); lstIter1++)
cout<<*lstIter1<<" ";
cout<<endl;
return 0;
}
Output examples:
The original vec1 vector data is: 0 1 2 3 4 5 6 7 8 9 7 7 7 7
The original vec1 vector randomly shuffled data with appended data is:
7 1 9 2 0 7 7 3 4 6 8 5 7 7 3 3 3 3 3 3 3 3 3 3 3 3 3 3
The vec1 vector with values of 72 replacing those greater
than 5 in the 1st half & copied into the 2nd half is:
7 1 9 2 0 7 7 3 4 6 8 5 7 7 72 1 72 2 0 72 72 3 4 72 72 5 72 72
A list copy of vec1 vector with the value -8
replacing those greater than 5 is: -8 1 -8 2 0 -8 -8 3 4 -8 -8 5 -8
Press any key to continue . . .