C++ Tutorial/STL Introduction/container as parameter

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Function with stack as parameter

#include <iostream>
using std::cout;
using std::endl;
#include <stack>  // stack adapter definition
#include <vector> // vector class-template definition
#include <list>   // list class-template definition
template< typename T > void pushElements( T &stackRef );
template< typename T > void popElements( T &stackRef );
int main()
{
   std::stack< int > intDequeStack;
   cout << "Pushing onto intDequeStack: ";
   pushElements( intDequeStack );
   cout << endl << endl;
   cout << "Popping from intDequeStack: ";
   popElements( intDequeStack );
   cout << endl;
   return 0;
}
template< typename T > void pushElements( T &stackRef )
{
   for ( int i = 0; i < 10; i++ )
   {
      stackRef.push( i );
      cout << stackRef.top() << " ";
   }
}
template< typename T > void popElements( T &stackRef )
{
   while ( !stackRef.empty() )
   {
      cout << stackRef.top() << " ";
      stackRef.pop();
   }
}
Pushing onto intDequeStack: 0 1 2 3 4 5 6 7 8 9
Popping from intDequeStack: 9 8 7 6 5 4 3 2 1 0

Function with vector as the parameter

#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
template < typename T > void printVector( const vector< T > &integers2 );
int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 }; // initialize array
   vector< int > integers; // create vector of ints
   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );
   printVector( integers );
   cout << endl;
   return 0;
}
template < typename T > void printVector( const vector< T > &integers2 ){
   typename vector< T >::const_iterator constIterator;
   for ( constIterator = integers2.begin();
      constIterator != integers2.end(); ++constIterator )
      cout << *constIterator << " ";
}
2 3 4

Use list as the function parameter

#include <iostream>
using std::cout;
using std::endl;
#include <list>      // list class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator
template < typename T > void printList( const std::list< T > &listRef );
int main()
{
   int array[ 4 ] = { 2, 6, 4, 8 };
   std::list< int > values;      // create list of ints
   std::list< int > otherValues; // create list of ints
   // insert items in values
   values.push_front( 1 );
   values.push_front( 2 );
   values.push_back( 4 );
   values.push_back( 3 );
   cout << "values contains: ";
   printList( values );
   cout << endl;
   return 0;
}
template < typename T > void printList( const std::list< T > &listRef )
{
    std::ostream_iterator< T > output( cout, " " );
    std::copy( listRef.begin(), listRef.end(), output );
}
values contains: 2 1 4 3

Using template functions to output array of different types

#include <iostream>
using std::cout;
using std::endl;
template< typename T >
void printArray( const T *array, int count )
{
   for ( int i = 0; i < count; i++ )
      cout << array[ i ] << " ";
   cout << endl;
}
int main()
{
   const int aCount = 5; 
   const int bCount = 7; 
   const int cCount = 6; 
   int a[ aCount ] = { 1, 2, 3, 4, 5 };
   double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
   char c[ cCount ] = "HELLO";
   printArray( a, aCount );  
   printArray( b, bCount );  
   printArray( c, cCount );  
   return 0;
}
1 2 3 4 5
1.1 2.2 3.3 4.4 5.5 6.6 7.7
H E L L O