1. 程式人生 > >C++語言基礎(18)-模板

C++語言基礎(18)-模板

ava col andro ted 獲取 聲明 精簡 androi int

Java中的泛型編程可以極大的提升編程的效率,比如在android中查找一個控件的ID:標準寫法為:

TextView tv_text = (TextView)findViewById(R.id.tv_text);

或者:

ImageView iv_img = (ImageView)findViewById(R.id.iv_img);

因為同為查詢控件ID,所以上面的寫法可以采用泛型編程精簡為:

protected final <T extends View> T getView(int id) {
        return (T) findViewById(id);
}

這樣在下次使用的時候就可以寫成這樣:

TextView tv_text = getView(R.id.tv_text);

C++中也有類似的東西,不過名字變了,叫模板(template)

一.函數模板

例:交換兩個相同類型變量的值

原始寫法:

//交換 int 變量的值
void Swap(int *a, int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}

//交換 float 變量的值
void Swap(float *a, float *b){
    float temp = *a;
    
*a = *b; *b = temp; } //交換 char 變量的值 void Swap(char *a, char *b){ char temp = *a; *a = *b; *b = temp; } //交換 bool 變量的值 void Swap(bool *a, bool *b){ char temp = *a; *a = *b; *b = temp; }

使用模板後的寫法:

#include <iostream>
using namespace std;

template
<typename T> void Swap(T *a, T *b){ T temp = *a; *a = *b; *b = temp; } int main(){ //交換 int 變量的值 int n1 = 100, n2 = 200; Swap(&n1, &n2); cout<<n1<<", "<<n2<<endl; //交換 float 變量的值 float f1 = 12.5, f2 = 56.93; Swap(&f1, &f2); cout<<f1<<", "<<f2<<endl; //交換 char 變量的值 char c1 = A, c2 = B; Swap(&c1, &c2); cout<<c1<<", "<<c2<<endl; //交換 bool 變量的值 bool b1 = false, b2 = true; Swap(&b1, &b2); cout<<b1<<", "<<b2<<endl; return 0; }

修改成引用:

#include <iostream>
using namespace std;

template<typename T> void Swap(T &a, T &b){
    T temp = a;
    a = b;
    b = temp;
}

int main(){
    //交換 int 變量的值
    int n1 = 100, n2 = 200;
    Swap(n1, n2);
    cout<<n1<<", "<<n2<<endl;
   
    //交換 float 變量的值
    float f1 = 12.5, f2 = 56.93;
    Swap(f1, f2);
    cout<<f1<<", "<<f2<<endl;
   
    //交換 char 變量的值
    char c1 = A, c2 = B;
    Swap(c1, c2);
    cout<<c1<<", "<<c2<<endl;
   
    //交換 bool 變量的值
    bool b1 = false, b2 = true;
    Swap(b1, b2);
    cout<<b1<<", "<<b2<<endl;

    return 0;
}

例:求三個數最大值:

#include <iostream>
using namespace std;

//聲明函數模板
template<typename T> T max(T a, T b, T c);

int main( ){
    //求三個整數的最大值
    int i1, i2, i3, i_max;
    cin >> i1 >> i2 >> i3;
    i_max = max(i1,i2,i3);
    cout << "i_max=" << i_max << endl;

    //求三個浮點數的最大值
    double d1, d2, d3, d_max;
    cin >> d1 >> d2 >> d3;
    d_max = max(d1,d2,d3);
    cout << "d_max=" << d_max << endl;

    //求三個長整型數的最大值
    long g1, g2, g3, g_max;
    cin >> g1 >> g2 >> g3;
    g_max = max(g1,g2,g3);
    cout << "g_max=" << g_max << endl;

    return 0;
}

//定義函數模板
template<typename T>  //模板頭,這裏不能有分號
T max(T a, T b, T c){ //函數頭
    T max_num = a;
    if(b > max_num) max_num = b;
    if(c > max_num) max_num = c;
    return max_num;
}

運行結果:

12 34 100
i_max=100
73.234 90.2 878.23
d_max=878.23
344 900 1000
g_max=1000

總結一下,函數模板的基本語法為:

template <typename 類型參數1 , typename 類型參數2 , ...> 返回值類型  函數名(形參列表){
    //在函數體中可以使用類型參數
}

二.類模板

類模板的聲明與函數模板的聲明類似:

template<typename 類型參數1 , typename 類型參數2 , …> class 類名{
    //TODO:
};

示例代碼:

#include <iostream>
using namespace std;

template<class T1, class 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坐標
};

template<class T1, class T2>  //模板頭
T1 Point<T1, T2>::getX() const /*函數頭*/ {
    return m_x;
}

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

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

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

int main(){
    Point<int, int> p1(10, 20);
    cout<<"x="<<p1.getX()<<", y="<<p1.getY()<<endl;
 
    Point<int, char*> p2(10, "東京180度");
    cout<<"x="<<p2.getX()<<", y="<<p2.getY()<<endl;
 
    Point<char*, char*> *p3 = new Point<char*, char*>("東京180度", "北緯210度");
    cout<<"x="<<p3->getX()<<", y="<<p3->getY()<<endl;

    return 0;
}

輸出結果:

x=10, y=20
x=10, y=東京180度
x=東京180度, y=北緯210度

註意:

1.在對類模板的成員函數進行定義時,除了 template 關鍵字後面要指明類型參數,類名 Point 後面也要帶上類型參數,只是不加 typename 關鍵字了

2.類模板在實例化時必須顯式地指明數據類型,賦值號兩邊也要指明具體的數據類型,且要保持一致

C++語言基礎(18)-模板