Using the Front of a Deque : deque iterator « deque « C++ Tutorial






#include <deque>
#include <iostream>
#include <string>
#include <vector>

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 Auction_item
{
   public:
   Auction_item( const string& name = "nothing",
      int minimum_bid = 0 );
   int minimum_bid() const;
   string name() const;

   private:
   int minimum_bid_;
   string name_;
};

inline
Auction_item::Auction_item( const string& name, int minimum_bid )
   : name_( name ), minimum_bid_( minimum_bid )
{}
inline
int Auction_item::minimum_bid() const{  return minimum_bid_; }

inline
string Auction_item::name() const{  return name_; }

int main( )
{
   vector<Auction_item> v( 5 );
   v[0] = Auction_item( "A", 150 );
   v[1] = Auction_item( "B", 10 );
   v[2] = Auction_item( "C", 75 );
   v[3] = Auction_item( "D", 20 );
   v[4] = Auction_item( "E", 100 );

   deque<Auction_item> items;

   // load auction items with cheap ones in front, expensive at back
   const int min_expensive_item = 50;
   for( vector<Auction_item>::iterator i = v.begin();
      i != v.end(); ++i )
      if( i->minimum_bid() >= min_expensive_item )
         items.push_back( *i );
      else
         items.push_front( *i );

   // sell the items
   bool sell_cheap = true;
   while( !items.empty() )
   {
      if( sell_cheap )
      {
         cout << items.front().name()
            << " for a minimum bid of "
            << items.front().minimum_bid() << endl;
         items.pop_front();
      }
      else
      {
         cout << "Now selling: a " << items.back().name()
            << " for a minimum bid of "
            << items.back().minimum_bid() << endl;
         items.pop_back();
      }

      // Alternate selling cheap and expensive
      sell_cheap = !sell_cheap;
   }
}








22.6.deque iterator
22.6.1.Define iterator for deque
22.6.2.deque::iterator and deque::reverse_iterator
22.6.3.Using the Front of a Deque
22.6.4.Use iterator and reverse_iterator with deque
22.6.5.Print the contents in reverse order using reverse_iterator and functions rbegin() and rend()
22.6.6.Create an empty deque and then assign it a sequence that is the reverse of deque
22.6.7.Use insert iterator adaptors to insert one deque into another by way of the copy() algorithm.