Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Enables a class to handle different data types. Most STL containers like vector and stack are implemented as class templates.
template <class T>
class Box {
T data;
public:
Box(T d) : data(d) {}
T getData() { return data; }
};
Box<int> intBox(10);
Box<string> strBox("Hello");Ensures that you only perform valid operations for the specific type T.
Write the data structure logic once; use it for any data type.
You can define a special version of the template for a specific type (like bool) to optimize its behavior.