C++中的template <typename T>用法詳解

template <typename T> 是C++中用于定義模板的固定格式。模板是實現(xiàn)代碼重用機制的一種工具,它可以實現(xiàn)類型參數(shù)化,即把類型定義為參數(shù), 從而實現(xiàn)了真正的代碼可重用性。模版可以分為兩類,一個是函數(shù)模版,另外一個是類模版。

第一,函數(shù)模板。
功能要求:我們需要對int、char、string、double等類型的數(shù)據(jù)做交換操作,假如沒有模板這種重用代碼的機制,則我們需要根據(jù)不同的參數(shù)類型編寫多個語句基本相同的函數(shù),有了模板功能,則只需要編寫一個函數(shù)即可,編譯器可以通過輸入?yún)?shù)的類型,推斷出形參的類型。范例代碼如下:

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

//template 關鍵字告訴C++編譯器 下面是個泛型模板  
//數(shù)據(jù)類型T 參數(shù)化數(shù)據(jù)類型
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;
}
函數(shù)模板范例運行結果

第二,類模板
功能要求:定義一個類來表示坐標,要求坐標的數(shù)據(jù)類型可以是整數(shù)、小數(shù)或字符串,例如:

  • 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 類的成員函數(shù)進行定義
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()
{
    // 與函數(shù)模板不同的是,類模板在實例化時必須顯式地指明數(shù)據(jù)類型
    // 編譯器不能根據(jù)給定的數(shù)據(jù)推演出數(shù)據(jù)類型
    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;
}
類模板范例運行結果
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容