16.3.3. remove, remove_if, remove_copy and remove_copy_if

Figure 16.3 demonstrates removing values from a sequence with algorithms remove, remove_if, remove_copy and remove_copy_if.


 1   // Fig. 16.3: fig16_03.cpp
 2   // Algorithms remove, remove_if, remove_copy and remove_copy_if.
 3   #include <iostream>
 4   #include <algorithm> // algorithm definitions
 5   #include <array> // array class-template definition
 6   #include <iterator> // ostream_iterator
 7   using namespace std;
 8
 9   bool greater9( int ); // 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 removing all 10s:    ";
19      copy( a1.cbegin(), a1.cend(), output );
20
21      // remove all 10s from a1                                
22      auto newLastElement = remove( a1.begin(), a1.end(), 10 );
23      cout << "a1 after removing all 10s:    ";
24      copy( a1.begin(), newLastElement, output );
25
26      array< int, SIZE > a2( init ); // initialize with copy of init
27      array< int, SIZE > c = { 0 }; // initialize to 0s
28      cout << " a2 before removing all 10s and copying:    ";
29      copy( a2.cbegin(), a2.cend(), output );
30
31      // copy from a2 to c, removing 10s in the process    
32      remove_copy( a2.cbegin(), a2.cend(), c.begin(), 10 );
33      cout << " c after removing all 10s from a2:    ";
34      copy( c.cbegin(), c.cend(), output );
35
36      array< int, SIZE > a3( init ); // initialize with copy of init
37      cout << " a3 before removing all elements greater than 9:    ";
38      copy( a3.cbegin(), a3.cend(), output );
39
40      // remove elements greater than 9 from a3                    
41      newLastElement = remove_if( a3.begin(), a3.end(), greater9 );
42      cout << " a3 after removing all elements greater than 9:    ";
43      copy( a3.begin(), newLastElement, output );
44
45      array< int, SIZE > a4( init ); // initialize with copy of init
46      array< int, SIZE > c2 = { 0 }; // initialize to 0s
47      cout << " a4 before removing all elements"
48         << " greater than 9 and copying:    ";
49      copy( a4.cbegin(), a4.cend(), output );
50
51      // copy elements from a4 to c2, removing elements greater      
52      // than 9 in the process                                       
53      remove_copy_if( a4.cbegin(), a4.cend(), c2.begin(), greater9 );
54      cout << " c2 after removing all elements"
55         << " greater than 9 from a4:    ";
56      copy( c2.cbegin(), c2.cend(), output );
57      cout << endl;
58   } // end main
59
60   // determine whether argument is greater than 9
61   bool greater9( int x )
62   {
63      return x > 9;
64   } // end function greater9


a1 before removing all 10s:
   10 2 10 4 16 6 14 8 12 10
a1 after removing all 10s:
   2 4 16 6 14 8 12

a2 before removing all 10s and copying:
   10 2 10 4 16 6 14 8 12 10
c after removing all 10s from a2:
   2 4 16 6 14 8 12 0 0 0

a3 before removing all elements greater than 9:
   10 2 10 4 16 6 14 8 12 10
a3 after removing all elements greater than 9:
   2 4 6 8

a4 before removing all elements
greater than 9 and copying:
   10 2 10 4 16 6 14 8 12 10
c2 after removing all elements
greater than 9 from a4:
   2 4 6 8 0 0 0 0 0 0


Fig. 16.3. Algorithms remove, remove_if, remove_copy and remove_copy_if.

remove Algorithm

Line 22 uses the remove algorithm to eliminate from a1 all elements with the value 10 in the range from a1.begin() up to, but not including, a1.end(). The first two iterator arguments must be forward iterators. This algorithm does not modify the number of elements in the container or destroy the eliminated elements, but it does move all elements that are not eliminated toward the beginning of the container. The algorithm returns an iterator positioned after the last element that was not removed. Elements from the iterator position to the end of the container have unspecified values.

remove_copy Algorithm

Line 32 uses the remove_copy algorithm to copy all elements from a2 that do not have the value 10 in the range from a2.cbegin() up to, but not including, a2.cend(). The elements are placed in c, starting at position c.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 inserted into the copy location. This algorithm returns an iterator positioned after the last element copied into vector c.

remove_if Algorithm

Line 41 uses the remove_if algorithm to delete from a3 all those elements in the range from a3.begin() up to, but not including, a3.end() for which our user-defined unary predicate function greater9 returns true. Function greater9 (defined in lines 61–64) returns true if the value passed to it is greater than 9; otherwise, it returns false. The iterators supplied as the first two arguments must be forward iterators. This algorithm does not modify the number of elements in the container, but it does move to the beginning of the container all elements that are not removed. This algorithm returns an iterator positioned after the last element that was not removed. All elements from the iterator position to the end of the container have undefined values.

remove_copy_if Algorithm

Line 53 uses the remove_copy_if algorithm to copy all those elements from a4 in the range from a4.cbegin() up to, but not including, a4.cend() for which the unary predicate function greater9 returns true. The elements are placed in c2, starting at 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
18.191.14.93