Accessing a Container's Elements in Reverse : list reverse « list « 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 » list » list reverse 
17.13.2.Accessing a Container's Elements in Reverse
#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>

using namespace std;

template <class ForwIter>
void print(ForwIter first, ForwIter last, const char* title)
{
   cout << title << endl;
   while first != last)
      cout << *first++ << '\t';
   cout << endl;
}

class Card
{
   public:
   enum Suit spades, clubs, hearts, diamonds };

   Cardint value = 1, Suit suit = spades );
   // value - 1 = Ace, 2-10, 11 = Jack, 12 = Queen, 13 = King

   bool operator<const Card& rhs const;

   void print() const;

   int suit() const;
   int value() const;

   private:
   int value_;
   Suit suit_;
}

inline
Card::Cardint value, Suit suit )
   : value_value ), suit_suit )
{} // empty

inline
bool Card::operator<const Card& rhs const
return value() < rhs.value()}

void Card::print() const
{
   ifvalue() >= && value() <= 10 )
      cout << value();
   else
      switchvalue() )
      {
         case  1: cout << "Ace"break;
         case 11: cout << "Jack"break;
         case 12: cout << "Queen"break;
         case 13: cout << "King"break;
      };

   cout << " of ";
   switchsuit() )
   {
      case spades: cout << "spades"break;
      case clubs: cout << "clubs"break;
      case diamonds: cout << "diamonds"break;
      case hearts: cout << "hearts"break;
   default: cout << "unknown suit"break;
     }
    cout << endl;
}

inline
int Card::suit() const
return suit_; }

inline
int Card::value() const
return value_; }

int main( )

   list<Card> hand;

   hand.push_backCard12, Card::hearts ) );
   hand.push_backCard6, Card::clubs ) );
   hand.push_backCard12, Card::diamonds ) );
   hand.push_backCard1, Card::spades ) );
   hand.push_backCard11, Card::clubs ) );

   hand.sort();
   for_eachhand.begin(), hand.end(), mem_fun_ref&Card::print ) );

   for_eachhand.rbegin(), hand.rend(), mem_fun_ref&Card::print ) );
}
17.13.list reverse
17.13.1.Reversing Elements in a list
17.13.2.Accessing a Container's Elements in Reverse
17.13.3.Use reverse function on list
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.