replace man page on SunOS

Man page or keyword search:  
man Server   20652 pages
apropos Keyword Search (all sections)
Output format
SunOS logo
[printable version]

replace(3C++)			       -			 replace(3C++)

Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.

NAME
       replace

	- Substitutes elements in a collection with new values.

SYNOPSIS
       #include <algorithm>
       template <class ForwardIterator, class T>
       void replace (ForwardIterator first,
	     ForwardIterator last,
	     const T& old_value,
	     const T& new_value);

DESCRIPTION
       For  the	 range	[first, last), the replace algorithm replaces elements
       referred to by iterator i with new_value, when the following  condition
       holds: *i == old_value.

COMPLEXITY
       Exactly	last  - first comparisons or applications of the corresponding
       predicate are done.

EXAMPLE
//
// replace.cpp
//
 #include <algorithm>
 #include <vector>
 #include <iterator>
 #include <iostream>
using namespace std;

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, 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,
   // s.b. 1 2 3 4 5 6 11 8 9 10
  cout << "List after replace " << endl << "	 ";
  copy(v.begin(),v.end(),o
       stream_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,
   // s.b. 13 13 13 4 5 6 11 8 9 10
  cout << "List after replace_if " << endl << "	    ";
  copy(v.begin(),v.end(),
       ostream_iterator<int,char>(cout," "));
  cout << endl << endl;

  return 0;
 }

Program 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

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

       vector<int, allocator<int> >

       instead of:

       vector<int>

       If your compiler does not support namespaces, then you do not need  the
       using declaration for std.

SEE ALSO
       replace_if, replace_copy, replace_copy_if

Rogue Wave Software		  02 Apr 1998			 replace(3C++)
[top]

List of man pages available for SunOS

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net