You cannot increase the size of an array during execution. It is fixed-size and needs to be allocated with the fixed-size during declaration.STL also provides generic representation of algorithm (such as searching and sorting), which works on the generic container. Template is extremely useful if a particular algorithm is to be applied to a variety of types, e.g., a container class which contains elements, possibly of various types.Ĭ++'s Standard Template Library (STL) provides template implementation of many container classes, such as vector, which can be used to hold elements of all types. Template supports so-called parameterized type - i.e., you can use type as argument in building a class or a function (in class template or function template). Template lets you program on generic type, instead of on a specific type. Can we write a single sorting routine that works on all types (or most of the types) by specifying the type during invocation? Can we have a general container that can work on all types?
For algorithms (such as searching or sorting), the code works only for a specific type, you need to rewrite the code for another type. For container data structures (such as array and structure), you need to specify the type of the elements. In C language, all codes are tied to a specific data type. The goal of generic programming is to write code that is independent of the data types. Passing type is known as generic programming, as we can program in generic type and invoke the code using a specific type. Instead of passing a variable, we pass a type (such as int, double, and Point) into template. We are familiar in passing value/variable into function.