Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
swap.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for copy, iter_swap
30#include <iostream> // for cout, endl
31#include <iterator> // for ostream_iterator
32#include <vector> // for vector
33
34int main ()
35{
36 typedef std::ostream_iterator<int, char, std::char_traits<char> > Iter;
37
38 const int a[] = { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 };
39
40 // Set up a vector.
41 std::vector<int, std::allocator<int> > v (a, a + sizeof a / sizeof *a);
42
43 // Output original vector.
44 std::cout << "For the vector: ";
45 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
46
47 // Swap the first five elements with the last five elements.
48 std::swap_ranges (v.begin (), v.begin () + 5, v.begin () + 5);
49
50 // Output result.
51 std::cout << "\n\nSwaping the first five elements "
52 "with the last five gives: \n ";
53
54 std::copy (v.begin (), v.end (), Iter (std::cout," "));
55
56 // Now an example of iter_swap -- swap first and last elements.
57 std::iter_swap (v.begin(), v.end () - 1);
58
59 // Output result.
60 std::cout << "\n\nSwaping the first and last elements gives: \n ";
61 std::copy (v.begin (), v.end (), Iter (std::cout, " "));
62
63 std::cout << std::endl;
64
65 return 0;
66}
#define v
Definition 17.names.cpp:138
#define a
Definition 17.names.cpp:109
std::ostream_iterator< int, char, std::char_traits< char > > Iter
Definition alg7.cpp:64
int main()
Definition swap.cpp:34