C++ templates are a powerful feature that enables developers to create generic and reusable code. They allow functions and classes to operate with any data type without needing to write multiple versions of the same code. This flexibility leads to cleaner, more maintainable code. Below are three practical examples showcasing both function and class templates.
In many applications, you may need to determine the maximum value among different data types (integers, floats, etc.). A function template can simplify this task.
#include <iostream>
using namespace std;
template <typename T>
T findMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
cout << "Max of 10 and 20 is: " << findMax(10, 20) << endl; // Works with integers
cout << "Max of 10.5 and 20.3 is: " << findMax(10.5, 20.3) << endl; // Works with floats
cout << "Max of 'A' and 'B' is: " << findMax('A', 'B') << endl; // Works with characters
return 0;
}
This function template findMax
accepts two parameters of the same type and returns the greater of the two. It can be used with various data types, demonstrating the flexibility of function templates.
typename T
allows the function to accept any data type, making it versatile.Class templates can be useful for creating data structures. The following example demonstrates a simple Pair
class that holds two values of potentially different types.
#include <iostream>
using namespace std;
template <typename T1, typename T2>
class Pair {
private:
T1 first;
T2 second;
public:
Pair(T1 f, T2 s) : first(f), second(s) {}
T1 getFirst() { return first; }
T2 getSecond() { return second; }
};
int main() {
Pair<int, double> intDoublePair(1, 2.5);
cout << "First: " << intDoublePair.getFirst() << ", Second: " << intDoublePair.getSecond() << endl;
Pair<string, char> stringCharPair("Hello", 'A');
cout << "First: " << stringCharPair.getFirst() << ", Second: " << stringCharPair.getSecond() << endl;
return 0;
}
In this example, the Pair
class template is defined with two type parameters, T1
and T2
. It can store a pair of values of different types, showcasing the utility of class templates.
Sorting arrays is a common task in programming. A function template can be employed to create a generic sorting function that works with different data types.
#include <iostream>
using namespace std;
template <typename T>
void bubbleSort(T arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
}
int main() {
int intArr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(intArr) / sizeof(intArr[0]);
bubbleSort(intArr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << intArr[i] << " ";
cout << endl;
double doubleArr[] = {64.5, 34.2, 25.1, 12.3};
int m = sizeof(doubleArr) / sizeof(doubleArr[0]);
bubbleSort(doubleArr, m);
cout << "Sorted array: ";
for (int i = 0; i < m; i++)
cout << doubleArr[i] << " ";
cout << endl;
return 0;
}
Here, the bubbleSort
function template sorts an array of any type using the bubble sort algorithm. It demonstrates the effectiveness of templates in creating reusable sorting functions.