Using a queue to store user-defined object : queue « queue stack « C++ Tutorial






#include <iostream>
#include <queue>
#include <string>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

class Vehicle{
   public:
   Vehicle( string description = "Unknown car",string license = "Unknown license", bool wax = false );
   string description() const;
   string license() const;
   bool wax() const;

   private:
   string description_, license_;
   bool wax_;
};
   inline
   Vehicle::Vehicle( string description, string license, bool wax )
      : description_( description ), license_( license ), wax_( wax )
   {} // empty

   string Vehicle::description() const
   {  return description_; }

   string Vehicle::license() const
   {  return license_; }

   bool Vehicle::wax() const
   {  return wax_; }

   int main( )
   {
      const char* description[] = { "A", "B","C", "D","E" };
      const char* license[] = { "a", "b", "c","d", "e" };
      const bool wax[] = { false, true, false, true, false };
      const int num_cars = sizeof( wax ) / sizeof( wax[0] );

      queue<Vehicle> line;

      int count = 0;
      while( count < num_cars || !line.empty() )
      {
         for( int i = 0; i < 2; ++i )
            if( count < num_cars )
            {
               cout << "A " << description[count] << ", license " << license[count] << ", is here for a wash";
               if( wax[count] )
                  cout << " and a wax";
               cout << endl << endl;

               line.push( Vehicle( description[count], license[count],wax[count] ) );
               ++count;
            }
            else
            break;
         cout << "ATTENTION PLEASE: a " << line.front().description()
         << ", license " << line.front().license()
         << ",\n\has been carefully washed ";
         if( line.front().wax() )
           cout << "and waxed ";
         cout << "and is available for pick-up\n\n";
         line.pop();
      }
}








21.2.queue
21.2.1.Instantiating an STL Queue
21.2.2.Working with a Queue of Integers
21.2.3.A queue for strings: push, empty, front, pop
21.2.4.queue with doubles
21.2.5.queue: push, pop, front and size
21.2.6.queue.front()
21.2.7.Queue buffer
21.2.8.Using a queue to store user-defined object
21.2.9.Queue: push, pop and size