Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- template <typename T>
- const T &max(T*, int);
- template <> const char *max(const char**, int);
- int main(void) {
- int int_array[5] = {0, 1, 2, 3, 4};
- double double_array[5] = {0.1, 1.1, 2.1, 3.1, 4.1};
- const char *c_str[] = {"abc", "defg", "hijklmn", "op"};
- std::cout << max(c_str, 4) << '\n';
- std::cout << max(int_array, 5) << '\n';
- std::cout << max(double_array, 5) << '\n';
- std::cin.ignore(std::numeric_limits<int>::max(), '\n');
- return 0;
- }
- template <typename T>
- const T &max(T *type, int n) {
- int k = 0;
- for(int i = 0; i < n; ++i)
- if(type[k] < type[i])
- k = i;
- return type[k];
- }
- template <> const char *max(const char **str, int nPointers){
- int k = 0;
- for(int i = 0; i < nPointers; ++i)
- if(std::strlen(str[k]) < std::strlen(str[i]))
- k = i;
- return str[k];
- }
Advertisement
Add Comment
Please, Sign In to add comment