1.Template
1.1 Template Concept
Templates are the foundation of generic programming, which is writing code in a way that is independent of any particular type.
It can also be understood as a blueprint or formula for a function. Library containers, such as iterators and algorithms, are examples of generic programming that use the concept of templates.
Features: Versatile
1.2 Template
-
Another programming idea in C++ is called generic programming, and the main technology used is templates
-
C++ provides two template mechanisms: function templates and class templates
1.2.1 Function Template Syntax
Function template role:
To establish a general function, the function return value type and formal parameter type can not be specified, but represented by a virtual type.
Syntax:
template<typename T> //function declaration or definition
Explanation:
template – declares the creation template
typename – the symbol behind the surface is a data type, which can be replaced by class
T – Generic data type, the name can be replaced, usually an uppercase letter
Example
/**************************************** *@File : template.cpp *@IDE: CLion *@Author: lingxiaotian *@Date: 2022/9/14 19:37 ****************************************/ #include "iostream" #include <string> using namespace std; //function template template<class T> // swap template of two numbers void swapNumber(T & amp;a,T & amp;b){<!-- --> T temp = a; a = b; b = temp; } //integer swap void test(){<!-- --> int a = 8; int b = 12; swapNumber(a,b); cout<<"The value of a after the exchange is: "<<a<<endl; cout<<"The value of b after the exchange is:"<<b<<endl; } // floating point swap void test1(){<!-- --> float a = 100.86; float b = 124.01; swapNumber(a,b); cout<<"The value of a after the exchange is: "<<a<<endl; cout<<"The value of b after the exchange is:"<<b<<endl; } //string swap void test2(){<!-- --> string a = "I was originally an a~"; string b = "I am meowing b!"; swapNumber(a,b); cout<<"The value of a after the exchange is: "<<a<<endl; cout<<"The value of b after the exchange is:"<<b<<endl; } int main(){<!-- --> test(); test1(); test2(); }
It can be seen that only one template needs to be used at this time to complete the exchange of two data of the same type, without writing an exchange function for each data type separately, which is also the characteristic of templates – universality.
At this time, when the function swapNumber() is called, it is automatically pushed to the type of the parameter, and the data type can also be specified manually.
Specify data type manually
//String exchange void test2(){<!-- --> string a = "I was originally an a~"; string b = "I am meowing b!"; // Manually specify the data type swapNumber<string>(a,b); cout<<"The value of a after the exchange is: "<<a<<endl; cout<<"The value of b after the exchange is:"<<b<<endl; }
There was an error when writing this case, the code is as follows:
Error cases
//function template template<class T> // swap template of two numbers void swap(T & amp;a,T & amp;b){<!-- --> T temp = a; a = b; b = temp; } //integer swap void test(){<!-- --> int a = 10; int b = 123; swap(a,b); //!!! An error is reported here, call to 'swap' is ambiguous! ! cout<<"The value of a after the exchange is: "<<a<<endl; cout<<"The value of b after the exchange is:"<<b<<endl; } int main(){<!-- --> test(); }
An error occurred when calling the template function swap(), and the error message was “call to ‘swap’ is ambiguous”. After research, it was found that it was in conflict with the library function name of C++ itself, so it was resolved by changing to swapNumber.
! So you must pay attention to the normative naming~~