16.3.4. replace, replace_if, replace_copy and replace_copy_if

Figure 16.4 demonstrates replacing values from a sequence using algorithms replace, replace_if, replace_copy and replace_copy_if.


 1   // Fig. 16.4: fig16_04.cpp
 2   // Algorithms replace, replace_if, replace_copy and replace_copy_if.
 3   #include <iostream>
 4   #include <algorithm>
 5   #include <array>
 6   #include <iterator> // ostream_iterator
 7   using namespace std;
 8
 9   bool greater9( int ); // predicate function prototype
10
11   int main()
12   {
13      const size_t SIZE = 10;
14      array< int, SIZE > init =  = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 };
15      ostream_iterator< int > output( cout, " " );
16
17      array< int, SIZE >  a1( init ); // initialize with copy of init
18      cout << "a1 before replacing all 10s:    ";
19      copy( a1.cbegin(), a1.cend(), output );
20
21      // replace all 10s in a1 with 100        
22      replace( a1.begin(), a1.end(), 10, 100 );
23      cout << " a1 after replacing 10s with 100s:    ";
24      copy( a1.cbegin(), a1.cend(), output );
25
26      array< int, SIZE > a2( init ); // initialize with copy of init
27      array< int, SIZE > c1; // instantiate c1
28      cout << " a2 before replacing all 10s and copying:    ";
29      copy( a2.cbegin(), a2.cend(), output );
30
31      // copy from a2 to c1, replacing 10s with 100s              
32      replace_copy( a2.cbegin(), a2.cend(), c1.begin(), 10, 100 );
33      cout << " c1 after replacing all 10s in a2:    ";
34      copy( c1.cbegin(), c1.cend(), output );
35
36      array< int, SIZE > a3( init ); // initialize with copy of init
37      cout << " a3 before replacing values greater than 9:    ";
38      copy( a3.cbegin(), a3.cend(), output );
39
40      // replace values greater than 9 in a3 with 100   
41      replace_if( a3.begin(), a3.end(), greater9, 100 );
42      cout << " a3 after replacing all values greater"
43         << " than 9 with 100s:    ";
44      copy( a3.cbegin(), a3.cend(), output );
45
46      array< int, SIZE > a4( init ); // initialize with copy of init
47      array< int, SIZE > c2; // instantiate c2'
48      cout << " a4 before replacing all values greater "
49         << "than 9 and copying:    ";
50      copy( a4.cbegin(), a4.cend(), output );
51
52      // copy a4 to c2, replacing elements greater than 9 with 100         
53      replace_copy_if( a4.cbegin(), a4.cend(), c2.begin(), greater9, 100 );
54      cout << " c2 after replacing all values greater than 9 in v4:    ";
55      copy( c2.begin(), c2.end(), output );
56      cout << endl;
57   } // end main
58
59   // determine whether argument is greater than 9
60   bool greater9( int x )
61   {
62      return x > 9;
63   } // end function greater9


a1 before replacing all 10s:
   10 2 10 4 16 6 14 8 12 10
a1 after replacing 10s with 100s:
   100 2 100 4 16 6 14 8 12 100

a2 before replacing all 10s and copying:
   10 2 10 4 16 6 14 8 12 10
c1 after replacing all 10s in a2:
   100 2 100 4 16 6 14 8 12 100

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

a4 before replacing all values greater than 9 and copying:
   10 2 10 4 16 6 14 8 12 10
c2 after replacing all values greater than 9 in a4:
   100 2 100 4 100 6 100 8 100 100


Fig. 16.4. Algorithms replace, replace_if, replace_copy and replace_copy_if.

replace Algorithm

Line 22 uses the replace algorithm to replace all elements with the value 10 in the range a1.begin() up to, but not including, a1.end() with the new value 100. The iterators supplied as the first two arguments must be forward iterators so that the algorithm can modify the elements in the sequence.

replace_copy Algorithm

Line 32 uses the replace_copy algorithm to copy all elements in the range a2.cbegin() up to, but not including, a2.cend(), replacing all elements with the value 10 with the new value 100. The elements are copied into c1, starting at position c1.begin(). The iterators supplied as the first two arguments must be input iterators. The iterator supplied as the third argument must be an output iterator so that the element being copied can be assigned to the copy location. This function returns an iterator positioned after the last element copied into c1.

replace_if Algorithm

Line 41 uses the replace_if algorithm to replace all those elements from a3.begin() up to, but not including, a3.end() for which the unary predicate function greater9 returns true. Function greater9 (defined in lines 60–63) returns true if the value passed to it is greater than 9; otherwise, it returns false. The value 100 replaces each value greater than 9. The iterators supplied as the first two arguments must be forward iterators.

replace_copy_if Algorithm

Line 53 uses the replace_copy_if algorithm to copy all elements from a4.cbegin() up to, but not including, a4.cend(). Elements for which the unary predicate function greater9 returns true are replaced with the value 100. The elements are placed in c2, starting at position c2.begin(). The iterators supplied as the first two arguments must be input iterators. The iterator supplied as the third argument must be an output iterator so that the element being copied can be assigned to the copy location. This algorithm returns an iterator positioned after the last element copied into c2.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.56.45