C++ STL algorithm, replace_copy() of the randomly shuffled vector, copy into a list code 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() to examine each element in a source range and replaces it if it matches a specified value while copying the result into a new destination range in C++ programming

To show: How to use the C++ algorithm, replace_copy() to examine each element in a source range and replaces it if it matches a specified value while copying the result into a new destination range in C++ programming

 

// C++ STL algorithm, replace_copy()

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

 

int main(void)

{

// vector and list containers

vector <int> vec1;

list <int> lst1 (15);

// vector and list iterators

vector <int>::iterator Iter1;

list <int>::iterator lstIter;

int i, j, k;

 

// push the 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());

// push the data in range

for (k = 0; k <= 15; k++)

vec1.push_back(1);

cout<<"\nThe original vec1 vector randomly shuffled with appended data is: ";

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<<"\nThe vec1 vector data with a replacement value of 7 is: ";

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<<"\nThe list copy, lst1 of vec1 vector with the value 0 replacing the 7 is: ";

for(lstIter = lst1.begin(); lstIter != lst1.end(); lstIter++)

cout<<*lstIter<<" ";

cout<<endl;

}

 

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 with appended data is: 7 1 9 2 0 7 7 3 4 6 8 5 7 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

The vec1 vector data with a replacement value of 7 is: 7 1 9 2 0 7 7 3 4 6 8 5 7 7 1 70 1 9 2 0 70 70 3 4 6 8 5 70 70 1

The list copy, lst1 of vec1 vector with the value 0 replacing the 7 is: 7 1 9 2 0 7 7 3 4 6 8 5 7 7 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index