Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
heap_ops.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for copy, make_heap, pop_heap, push_heap
30#include <functional> // for less
31#include <iostream> // for cout
32#include <iterator> // for ostream_iterator
33#include <vector> // for vector
34
35#include <examples.h>
36
37
38template <class charT, class Traits, class T, class Allocator>
39void print_vector (std::basic_ostream<charT, Traits> &strm,
40 const std::vector<T, Allocator> &v)
41{
42 std::copy (v.begin (), v.end (),
43 std::ostream_iterator<T, charT, Traits> (strm, " "));
44
45 strm << std::endl;
46}
47
48
49int main ()
50{
51 typedef std::vector<int, std::allocator<int> > Vector;
52
53 const Vector::value_type d1[] = { 1, 2, 3, 4 };
54 const Vector::value_type d2[] = { 1, 3, 2, 4 };
55
56 // Set up two vectors.
57 Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
58 Vector v2 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
59
60 // Make heaps.
61 std::make_heap (v1.begin (), v1.end ());
62 std::make_heap (v2.begin (), v2.end (), std::less<int>());
63
64 // v1 = (4, x, y, z) and v2 = (4, x, y, z)
65
66 // Note that x, y and z represent the remaining values in the
67 // container (other than 4). The definition of the heap and heap
68 // operations does not require any particular ordering
69 // of these values.
70
71 // Copy both vectors to cout.
72 print_vector (std::cout, v1);
73 print_vector (std::cout, v2);
74
75 // Now let's pop.
76 std::pop_heap (v1.begin (), v1.end ());
77 std::pop_heap (v2.begin (), v2.end (), std::less<int>());
78
79 print_vector (std::cout, v1);
80 print_vector (std::cout, v2);
81
82 // And push.
83 std::push_heap (v1.begin (), v1.end ());
84 std::push_heap (v2.begin (), v2.end (), std::less<int>());
85
86 print_vector (std::cout, v1);
87 print_vector (std::cout, v2);
88
89 // Now sort those heaps.
90 std::sort_heap (v1.begin (), v1.end ());
91 std::sort_heap (v2.begin (), v2.end (), std::less<int>());
92
93 print_vector (std::cout, v1);
94 print_vector (std::cout, v2);
95
96 return 0;
97}
#define d1
Definition 17.names.cpp:228
#define v
Definition 17.names.cpp:138
#define v2
Definition 17.names.cpp:271
#define strm
Definition 17.names.cpp:549
#define d2
Definition 17.names.cpp:254
#define v1
Definition 17.names.cpp:245
std::vector< UserClass, std::allocator< UserClass > > Vector
int main()
Definition heap_ops.cpp:49
void print_vector(std::basic_ostream< charT, Traits > &strm, const std::vector< T, Allocator > &v)
Definition heap_ops.cpp:39