fork(1) download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. template <typename T>
  7. auto maxn(const T * pArr, int n)->decltype (*pArr)
  8. {
  9. int f(0);
  10. for (int i = 0; i < n; i++)
  11. if (pArr[i] > pArr[f])
  12. f = i;
  13. return pArr[f];
  14. }
  15. template <>
  16. auto maxn<const char *>(const char * const *pArr, int n)->decltype(*pArr)
  17. {
  18. int *pArr1 = new int [n];
  19. for (int i = 0; i < n; i++)
  20. pArr1[i] = strlen(pArr[i]);
  21. int f(0);
  22. for (int i = 0; i < n; i++)
  23. if (pArr1[i] > pArr1[f])
  24. f = i;
  25. delete [] pArr1;
  26. return pArr[f];
  27. }
  28. int main(int argc, char* argv[])
  29. {
  30. srand(unsigned (time(0)));
  31. const int k = 5;
  32. int *pArr = new int [k];
  33. for (int i = 0; i < k; i++)
  34. pArr[i] = rand() % 11;
  35. for (int i = 0; i < k; i++)
  36. cout << pArr[i] << ' ';
  37. cout << endl;
  38. cout << "The max member of array is " << maxn(pArr, k) << endl;
  39. delete [] pArr;
  40.  
  41. double *pArr1 = new double [k];
  42. for (int i = 0; i < k; i++)
  43. pArr1[i] = i * 0.5;
  44. for (int i = 0; i < k; i++)
  45. cout << pArr1[i] << ' ';
  46. cout << endl;
  47. cout << "The max member of array is " << maxn(pArr1, k) << endl;
  48. delete [] pArr1;
  49.  
  50. char str[k][15] = {"Hello!", "Good bye!", "What's up!", "Yo, man!", "Hi, byotch!"};
  51. char **ppStr = new char *[k];
  52. for (int i = 0; i < k; i++)
  53. ppStr[i] = new char [20];
  54. for (int i = 0; i < k; i++)
  55. memcpy(ppStr[i], str[rand() % 5], 20);
  56. for (int i = 0; i < k; i++)
  57. cout << ppStr[i] << endl;
  58. cout << maxn(ppStr, k) << endl;
  59. for (int i = 0; i < k; i++)
  60. delete [] ppStr[i];
  61. delete [] ppStr;
  62. // if (_CrtDumpMemoryLeaks())
  63. // cout << "Memory Leaks\n";
  64. // else
  65. // cout << "All good\n";
  66. return 0;
  67. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
6 3 10 4 8 
The max member of array is 10
0 0.5 1 1.5 2 
The max member of array is 2
Yo, man!
Hi, byotch!
Hi, byotch!
Hello!
Yo, man!
Yo, man!