Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template<typename T> class Array
- {
- private:
- int size;
- T* array;
- public:
- Array(int n)
- {
- size = n;
- array = new T[size];
- }
- void set_value(int n, T value)
- {
- array[n] = value;
- }
- T get_min()
- {
- T temp = array[0];
- for (int i = 0; i < size; i++)
- {
- if (array[i] < temp)
- {
- temp = array[i];
- }
- }
- return temp;
- }
- T get_max()
- {
- T temp = array[0];
- for (int i = 0; i < size; i++)
- {
- if (array[i] > temp)
- {
- temp = array[i];
- }
- }
- return temp;
- }
- };
- template<> class Array<std::string>
- {
- private:
- int size;
- std::string* array;
- public:
- Array(int n)
- {
- size = n;
- array = new std::string[size];
- }
- void set_value(int n, std::string value)
- {
- array[n] = value;
- }
- std::string get_min()
- {
- std::string temp = array[0];
- for (int i = 0; i < size; i++)
- {
- if (array[i].length() < temp.length())
- {
- temp = array[i];
- }
- }
- return temp;
- }
- std::string get_max()
- {
- std::string temp = array[0];
- for (int i = 0; i < size; i++)
- {
- if (array[i].length() > temp.length())
- {
- temp = array[i];
- }
- }
- return temp;
- }
- };
- int main()
- {
- Array<int> int_array(3);
- int_array.set_value(0, 1);
- int_array.set_value(1, 2);
- int_array.set_value(2, 3);
- std::cout << int_array.get_min() << "\n";
- std::cout << int_array.get_max() << "\n";
- Array<char> char_array(6);
- char_array.set_value(0, 'A');
- char_array.set_value(1, 'E');
- char_array.set_value(2, 'Z');
- char_array.set_value(3, 'K');
- char_array.set_value(4, 'M');
- char_array.set_value(5, 'I');
- std::cout << char_array.get_min() << "\n";
- std::cout << char_array.get_max() << "\n";
- Array<std::string> string_array(2);
- string_array.set_value(0, "aaaaa");
- string_array.set_value(1, "zzz");
- std::cout << string_array.get_min() << "\n";
- std::cout << string_array.get_max() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement