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:\n   ";16      copy( a.cbegin(), a.cend(), output ); // display array a17 18      swap( a[ 0 ], a[ 1 ] ); // swap elements at locations 0 and 1 of a 19 20 ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.