16.3.7. swap, iter_swap and swap_ranges

Figure 16.7 demonstrates algorithms swap, iter_swap and swap_ranges for swapping elements.


 1   // Fig. 16.7: fig16_07.cpp
 2   // Algorithms iter_swap, swap and swap_ranges.
 3   #include <iostream>
 4   #include <array>
 5   #include <algorithm> // algorithm definitions
 6   #include <iterator>
 7   using namespace std;
 8
 9   int main()
10   {
11      const size_t SIZE = 10;
12      array< int, SIZE > a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
13      ostream_iterator< int > output( cout, " " );
14
15      cout << "Array a contains:    ";
16      copy( a.cbegin(), a.cend(), output ); // display array a
17
18      swap( a[ 0 ], a[ 1 ] ); // swap elements at locations 0 and 1 of a
19
20      cout << " Array a after swapping a[0] and a[1] using swap:    ";
21      copy( a.cbegin(), a.cend(), output ); // display array a
22
23      // use iterators to swap elements at locations 0 and 1 of array a
24      iter_swap( a.begin(), a.begin() + 1 ); // swap with iterators    
25      cout << " Array a after swapping a[0] and a[1] using iter_swap:    ";
26      copy( a.cbegin(), a.cend(), output );
27
28      // swap elements in first five elements of array a with
29      // elements in last five elements of array a           
30      swap_ranges( a.begin(), a.begin() + 5, a.begin() + 5 );
31
32      cout << " Array a after swapping the first five elements "
33           << "with the last five elements:    ";
34      copy( a.cbegin(), a.cend(), output );
35      cout << endl;
36   } // end main


Array a contains:
   1 2 3 4 5 6 7 8 9 10
Array a after swapping a[0] and a[1] using swap:
   2 1 3 4 5 6 7 8 9 10
Array a after swapping a[0] and a[1] using iter_swap:
   1 2 3 4 5 6 7 8 9 10
Array a after swapping the first five elements
with the last five elements:
   6 7 8 9 10 1 2 3 4 5


Fig. 16.7. Algorithms iter_swap, swap and swap_ranges.

swap Algorithm

Line 18 uses the swap algorithm to exchange two values. In this example, the first and second elements of array a are exchanged. The function takes as arguments references to the two values being exchanged.

iter_swap Algorithm

Line 24 uses function iter_swap to exchange the two elements. The function takes two forward iterator arguments (in this case, iterators to elements of an array) and exchanges the values in the elements to which the iterators refer.

swap_ranges Algorithm

Line 30 uses function swap_ranges to exchange the elements from a.begin() up to, but not including, a.begin() + 5 with the elements beginning at position a.begin() + 5. The function requires three forward iterator arguments. The first two arguments specify the range of elements in the first sequence that will be exchanged with the elements in the second sequence starting from the iterator in the third argument. In this example, the two sequences of values are in the same array, but the sequences can be from different arrays or containers. The sequences must not overlap. The destination sequence must be large enough to contain all the elements of the ranges being swapped.

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

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