Hello. Relatively recently, they helped me to deal with iterators, more precisely to understand their implementation, etc. (thanks to those who helped). Here we will create one more question (for he has exhausted his task).
The point is this ... I prescribed these iterators, it would seem, I understood how that what is more or less .. The result was the following class:
class Array { private: int n; int* arr; public: class Iterator { public: int *p; Iterator() { p = nullptr; } Iterator(int *pv) { p = pv; } int operator*() const { return *p; } /*Iterator operator++(int) { int* temp = p; return ++temp; }*/ Iterator& operator++() { ++p; return *this; } Iterator operator++(int) { return Iterator(p++); } Iterator& operator--() { --p; return *this; } Iterator operator--(int) { return Iterator(p--); } const int& operator[](const std::size_t&pv) { return p[pv]; } Iterator& operator+=(const std::size_t&pv) { p += pv; return *this; } int* base() const { return p; } Iterator operator=(const int a) { *p = a; return *this; } }; Iterator begin() { return Iterator(arr); } Iterator end() { return arr+n; } friend inline bool operator==(const Iterator&it1, const Iterator&it2) { return it1.base() == it2.base(); } friend inline bool operator!=(const Iterator&it1, const Iterator&it2) { return it1.base() != it2.base(); } friend inline bool operator <(const Iterator&it1, const Iterator&it2) { return it1.base() < it2.base(); } friend inline bool operator >(const Iterator&it1, const Iterator&it2) { return it1.base() > it2.base(); } friend inline bool operator <=(const Iterator&it1, const Iterator&it2) { return it1.base() <= it2.base(); } friend inline bool operator >=(const Iterator&it1, const Iterator&it2) { return it1.base() >= it2.base(); } friend Iterator operator+(const Iterator&it, std::size_t n) { return Array::Iterator(it.base() + n); } friend Iterator operator-(const Iterator&it, std::size_t n) { return Array::Iterator(it.base() - n); } ... }
Everything seems fine ... But now I tried to remake the sorting algorithms for these iterators. Well .. Take for example the simplest InsertionSort: Without iterators it was like this:
void Array::InsertionSort() { int i, j, key; for (i = 1; i < length; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } }
After I added iterators, it turned out like this:
void Array::InsertionSort() { int key; Iterator start = Array::begin(); Iterator end = Array::end(); Iterator temp; for (++start; start != end; start++) { key = *start; temp = start - 1; while (temp >= Array::begin() && *temp > key) { (temp+1) = *temp; --temp; } compares++; (temp+1) = key; } }
Did you understand the concept correctly or not? Is there any way to optimize this?
And yet ... I wanted to compare the element and change their places, so that there was something like this
*it1 = *it2;
Such a record did not work, so after an hour of attempts I stopped at this overload:
Iterator operator=(const int a) { *p = a; return *this; }
Perhaps there is a better option? And is it possible to overload as I wanted? ( *it1 = *it2
);
And what about sorting algorithms such as shaker or fast ... Same general bounds ... And you need to take by index .. How do iterators use in this case? - Maybe using overloaded [] or is it more optimal?