c++ template

1 c++ template示例

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
    T a, b;
  public:
    mypair (T first, T second)
      {a=first; b=second;}
    T getmax ();
};

template <class T>
T mypair<T>::getmax ()
{
  T retval;
  retval = a>b? a : b;
  return retval;
}

int main () {
  mypair <int> myobject (100, 75);
  cout << myobject.getmax();
  return 0;
}

2 定义类和类外函数的时候,都是直接在前面加上template <class T>即可,并且定义类外函数的时候要加上<T>的限定

 
原文地址:https://www.cnblogs.com/hustdc/p/10362539.html