blob: c79bf25eb68853d55ce654483200b5f90237f8f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
This is an example illustrating the use of the queue component (and
to some degree the general idea behind most of the other container
classes) from the dlib C++ Library.
It loads a queue with 20 random numbers. Then it uses the enumerable
interface to print them all to the screen. Then it sorts the numbers and
prints them to the screen.
*/
#include <dlib/queue.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
// I'm picking the version of the queue that is kernel_2a extended by
// the queue sorting extension. This is just a normal queue but with the
// added member function sort() which sorts the queue.
typedef dlib::queue<int>::sort_1b_c queue_of_int;
using namespace std;
using namespace dlib;
int main()
{
queue_of_int q;
// initialize rand()
srand(time(0));
for (int i = 0; i < 20; ++i)
{
int a = rand()&0xFF;
// note that adding a to the queue "consumes" the value of a because
// all container classes move values around by swapping them rather
// than copying them. So a is swapped into the queue which results
// in a having an initial value for its type (for int types that value
// is just some undefined value. )
q.enqueue(a);
}
cout << "The contents of the queue are:\n";
while (q.move_next())
cout << q.element() << " ";
cout << "\n\nNow we sort the queue and its contents are:\n";
q.sort(); // note that we don't have to call q.reset() to put the enumerator
// back at the start of the queue because calling sort() does
// that automatically for us. (In general, modifying a container
// will reset the enumerator).
while (q.move_next())
cout << q.element() << " ";
cout << "\n\nNow we remove the numbers from the queue:\n";
while (q.size() > 0)
{
int a;
q.dequeue(a);
cout << a << " ";
}
cout << endl;
}
|