// C++ template program with arrays
#include <iostream>
#include <string.h>
using namespace std;
template <class TypeName>
// function using the template
void printArray(TypeName* arr, int len)
{
for (int i = 0; i < len; i++) {
cout << *arr << " ";
++arr; // pointing to next element
}
cout << endl;
}
// main code/ main function
int main()
{
// declaring character array
char chrArr[] = "IncludeHelp";
//declaring integer array
int numArr[] = { 10, 20, 30, 40, 50 };
//printing array elements
cout << "chrArr: ";
printArray(chrArr, strlen(chrArr));
cout << "numArr: ";
printArray(numArr, 5);
//passing direct string
cout << "Hello: ";
printArray("Hello", 5);
return 0;
}
Output
chrArr: I n c l u d e H e l p
numArr: 10 20 30 40 50
Hello: H e l l o
C++ program:
Output