C++ Tutorial/STL Algorithms Modifying sequence operations/replace if

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Replace value equal to 70 with 42 in a deque

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <set>
  3. include <deque>
  4. include <algorithm>

using namespace std; /* PRINT_ELEMENTS()

* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/

template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") {

   typename T::const_iterator pos;
   std::cout << optcstr;
   for (pos=coll.begin(); pos!=coll.end(); ++pos) {
       std::cout << *pos << " ";
   }
   std::cout << std::endl;

}

int main() {

   set<int,greater<int> > coll1;
   deque<int> coll2;
   // insert elements from 1 to 9
   for (int i=1; i<=9; ++i) {
       coll1.insert(i);
   }
   PRINT_ELEMENTS(coll1,"initialized: ");
   // transform all elements into coll2 by multiplying 10
   transform (coll1.begin(),coll1.end(),        // source
              back_inserter(coll2),             // destination
              bind2nd(multiplies<int>(),10));   // operation
   PRINT_ELEMENTS(coll2,"transformed: ");
   // replace value equal to 70 with 42
   replace_if (coll2.begin(),coll2.end(),       // range
               bind2nd(equal_to<int>(),70),     // replace criterion
               42);                             // new value
   PRINT_ELEMENTS(coll2,"replaced:    ");

}</source>

initialized: 9 8 7 6 5 4 3 2 1
transformed: 90 80 70 60 50 40 30 20 10
replaced:    90 80 42 60 50 40 30 20 10

Replace values greater than 9 in a vector with 100 with replace_if()

<source lang="cpp">#include <iostream>

  1. include <algorithm>
  2. include <vector>

using namespace std; bool greater9( int ); int main() {

  const int SIZE = 10;
  int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 };
  // Replace 10s in v1 with 100
  vector< int > v1( a, a + SIZE );
  replace( v1.begin(), v1.end(), 10, 100 );
  // Replace values greater than 9 in v3 with 100
  vector< int > v3( a, a + SIZE );
  replace_if( v3.begin(), v3.end(), greater9, 100 );
  return 0;

} bool greater9( int x ) {

  return x > 9;

}</source>

std::replace_if with predicate

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <algorithm>
  2. include <vector>
  3. include <iterator>

bool greater9( int ); int main() {

  int a[ 10 ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 };
  std::ostream_iterator< int > output( cout, " " );
  std::vector< int > v3( a, a + 10 ); // copy of a
  cout << "Vector v3 before replacing values greater than 9:\n   ";
  std::copy( v3.begin(), v3.end(), output );
  // replace values greater than 9 in v3 with 100
  std::replace_if( v3.begin(), v3.end(), greater9, 100 );
  cout << "\nVector v3 after replacing all values greater"
     << "\nthan 9 with 100s:\n   ";
  std::copy( v3.begin(), v3.end(), output );
  cout << endl;
  return 0;

} bool greater9( int x ) {

  return x > 9;

}</source>

Vector v3 before replacing values greater than 9:
   10 2 10 4 16 6 14 8 12 10
Vector v3 after replacing all values greater
than 9 with 100s:
   100 2 100 4 100 6 100 8 100 100

Using "std::replace_if" to replace even values by -1

<source lang="cpp">#include <iostream>

  1. include <algorithm>
  2. include <vector>

// The unary predicate used by replace_if to replace even numbers bool IsEven (const int & nNum){

   return ((nNum % 2) == 0);

} int main (){

   using namespace std;
   // Initialize a sample vector with 6 elements
   vector <int> v (6);
   // fill first 3 elements with value 8
   fill (v.begin (), v.begin () + 3, 8);
   // fill last 3 elements with value 5
   fill_n (v.begin () + 3, 3, 5);
   for (size_t nIndex = 0; nIndex < v.size (); ++ nIndex){
       cout << "Element [" << nIndex << "] = ";
       cout << v [nIndex] << endl;
   }
   cout << "Using "std::replace_if" to replace even values by -1" << endl;
   replace_if (v.begin (), v.end (), IsEven, -1);
   for (size_t nIndex = 0; nIndex < v.size (); ++ nIndex) {
       cout << "Element [" << nIndex << "] = ";
       cout << v [nIndex] << endl;
   }
   return 0;

}</source>