SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <cstring> | |
| 3 | ||
| 4 | template <typename T> | |
| 5 | const T &max(T*, int); | |
| 6 | template <> const char *max(const char**, int); | |
| 7 | ||
| 8 | int main(void) {
| |
| 9 | int int_array[5] = {0, 1, 2, 3, 4};
| |
| 10 | double double_array[5] = {0.1, 1.1, 2.1, 3.1, 4.1};
| |
| 11 | const char *c_str[] = {"abc", "defg", "hijklmn", "op"};
| |
| 12 | ||
| 13 | std::cout << max(c_str, 4) << '\n'; | |
| 14 | std::cout << max(int_array, 5) << '\n'; | |
| 15 | std::cout << max(double_array, 5) << '\n'; | |
| 16 | ||
| 17 | std::cin.ignore(std::numeric_limits<int>::max(), '\n'); | |
| 18 | return 0; | |
| 19 | } | |
| 20 | ||
| 21 | template <typename T> | |
| 22 | const T &max(T *type, int n) {
| |
| 23 | int k = 0; | |
| 24 | - | for(int i = 0; i < n; ++i) {
|
| 24 | + | for(int i = 0; i < n; ++i) |
| 25 | if(type[k] < type[i]) | |
| 26 | k = i; | |
| 27 | - | } |
| 27 | + | |
| 28 | } | |
| 29 | ||
| 30 | template <> const char *max(const char **str, int nPointers){
| |
| 31 | int k = 0; | |
| 32 | for(int i = 0; i < nPointers; ++i) | |
| 33 | if(std::strlen(str[k]) < std::strlen(str[i])) | |
| 34 | k = i; | |
| 35 | return str[k]; | |
| 36 | } |