Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib replace_copy_if
Personal tools
Document Actions

replace_copy_if



Click on the banner to return to the class reference home page.

replace_copy_if


Algorithm

Summary

Substitutes elements stored in a collection with new values.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

#include <algorithm>
template <class InputIterator,
          class OutputIterator,
          class Predicate,
          class T>
OutputIterator replace_copy_if (InputIterator first,
                                InputIterator last,
                                OutputIterator result,
                                Predicate pred,
                                const T& new_value);

Description

The replace_copy_if algorithm leaves the original sequence intact and places a revised sequence into result. The algorithm compares each element *i in the range [first,last) with the conditions specified by pred. If pred(*i)==false, replace_copy_if copies *i to result+(first-i). If pred(*i)==true, then replace_copy copies new_value to result+(first-i). replace_copy_if returns result+(last-first).

Complexity

Exactly last - first applications of the predicate are performed.

Example

//
// replace.cpp
//
 #include <algorithm>
 #include <vector>
 #include <iterator>
 #include <iostream.h>

 template<class Arg>
 struct all_true : public unary_function<Arg, bool>
 {
   bool operator() (const Arg&) { return 1; }
 };

 int main ()
 {
   //
   // Initialize a vector with an array of integers.
   //
   int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
   vector<int> v(arr+0, arr+10);
   //
   // Print out original vector.
   //
   cout << "The original list: " << endl << "     ";
   copy(v.begin(), v.end(), ostream_iterator<int,char>(cout," "));
   cout << endl << endl;
   //
   // Replace the number 7 with 11.
   //
   replace(v.begin(), v.end(), 7, 11);
   //
   // Print out vector with 7 replaced. 
   //
   cout << "List after replace:" << endl << "     ";
   copy(v.begin(), v.end(), ostream_iterator<int,char>(cout," "));
   cout << endl << endl;
   //
   // Replace 1 2 3 with 13 13 13.
   //
   replace_if(v.begin(), v.begin()+3, all_true<int>(), 13);
   //
   // Print out the remaining vector.
   //
   cout << "List after replace_if:" << endl << "     ";
   copy(v.begin(), v.end(), ostream_iterator<int,char>(cout," "));
   cout << endl << endl;
   //
   // Replace those 13s with 17s on output.
   //
   cout << "List using replace_copy to cout:" << endl << "     ";
   replace_copy(v.begin(), v.end(), 
                ostream_iterator<int,char>(cout, " "), 13, 17);
   cout << endl << endl;
   //
   // A simple example of replace_copy_if.
   //
   cout << "List w/ all elements output as 19s:" << endl << "   ";
   replace_copy_if(v.begin(), v.end(), 
                   ostream_iterator<int,char>(cout, " "),
                   all_true<int>(), 19);
   cout << endl;

   return 0;
 }
Output :
The original list:
     1 2 3 4 5 6 7 8 9 10
List after replace:
     1 2 3 4 5 6 11 8 9 10
List after replace_if:
     13 13 13 4 5 6 11 8 9 10
List using replace_copy to cout:
     17 17 17 4 5 6 11 8 9 10
List with all elements output as 19s:
     19 19 19 19 19 19 19 19 19 19

Warning

If your compiler does not support default template parameters, then you need to always supply the Allocator template argument. For instance, you will need to write :

vector<int, allocator<int> >

instead of :

vector<int>

See Also

replace, replace_if, replace_copy


©Copyright 1996, Rogue Wave Software, Inc.


Powered by Plone This site conforms to the following standards: