fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>
  7. class MyContainer {
  8. public:
  9.  
  10. MyContainer(int size) {
  11. this->size = size;
  12.  
  13. this->array = new T[this->size];
  14.  
  15. }
  16.  
  17. T& operator[](int index) {
  18.  
  19.  
  20. if (index >= this->size || index < 0) {
  21.  
  22. cout << "Error, index was out of range!!!" << endl;
  23.  
  24. return this->array[0];
  25. }
  26.  
  27.  
  28. return this->array[index];
  29.  
  30. }
  31.  
  32. class Iterator;
  33.  
  34. Iterator begin() { return this->array; }
  35. Iterator end() { return this->array + this->size; }
  36.  
  37. class Iterator
  38. {
  39. private:
  40. T* cur;
  41. public:
  42. Iterator(T *first) : cur(first)
  43. {}
  44.  
  45. T& operator+ (int n) { return *(cur + n); }
  46. T& operator- (int n) { return *(cur - n); }
  47.  
  48. Iterator operator++ (int) { return cur++; }
  49. Iterator operator-- (int) { return cur--; }
  50.  
  51. Iterator& operator++ () { ++cur; return *this; }
  52. Iterator& operator-- () { --cur; return *this; }
  53.  
  54. bool operator!= (const Iterator& it) { return cur != it.cur; }
  55. bool operator== (const Iterator& it) { return cur == it.cur; }
  56.  
  57. T& operator* () { return *cur; }
  58.  
  59. };
  60.  
  61. ~MyContainer() {
  62. delete[] this->array;
  63. }
  64.  
  65. private:
  66. int size;
  67. T* array;
  68. };
  69.  
  70. int main() {
  71.  
  72. int size = 5;
  73.  
  74. MyContainer<int> Con(size);
  75.  
  76. for (int i = 0; i < size; i++) {
  77. Con[i] = i * 2;
  78. }
  79.  
  80. cout << endl << endl;
  81.  
  82. auto it1 = MyContainer<int>::Iterator(Con.begin());
  83.  
  84. auto it2 = MyContainer<int>::Iterator(Con.end());
  85.  
  86. auto result = max_element(it1, it2);
  87.  
  88. cout << *result << endl;
  89.  
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0.01s 5504KB
stdin
Standard input is empty
stdout

8