C + + templates (Templates)

Source: Internet
Author: User

Templates are the basis of generic programming , and the meaning of " generics " is that no specific data types are not dependent. The introduction of templates is to create generic classes (template classes) or functions (template functions). Typical containers such as iterators/algorithms are typical examples of generic programming. For example, define a vector that can use Vector<int>, Vector<string>, vector<vector>, and so on.

function templates

The general definition of a template function is: The type here can be seen as a placeholder name representing a data type, which can be used within the definition of a function.

Template <class
Ret-type func-Name (parameter list) { // body of function}

Examples are analyzed below. Note that the typename keyword and class keyword function is the same , the class keyword will let you produce whether the definition of the type can only be a class, but not other data types? To avoid this ambiguity, TypeName is introduced here!!!

1#include <iostream>2#include <string>3 4 using namespacestd;5 6Template <TypeNameT>7Inline TConst& Max (TConst& A, TConst&b) {8     returnA < b?b:a;9 }Ten  One intMain () { A  -     inti = the; -     intj = -; thecout <<"Max (i, J)"<< Max (i, J) <<Endl; # Max (i,j): -  -     DoubleF1 =13.5; -     DoubleF2 =20.7; +cout <<"Max (F1, F2)"<< Max (F1, F2) <<Endl; # Max (F1, F2): 20.7 -  +     stringS1 ="Hello"; A     stringS2 =" World"; atcout <<"Max (S1, S2)"<< Max (S1, S2) <<Endl; # Max (S1, S2): World -  -     return 0; -  -}

Class template

Just as we define a function template, you can define a class template here as well. The general form of declaring a class template is:

Template <class type>classclass-name{  }

Example:

#include <iostream>#include<vector>#include<cstdlib>#include<string>#include<stdexcept>using namespacestd;template<classT>classStack {Private: Vector<T> Elems;//Elements    Public:       voidPush (TConst&);//Push Element      voidPop ();//Pop elementT Top ()Const;//Return Top element      BOOLEmpty ()Const{//return True if empty.         returnElems.empty (); } }; Template<classT>voidStack<t>::p Ush (TConst&elem) {    //append copy of passed elementElems.push_back (Elem); } template<classT>voidStack<t>::p op () {if(Elems.empty ()) {ThrowOut_of_range ("Stack<>::p op (): Empty Stack"); }       //Remove last elementElems.pop_back (); } template<classT>T Stack<t>::top ()Const {    if(Elems.empty ()) {ThrowOut_of_range ("stack<>::top (): Empty Stack"); }       //return copy of last element   returnElems.back (); } intMain () {Try{Stack<int> intstack;//Stack of INTsstack<string> stringstack;//Stack of Strings//manipulate int StackIntstack.push (7); cout<< intstack.top () <<Endl; //7 //Manipulate string StackStringstack.push ("Hello"); cout<< stringstack.top () <<Std::endl;       //Hello stringstack.pop ();    Stringstack.pop (); }Catch(ExceptionConst&ex) {Cerr<<"Exception:"<< ex.what () <<Endl; //Exception:stack<>::p op (): Empty Stack return-1; } } 

Reference:

[1] C + + templates:https://www.tutorialspoint.com/cplusplus/cpp_templates.htm

C + + templates (Templates)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.