Accessing the Back of a Vector : vector back « vector « 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 » vector » vector back 
16.4.2.Accessing the Back of a Vector
#include <iostream>
#include <list>
#include <vector>

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

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

   private:
   int value_;
   Suit suit_;
};

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

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

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

// global function
ostream& operator<<ostream& out, const Card& card )
{
   ifcard.value() >= && card.value() <= 10 )
      out << card.value();
   else
      switchcard.value() )
      {
         case  1: out << "Ace"break;
         case 11: out << "Jack"break;
         case 12: out << "Queen"break;
         case 13: out << "King"break;
         default: out << "Unknown value"break;
      };

   out << " of ";
   switchcard.suit() )
   {
      case Card::spades:   out << "spades"break;
      case Card::clubs:    out << "clubs"break;
      case Card::diamonds: out << "diamonds"break;
      case Card::hearts:   out << "hearts"break;
      default:             out << "unknown suit"break;
   }
   return out;
}

int main( )
{
   const int num_players = 2;
   vector< list<Card> > handsnum_players );

   // deal first player a hand
   hands[0].push_frontCard12, Card::hearts ) );
   hands[0].push_frontCard1,  Card::spades ) );
   hands[0].push_frontCard4,  Card::spades ) );

   // deal second player a hand
   hands[1].push_frontCard13, Card::diamonds ) );
   hands[1].push_frontCard12, Card::clubs ) );
   hands[1].push_frontCard2,  Card::hearts ) );
   const int num_plays = 3;
   bool discard[num_players][num_plays{ { true, true, false },
                                            true, false, false } };
   vector<Card> discard_pile;

   // simulate card play
   forint i = 0; i < num_plays; ++i )
      forint j = 0; j < num_players; ++j )

         // if discard and hand not empty...
         ifdiscard[j][i&& !hands[j].empty() )
         {
            discard_pile.push_backhands[j].front() );
            hands[j].pop_front();
            cout << "Player " << (j+1<< " discarded a "
               << discard_pile.back() << endl;
         }// if pick up and discard pile not empty...
         else if!discard[j][i&& !discard_pile.empty() )
         {
            hands[j].push_backdiscard_pile.back() );
            cout << "Player " << (j+1<< " picked up a "
               << discard_pile.back() << endl;
            discard_pile.pop_back();
         }
}
16.4.vector back
16.4.1.Get the last element in the vector
16.4.2.Accessing the Back of a Vector
16.4.3.Demonstrating the STL vector back and pop_back operations
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.