All pastes #362564 Raw Edit

Untitled

public text v1 · immutable
·published 2007-02-18 22:26 UTC
rendered paste body
template < typename T >
void ToyBox < T >::PrintBoxSummary( ostream & out ) const
{

   //prints the number of items in the box
   out << endl << "ToyBox Contents" << endl;
   out << "--------------" << endl;
   out << "The ToyBox contains " << m_numToys << " of a possible ";
   out << m_capacity << " items" << endl;

   if( m_numToys > 0)
   {

      //prints out the number of distinct items in the box
      out << endl << "There are " << NumDistinctItems();
      out << " distinct items" << endl;

      for( unsigned int i = 0; i < m_toys.size(); i++ )
      {

	out << endl << m_toys.at(i).GetShape() << ", ";
	out << m_toys.at(i).GetColor(); 

      }

   }

}



//Function: NumDistinctItems
//Description: Returns the number of distinct items in the ToyBox
template < typename T >
int ToyBox < T >::NumDistinctItems( )
{

   int size = m_toys.size();

   int distinct = 0;

   bool found = false;

   vector <T> AlreadyCounted;

   //check every toy against a vector that contains toys already seen
   for( int i = 0; i < size; i++ )
   {
      
      for( unsigned int j = 0; j < AlreadyCounted.size(); j++ )
      {
	 
	 //if the toy is in the vector, set found to true and break the
	 //loop
	 if( m_toys.at(i) == AlreadyCounted.at(j) )
	 {
	    
	    found = true;
	    
	    break;
	    
	 }
	 
      }
      
      //if after checking all toys against the already found vector found
      //still equals false, then put the toy in the already counted vector
      if( found == false )
      {
	 
	 AlreadyCounted.push_back( m_toys.at(i) );
	 
      }
      
      //set found to false again
      found = false;
      
   }
      
   //the number of distinct items will be the size of the alreadcounted
   // vector
   distinct = AlreadyCounted.size();
   
   //return the number of distinct items
   return( distinct );
   
}