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

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » deque » deque iterator 
22.6.3.Using the Front of a Deque
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

class Auction_item
{
   public:
   Auction_itemconst string& name = "nothing",
      int minimum_bid = );
   int minimum_bid() const;
   string name() const;

   private:
   int minimum_bid_;
   string name_;
};

inline
Auction_item::Auction_itemconst 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);
   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;
   forvector<Auction_item>::iterator i = v.begin();
      i != v.end(); ++i )
      ifi->minimum_bid() >= min_expensive_item )
         items.push_back*i );
      else
         items.push_front*i );

   // sell the items
   bool sell_cheap = true;
   while!items.empty() )
   {
      ifsell_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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.