fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <type_traits>
  4.  
  5. using namespace std;
  6.  
  7. template<typename Arr>
  8. auto maxValue(const Arr& a) -> typename remove_all_extents<Arr>::type
  9. {
  10. static_assert(rank<Arr>::value,"[] type only :)");
  11. using T = typename remove_all_extents<Arr>::type;
  12. T res = numeric_limits<T>::min();
  13. if constexpr(rank<Arr>::value == 1)
  14. {
  15. for(int i = 0; i < extent<Arr>::value; ++i)
  16. if (res < a[i]) res = a[i];
  17. }
  18. else
  19. {
  20. for(int i = 0; i < extent<Arr>::value; ++i)
  21. {
  22. T val = maxValue(a[i]);
  23. if (res < val) res = val;
  24. }
  25. }
  26. return res;
  27. }
  28.  
  29.  
  30. int main(int argc, char * argv[])
  31. {
  32. int a3[3][2][2] = {
  33. {{1,2},{3,4}},
  34. {{5,6},{7,8}},
  35. {{0,2},{2,4}},
  36. };
  37. int a1[5] = { 1, 5, 2, 9, 7 };
  38.  
  39. cout << maxValue(a3) << endl;
  40. cout << maxValue(a1) << endl;
  41. }
  42.  
Success #stdin #stdout 0s 4956KB
stdin
Standard input is empty
stdout
8
9