⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7.9.4traits.cc

📁 C++ source code for book-C++ and Object Oriented Numeric computing for scientists and engineers
💻 CC
字号:

// using traits

#include <iostream>
#include <complex>
#include <algorithm>

template<class T> struct RTrait {
  typedef T RType;
};

template<class T> 
struct RTrait< std::complex<T> > {
  typedef T RType;
};

template<class T> 
typename RTrait<T>::RType maxnorm(T u[], int n) {
  typename RTrait<T>::RType norm = 0;
  for (int i = 0; i < n; i++) norm = std::max(norm, std::abs(u[i]));
  return norm;
}

int main() {

  int u[] = { 1, 2, 3, 4};

  std::complex<double> v[4];
  for (int i = 0; i < 4; i++) {
    //std::complex<double> a(i,i+1);
    //v[i] = a;
    v[i] = std::complex<double>(i,i+1);
  }
  for (int i = 0; i < 4; i++) std::cout << v[i] << '\n';

  std::cout << maxnorm(u, 4) << '\n';
  std::cout << maxnorm(v, 4) << '\n';

} // end main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -