"Fossies" - the Fresh Open Source Software Archive

Member "dlib-19.18/docs/queue_ex.cpp.html" (22 Sep 2019, 6454 Bytes) of package /linux/misc/dlib-19.18.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) HTML source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

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