C++模板超详细介绍

  #include

  #include

  using namespace std;

  template

  inline T const& Max(T const& a, T const& b)

  {

  return a < b ? b : a;

  }

  int main()

  {

  int x1 = 114514;

  int x2 = 121212;

  cout << "Max: " << Max(x1, x2) << endl;//隐式调用

  int x3 = 114514;

  int x4 = 'a';

  //cout << "Max: " << Max(x3, x4) << endl;//无法通过编译

  cout << "Max: " << Max(x3, x4) << endl;//显示调用

  double f1 = 13.5;

  double f2 = 24.6;

  cout << "Max: " << Max(f1, f2) << endl;

  string s1 = "Yuta";

  string s2 = "Rokka";

  cout << "Max: " << Max(s1, s2) << endl;

  return 0;

  }