Detailed explanation of the usage of template <typename T> in C++

1. Introduction

template <typename T> is a fixed format used to define templates in C++. Template is a tool to implement a code reuse mechanism . It can implement type parameterization, that is, defining types as parameters, thus achieving true code reusability. Templates can be divided into two categories, one is function template and the other is class template .

2. Function template

Functional requirements : We need to exchange data of int, char, string, double and other types of data. If there is no mechanism for reusing code such as templates, we need to write multiple functions with basically the same statements based on different parameter types. With For the template function, you only need to write a function, and the compiler can infer the type of the formal parameter by the type of the input parameter.

#include<iostream>
#include<string>
using namespace std;

//template 关键字告诉C++编译器 下面是个泛型模板  
//数据类型T 参数化数据类型
template <typename T>
void generic_swap(T& a, T& b)
{
    cout << "Initial value: " << a << " : " << b << endl;

    T tmp;
    tmp = b;
    b = a;
    a = tmp;
}

int main()
{
    int a = 100, b = 50;
    generic_swap(a, b);
    cout << "excute the swap():" << a << " : " << b << endl;


    char c = 'A', d = 'B';
    generic_swap(c, d);
    cout << "excute the swap():" << c << " : " << d << endl;
    
    string e = "Jacky", f = "Lucy";
    generic_swap(e, f);
    cout << "excute the swap():" << e << " : " << f << endl;

    double j = 1.314, k = 5.12;
    generic_swap(j, k);
    cout << "excute the swap():" << j << " : " << k << endl;
    
    return 0;
}

3. Class template

Functional requirements : Define a class to represent coordinates. The data type of the coordinates can be integers, decimals or strings, for example:

  • x = 10、y = 10
  • x = 12.88、y = 129.65
  • x = "E180"、y = "N210"
#include<iostream>
#include<string>
using namespace std;

//注意:模板头和类头是一个整体,可以换行,但是中间不能有分号
template<typename T1, typename T2>  //这里不能有分号
class Point {
public:
    Point(T1 x, T2 y) : m_x(x), m_y(y) { }
public:
    T1 getX() const;  //获取x坐标
    void setX(T1 x);  //设置x坐标
    T2 getY() const;  //获取y坐标
    void setY(T2 y);  //设置y坐标
private:
    T1 m_x;  //x坐标
    T2 m_y;  //y坐标
};

//下面就对 Point 类的成员函数进行定义
template<typename T1, typename T2> T1 Point<T1, T2>::getX() const {
    return m_x;
}

template<typename T1, typename T2> void Point<T1, T2>::setX(T1 x) {
    m_x = x;
}

template<typename T1, typename T2> T2 Point<T1, T2>::getY() const {
    return m_y;
}

template<typename T1, typename T2> void Point<T1, T2>::setY(T2 y) {
    m_y = y;
}

int main()
{
    // 与函数模板不同的是,类模板在实例化时必须显式地指明数据类型
    // 编译器不能根据给定的数据推演出数据类型
    Point<int, int> p1(10, 10);
    cout << "x=" << p1.getX() << ", y=" << p1.getY() << endl;

    Point<float, float> p2(12.88, 129.65);
    cout << "x=" << p2.getX() << ", y=" << p2.getY() << endl;

    Point<string, string> p3("E180","N210");
    cout << "x=" << p3.getX() << ", y=" << p3.getY() << endl;

    Point<int, float> p4(4, 129.65);
    cout << "x=" << p4.getX() << ", y=" << p4.getY() << endl;

    Point<string, int> p5("hello,world!", 5);
    cout << "x=" << p5.getX() << ", y=" << p5.getY() << endl;

    //除了对象变量,我们也可以使用对象指针的方式来实例化
    Point<string, int>* p7 = new Point<string, int>("hello,world!", 7);
    // (pointer_name)->(variable_name)
    // The Dot(.) operator is used to normally access members of a structure or union.
    // The Arrow(->) operator exists to access the members of the structure or the unions using pointers
    cout << "x=" << p7->getX() << ", y=" << p7->getY() << endl;
    
    delete p7;

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44884561/article/details/129042538