Mega Code Archive

 
Categories / C++ / Basics
 

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  */