0% found this document useful (0 votes)
4 views

Iterator Class

c++

Uploaded by

nandinimani2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Iterator Class

c++

Uploaded by

nandinimani2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Iterator class

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

template<class T>
class arrayListIterator
{
public:
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;

arrayListIterator(T* thePosition = nullptr) : position(thePosition) {}

T& operator*() const { return *position; }


T* operator->() const { return &*position; }

arrayListIterator& operator++()
{
++position;
return *this;
}

arrayListIterator operator++(int) {
arrayListIterator old = *this;
++position;
return old;
}

arrayListIterator& operator--() {
--position;
return *this;
}

arrayListIterator operator--(int)
{
arrayListIterator old = *this;
--position;
return old;

2
}

bool operator!=(const arrayListIterator& right) const {


return position != right.position;
}

bool operator==(const arrayListIterator& right) const {


return position == right.position;
}

protected:
T* position; // Pointer to a list element
};

// A simple array-based list class


template<class T>
class arrayList {
public:
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>& theList);
~arrayList() { delete[] element; }

typedef arrayListIterator<T> iterator;

3
bool empty() const { return listSize == 0; }
int size() const { return listSize; }
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;
int capacity() const { return arrayLength; }

iterator begin() { return iterator(element); }


iterator end() { return iterator(element + listSize); }

protected:
void checkIndex(int theIndex) const;

T* element;
int arrayLength;
int listSize;
};

template<class T>

4
arrayList<T>::arrayList(int initialCapacity) {
if (initialCapacity < 1) {
throw invalid_argument("Initial capacity must be > 0");
}
arrayLength = initialCapacity;
element = new T[arrayLength];
listSize = 0;
}

template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList) {
arrayLength = theList.arrayLength;
listSize = theList.listSize;
element = new T[arrayLength];
for (int i = 0; i < listSize; i++) {
element[i] = theList.element[i];
}
}

template<class T>
T& arrayList<T>::get(int theIndex) const {
checkIndex(theIndex);
return element[theIndex];

5
}

template<class T>
int arrayList<T>::indexOf(const T& theElement) const {
for (int i = 0; i < listSize; i++) {
if (element[i] == theElement) {
return i;
}
}
return -1; // Element not found
}

template<class T>
void arrayList<T>::erase(int theIndex) {
checkIndex(theIndex);
for (int i = theIndex + 1; i < listSize; i++) {
element[i - 1] = element[i];
}
listSize--;
}

template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement) {

6
if (theIndex < 0 || theIndex > listSize) {
throw out_of_range("Invalid index");
}
if (listSize == arrayLength) {
// Resize array if necessary
T* newArray = new T[2 * arrayLength];
for (int i = 0; i < listSize; i++) {
newArray[i] = element[i];
}
delete[] element;
element = newArray;
arrayLength *= 2;
}
for (int i = listSize - 1; i >= theIndex; i--) {
element[i + 1] = element[i];
}
element[theIndex] = theElement;
listSize++;
}

template<class T>
void arrayList<T>::output(ostream& out) const {
for (int i = 0; i < listSize; i++) {

7
out << element[i] << " ";
}
out << endl;
}

template<class T>
void arrayList<T>::checkIndex(int theIndex) const {
if (theIndex < 0 || theIndex >= listSize) {
throw out_of_range("Index out of range");
}
}

int main() {

arrayList<int> list(5);
list.insert(0, 10);
list.insert(1, 20);
list.insert(2, 30);
list.insert(3, 40);
list.insert(4, 50);

cout << "List elements: ";

8
for (arrayList<int>::iterator it = list.begin(); it != list.end(); ++it) {
cout << *it << " ";
}
cout << endl;

arrayList<int>::iterator it = list.begin();
++it;
cout << "Element after pre-increment: " << *it << endl;

it++;
cout << "Element after post-increment: " << *it << endl;

--it;
cout << "Element after pre-decrement: " << *it << endl;

it--;
cout << "Element after post-decrement: " << *it << endl;

return 0;
}

9
List elements: 10 20 30 40 50
Element after pre-increment: 20
Element after post-increment: 30
Element after pre-decrement: 20
Element after post-decrement: 10

10

You might also like