C++ Code Example 2: Function Templates – Copy Array in Reverse Order

This C++ program illustrates the use of templates to create a flexible copy function that can duplicate the contents of an array into another array of any data type. The function not only performs the copying operation but also prints the contents of the source and destination arrays before and after the copy.

By employing templates, the code ensures type safety and reduces redundancy, making it a powerful tool for handling arrays of different types. This example highlights the practicality of templates and dynamic memory allocation in C++ programming.

#include <iostream>
using namespace std;

template <class T>
void copy(T *src, T *dst, int length) {
    // print array
    cout << "\nSource array" << endl;
    for (int i = 0; i < length; i++) {
        cout << src[i] << " ";
    }

    cout << "\nDestination array" << endl;
    for (int i = length - 1; i >= 0; i--) {
        dst[i] = src[i];
        cout << dst[i] << " ";
    }
}

int main() {
    int intNumbers[] = {1, 2, 3, 4, 5, 6, 7}, *copyIntNumbers = new int[8], intArrayLength;
    double doubleNumbers[] = {1.4, 2.3, 3.7, 4.12, 5.01, 6.8, 7.212}, *copyDoubleNumbers = new double[8], doubleArrayLength;

    intArrayLength = end(intNumbers) - begin(intNumbers);
    doubleArrayLength = end(doubleNumbers) - begin(doubleNumbers);
    
    copy<int>(intNumbers, copyIntNumbers, intArrayLength);
    copy<double>(doubleNumbers, copyDoubleNumbers, doubleArrayLength);

    return 0;
}
Output
Source array
1 2 3 4 5 6 7 
Destination array
7 6 5 4 3 2 1 
Source array
1.4 2.3 3.7 4.12 5.01 6.8 7.212 
Destination array
7.212 6.8 5.01 4.12 3.7 2.3 1.4

Code Explanation

Template Function Definition

template <class T>
void copy(T *src, T *dst, int length) {
    // print array
    cout << "\nSource array" << endl;
    for (int i = 0; i < length; i++) {
        cout << src[i] << " ";
    }

    cout << "\nDestination array" << endl;
    for (int i = length - 1; i >= 0; i--) {
        dst[i] = src[i];
        cout << dst[i] << " ";
    }
}
  • template <class T>: This line defines a template. A template allows the function to work with any data type. The class T part indicates that T is a placeholder for any data type.
  • void copy(T *src, T *dst, int length): This declares a function named copy that takes three parameters: two pointers to a type T (src and dst) and an integer (length).
  • Inside the function:
    • cout << "\nSource array" << endl;: This prints “Source array” followed by a newline.
    • for (int i = 0; i < length; i++): This loop iterates over the elements of the source array.
      • cout << src[i] << " ";: This prints each element of the source array followed by a space.
    • cout << "\nDestination array" << endl;: This prints “Destination array” followed by a newline.
    • for (int i = length - 1; i >= 0; i--): This loop iterates over the elements of the destination array in reverse order.
      • dst[i] = src[i];: This copies each element from the source array to the destination array.
      • cout << dst[i] << " ";: This prints each element of the destination array followed by a space.

Main Function

int main() {
    int intNumbers[] = {1, 2, 3, 4, 5, 6, 7}, *copyIntNumbers = new int[8], intArrayLength;
    double doubleNumbers[] = {1.4, 2.3, 3.7, 4.12, 5.01, 6.8, 7.212}, *copyDoubleNumbers = new double[8], doubleArrayLength;

    intArrayLength = end(intNumbers) - begin(intNumbers);
    doubleArrayLength = end(doubleNumbers) - begin(doubleNumbers);
    
    copy<int>(intNumbers, copyIntNumbers, intArrayLength);
    copy<double>(doubleNumbers, copyDoubleNumbers, doubleArrayLength);

    return 0;
}
  • int main() { ... }: This defines the main function where the execution of the program begins.
  • Variable declarations:
    • int intNumbers[] = {1, 2, 3, 4, 5, 6, 7}, *copyIntNumbers = new int[8], intArrayLength;: This declares and initializes an integer array intNumbers, dynamically allocates memory for an integer array copyIntNumbers with 8 elements, and declares an integer variable intArrayLength.
    • double doubleNumbers[] = {1.4, 2.3, 3.7, 4.12, 5.01, 6.8, 7.212}, *copyDoubleNumbers = new double[8], doubleArrayLength;: This declares and initializes a double array doubleNumbers, dynamically allocates memory for a double array copyDoubleNumbers with 8 elements, and declares a double variable doubleArrayLength.
  • Calculating the array lengths:
    • intArrayLength = end(intNumbers) - begin(intNumbers);: This calculates the length of the intNumbers array by subtracting the beginning pointer from the ending pointer.
    • doubleArrayLength = end(doubleNumbers) - begin(doubleNumbers);: This calculates the length of the doubleNumbers array by subtracting the beginning pointer from the ending pointer.
  • Function calls to copy:
    • copy<int>(intNumbers, copyIntNumbers, intArrayLength);: This calls the copy function with int as the type T. The intNumbers array, copyIntNumbers array, and intArrayLength are passed to the function.
    • copy<double>(doubleNumbers, copyDoubleNumbers, doubleArrayLength);: This calls the copy function with double as the type T. The doubleNumbers array, copyDoubleNumbers array, and doubleArrayLength are passed to the function.
  • return 0;: This indicates that the program terminated successfully.