I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com

replace_copy

Replaces elements that have a special value while copying the whole range
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The replace_copy() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#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

Description

Replace_copy algorithm makes a copy of a range and replaces all oldVal values by the value newVal into it. It uses operator= to make the copy and operator== to compare elements.

Return Value

The value return is an output iterator pointing to the position one past the final element in the destination range to where the altered sequence of elements is being copied.

Complexity

The complexity is linear; there are last - first comparisons for equality and last - first assignments.
Example:
Example - replace_copy algorithm
Problem
This program illustrates the functionality of replace_copy() algorithm.
Workings
#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;
}
Solution
Output:

The original vector vec1 data is:
0 1 2 3 4 5 6 7 8 9 7 7 7 7
The original random shuffle vector vec1 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 vector vec1 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 with the value 0 replacing the 7 is:
7 1 9 2 0 7 7 3 4 6 8 5 7 7 0
References