Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Random Access Iterator Create an iterator class that, using templates, can opera

ID: 3677852 • Letter: R

Question

Random Access Iterator

Create an iterator class that, using templates, can operate on any random-access sequence container. Random-access containers are containers that can be accessed using the [ ] operator. An example of such a container is vector. An example iterator type would be MyIterator<vector<int>>.

The class must have a single constructor, that takes a reference to a container and an index to start the iterator.

This class must support operator overloads for:

* - go to the actual item referenced by the iterator

++ - move the iterator forward to the next item

-- - move the iterator backward to the previous item

== - check to see if the current iterator is at the same position in same the random-access sequence container as another

!= - check to see that the current iterator is at a different position or a different sequence container

Explanation / Answer


#include <iostream>
#include <vector>
using namespace std;

template <typename T, typename itemType>
class MyIterator {
    T container;
    long index;
  
public:
    MyIterator(T& con, long idx){
        this->container = con;
        this->index = idx;
    }
  
    itemType operator*() const {
      
        return this->container[index];
    }
  
    MyIterator& operator++(){
        ++index;
        return *this;
    }
  
    MyIterator& operator++(int){
        ++index;
        return *this;
    }
  
    MyIterator& operator--(int){
        --index;
        return *this;
    }
  
    MyIterator& operator--(){
        --index;
        return *this;
    }
  
    bool operator==(const MyIterator & other){
        return this->index == other.index;
    }
  
    bool operator!=(const MyIterator & other){
        return this->index != other.index;
    }
};

int main(int argc, const char * argv[]) {
    vector<int> vec = {1,2,3,4,5,6,7,8,9,10};

    auto iter = MyIterator<vector<int>, int>(vec, 1L);
    auto end = MyIterator<vector<int>, int>(vec,vec.size());
  
    cout <<"We init the iter at position 1, the actual item is: " << *iter<<endl;
  
    for (; iter != end; iter++) {
        cout << *iter <<endl;
    }
  
  
    return 0;
}

output

We init the iter at position 1,                                                                                                                             
the actual item is: 2                                                                                                                                       
2                                                                                                                                                           
3                                                                                                                                                           
4                                                                                                                                                           
5                                                                                                                                                           
6                                                                                                                                                           
7                                                                                                                                                           
8                                                                                                                                                           
9                                                                                                                                                           
10