From 5da14042f70711ea5cf66e034699730335462f66 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 14:08:03 +0200 Subject: Merging upstream version 1.45.3+dfsg. Signed-off-by: Daniel Baumann --- src/ml/dlib/examples/queue_ex.cpp | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/ml/dlib/examples/queue_ex.cpp (limited to 'src/ml/dlib/examples/queue_ex.cpp') diff --git a/src/ml/dlib/examples/queue_ex.cpp b/src/ml/dlib/examples/queue_ex.cpp new file mode 100644 index 000000000..c79bf25eb --- /dev/null +++ b/src/ml/dlib/examples/queue_ex.cpp @@ -0,0 +1,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 +#include +#include +#include +#include + + +// 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::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; +} + -- cgit v1.2.3