template

function template

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 template<typename A>A Max(A a, A b);
 6 
 7 int main()
 8 {
 9     double x, y;
10     cin >> x >> y;
11     cout << "The max is:" << Max(x, y) << endl;
12     
13     int m, n;
14     cin >> m >> n;
15     cout << "The max is:" <<Max(m,n) << endl;
16     
17     long i, j;
18     cin >> i >> j;
19     cout << "The max is" <<Max(i, j) << endl; 
20     
21     return 0;
22 }
23 template<typename A>A Max(A a, A b){
24     return a > b ? a : b;
25 }
maximum value between two numbers

1.template <typename type parameter 1 , typename type parameter 2 , ...> return value type function name (formal parameter list){
    //type parameters can be used in the function body
}

 

2.typename can also be replaced by class

 

class template

1.template<typename type parameter 1 , typename type parameter 2 , …> class class name {
    //TODO:
};

 

2.

example
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 template<typename T1,typename T2>
 6 class Point{
 7     public:
 8         Point(T1 x, T2 y):m_x(x), m_y(y){}
 9     public:
10         T1 getx() const;
11         void setx(T1 x);
12         T2 gety() const;
13         void sety(T2 y);
14     private:
15         T1 m_x;
16         T2 m_y;
17 };
18 
19 template<typename T1,typename T2>
20 T1 Point<T1, T2>::getx() const{
21     return m_x;
22 }
23 
24 template<typename T1,typename T2>
25 void Point<T1, T2>::setx(T1 x){
26     m_x = x;
27 }
28 
29 template<typename T1,typename T2>
30 T2 Point<T1, T2>::gety() const{
31     return m_y;
32 }
33 
34 template<typename T1,typename T2>
35 void Point<T1, T2>::sety(T2 y){
36     m_y = y;
37 } 
38 
39 int main()
40 {
41     Point<int, int> p1(10, 20);
42     cout << p1.getx() << p1.gety() << endl;
43     
44     Point<int, char*> p2(20, "东京20°");
45     cout << p2.getx() << p2.gety();
46     
47     return 0;
48 }
View Code

 

Dahua Template Programming (Optional)

1. From the perspective of variable definition and type conversion:

Strongly typed language: When defining variables, you need to explicitly specify the data type (C/C++, Java, C#, etc.)

  Weakly typed languages: ~~ (JavaScript, Python, PHP, Ruby, shell, etc.)

From a compile and run perspective:

Strongly typed languages ​​can confirm the variable type at compile time, but C/C++ sometimes does not, so, in this regard, C/C++ is a weakly typed language

 

2. Whether it is a strongly typed language or a weakly typed language, there is a type system inside the compiler (interpreter) to maintain various information about variables.

 

3. Strongly typed languages ​​are more rigorous, and many errors can be found during compilation, which is suitable for developing large-scale, system-level, and industrial-level projects; while weakly typed languages ​​are more flexible and have high coding efficiency.

Easy to deploy, low learning cost, and useful in web development. In addition, IDEs of strongly typed languages ​​are generally more powerful, with good code awareness and rich prompt information; while weakly typed languages

The language is generally written directly in the editor.

 

4. The types supported by templates are broad and unrestricted. We can use any type to replace. This programming method is called Generic Programming.

Big talk C++ template programming (thorough)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326857899&siteId=291194637
Recommended