C ++ template Learning

Source: Internet
Author: User

1. Concepts of templates

We have learned the overloading feature. for heavy-duty functions, the C ++ check mechanism can vary with function parameters and classes. Call the overload function correctly. For example, to obtain the maximum values of two numbers, we define the max () function to define different overload versions for different data types.

// Function 1.

Int max (int x, int y)
{Return (x> Y )? X: Y ;}

// Function 2.
Float max (float X, float y)

{Return (x> Y )? X: Y ;}

// Function 3.
Double max (Double X, Double Y)
{Return (x> Y )? X: Y ;}

However, if char A and B are defined in the main function, an error occurs when Max (A, B) is executed, because we do not define the overloaded version of the char type.

Now, let's re-examine the above max () functions. They all have the same function, that is, to find the maximum value of two numbers. Can we write only one set of code to solve this problem? This will avoid calling errors caused by incomplete definition of overload functions. To solve the above problem, C ++ introduces a template mechanism. template definition: A template is a tool for implementing code reuse mechanisms. It can implement type parameterization, that is, define the type as a parameter, this achieves real code reusability. Templates can be divided into two types: function templates and class templates.

2. Writing function templates

The function template is generally in the following format:

Template <class or typename T>

Return type function name (parameter table)
{// Function Definition body}

Description: Template is a keyword used to declare a template. It indicates that the class that declares a template keyword cannot be omitted. If the type parameter is redundant, class <type parameter table> must be added before each parameter to include basic data types and class types.

See the following program:

# Include <iostream> Using STD: cout; Using STD: Endl; // declare a function template to compare the input values of two parameters of the same data type, class can also be replaced by typename, and // t can be replaced by any letter or number. Template <class T> T min (t x, t y) {return (x <Y )? X: Y;} void main () {int n1 = 2, n2 = 10; double d1 = 1.5, D2 = 5.6; cout <"small integer: "<min (N1, N2) <Endl; cout <" smaller real number: "<min (D1, D2) <Endl; system ("pause ");}

Program running result:

 

 

 

 

 

 

 

1. Use the template class together with the overload Function
When using the two functions together, consider reload functions first, and then consider the template class. If you cannot find them, consider type conversion, which may lead to changes in accuracy.

# Include "iostream" using namespace STD; // function template <class T> const T max (t a, t B) {printf ("% s \ n ", "template"); Return (A> B )? A: B;} int max (INT X, int y) {printf ("% s \ n", "int"); Return (x> Y )? X: Y;} int max (char X, int y) {printf ("% s \ n", "char int"); Return (x> Y )? X: Y;} int max (int x, char y) {printf ("% s \ n", "int char"); Return (x> Y )? X: Y;} int main (void) {int A = 3, B = 5; char x = 'X'; double C = 3.4; cout <max (, b) <Endl; // call the overload function cout <max (C, B) <Endl; // no corresponding overload function, call the template cout <max (A, x) <Endl; // overload function cout <max (X, A) <Endl; // overload function cout <max (C, A) <Endl; cout <max (a); System ("pause"); Return 0 ;}

2. class template
(1) the specific format of the class template
Template <class T>
Class
{

}
The function template should be used to define the member functions defined in vitro in the class.

/* Class template, but when defining a member function outside the class, use the function template */# include <iostream> using namespace STD; Template <class T> class base {public: t a; base (t B) {A = B;} t geta () {return a;} // void Seta (t C) defined in the class );}; template <class T> // template definition outside the class void base <t >:: Seta (t C) {A = C;} int main (void) {base <int> B (4); cout <B. geta () <Endl; base <double> BC (4); BC. seta (4.3); cout <BC. geta () <Endl; System ("pause"); Return 0 ;}

Pay attention to the situation where member functions are defined outside the class.
3. template class
It mainly refers to the STL template class.

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.