Generics are a concept. templates in C ++ support and implement generics.
---------------------------------------------------------------
To put it simply, generic programming is a kind of thinking model that "converts data types into values. The C ++ template mechanism is a specific vehicle of generic technology. In C ++, whether it is functions or classes, you can represent the data type required in it as a retainer (placeholder). This retainer is also called a template parameter. For example, the function template is as follows:
Template <typename T1, typename T2)
Void func (T1 param1, T2 param2 ){/*...*/}
Or class template:
Template <typename T1, typename T2)
Class {/*...*/}
Once the function func () or Class A is used in the program:
Func (5, 2.3 );
A <int, double>;
The compiler automatically exports a letter object or Class Object Based on the function template function reference or the class template reference. In other words, this current action is completed during compilation without increasing the cost of execution. (For the syntax and performance of the template, refer to any "young" C ++ full-picture book)
STL is a set of objects of the generic concept. Technically, it is actually a set of rigorous "Concepts" classifications. Here, the so-called concepts has a serious definition, which means "some condition requirements for a certain type 」. The models that meet these conditions is called the concepts. STL concepts does not actually correspond to anything in C ++ or other languages.
---------------------------------------------------------------
For C ++ programming
Generic programming is STL programming.
---------------------------------------------------------------
Generic programming and templates are technically similar, but not completely equivalent. Generic programming is an art and a kindProgramThe design concept (I can't think of a better word), and the template is a pure technology.
---------------------------------------------------------------
Generic programming is also called general programming,
MakeAlgorithmOr the structure can be independent of its details, that is, the type, to achieve a more advanced Abstraction
---------------------------------------------------------------
Generic is a method. How does a template use this method in C ++?