In custom reverse vector iterator don't see first element - c++

The other day I wanted to try writing my own iterators for a vector, of course, the most primitive example, since there is a lot of confusing code in the c++ standards. So the usual iterator for a vector in the forward direction works fine, but there was a problem with the reverse iterator. I have it completely built on the base iterator, only I changed / inverted the operators specifically for the reverse iterator.
template<typename Vector>
class VectorRevIterator : public VectorIterator<Vector> //This is normal (work) vector iterator
{
public:
using Base = VectorIterator<Vector>;
VectorRevIterator(PointerType ptr) noexcept : Base(ptr) {};
VectorRevIterator(const VectorRevIterator& other) : Base(other) { *this = other; };
VectorRevIterator& operator++()
{
Base::operator--(); //--ptr;
return *this;
}
VectorRevIterator operator++(int)
{
VectorRevIterator itr = *this;
Base::operator--(); //--*this;
return itr;
}
VectorRevIterator& operator--()
{
Base::operator++(); //++ptr;
return *this;
}
VectorRevIterator operator--(int)
{
VectorRevIterator itr = *this;
Base::operator++(); //++*this;
return itr;
}
VectorRevIterator& operator+=(const PointerType otherPtr)
{
Base::operator-=(otherPtr); //ptr -= otherPtr;
return *this;
}
VectorRevIterator operator+(const PointerType otherPtr)
{
VectorRevIterator itr = *this;
Base::operator-(otherPtr); // itr -= otherPtr
return *this;
}
VectorRevIterator& operator-=(const PointerType otherPtr) { return Base::operator+=(otherPtr); }
VectorRevIterator operator-(const PointerType otherPtr) { Base::operator+(otherPtr); }
ReferenceType operator*() const { return *ptr; }
PointerType operator->() const { return std::_Const_cast(Base::operator->()); }
};
Access to iterator from Vector:
template<typename T>
class Vector
{
public:
using ValueType = T;
using PointerType = ValueType*;
using ReferenceType = ValueType&;
using ReverseIterator = VectorRevIterator<Vector<T>>;
public:
T* data;
size_t size;
size_t capacity;
...
// construct/destructor
// custom allocator
// index operators
...
ReverseIterator rBegin() { return ReverseIterator(data + size); }
ReverseIterator rEnd() { return ReverseIterator(data); }
};
The problem itself is that when I try to go through all the elements in the opposite direction VectorRevIterator. When trying to output all this to the console, it seems to shift one element forward and does not see/cannot read the characters of the first element. But then output all the elements, only without the last one.
Here a example:
Vector<String> values;
values.emplaceBack("1");
values.emplaceBack("2");
values.emplaceBack("3");
values.emplaceBack("4");
values.emplaceBack("5");
Vector<String>::ReverseIterator revIt = values.rBegin();
// output with spdlog
for (revIt; revIt != values.rEnd(); ++revIt)
INFO(*revIt); // error on first iteration, but print only 1, 2, 3, 4
// ouput with std::cout
for (revIt; revIt != values.rEnd(); ++revIt)
std::cout << *revIt << std::endl; // doesn't print anything
How solve this problem? To make reverse iterator it just need to revert operators ++ -- += -= and rBegin rEnd functions. Or maybe im forgotten about something?

rBegin() returns an iterator to the first element:
ReverseIterator rBegin() { return ReverseIterator(data + size); }
But it's pointing one element beyond the end and can't be dereferenced. You need to dereference the element before it.
You could therefore adjust the dereference operator in the VectorRevIterator version:
ReferenceType operator*() const { return *std::prev(ptr); }

Related

How to overload dereference operator of std::list for range-based for?

I am trying to handle std::list of pointer type, like this:
std::list<int*> pNums;
Originally, iterating this container with range-based for loop will look like this :
for(int* pNum : pNums)
{
std::cout << (*pNum) << std::endl;
}
However, I want to iterate this container with a value, not a pointer, like below:
for(int num : Range(pNums))
{
std::cout << num << std::endl;
}
|
Here, Range is a custom wrapping-class of std::list<int*>, something should be defined in this manner, I guess:
class Range
{
Range(std::list<int*>& _list) : list(_list) {}
std::list<int*>& list;
// Basically inherit the original iterator
class custom_const_iterator : std::list<int*>::const_iterator
{
// Define an overloaded dereference operator
const int& operator*() const
{
...
}
...
};
public:
custom_const_iterator begin() { return ...; }
custom_const_iterator end() { return ...; }
};
So, my question is, what should I write down for class Range?
I would take the following approach (explanation in the comments):
class Range
{
private:
// Store iterators to the begin and the end of the range,
// rather than a reference to the whole list
std::list<int*>::const_iterator first;
std::list<int*>::const_iterator last;
class iterator
{
private:
std::list<int*>::const_iterator it;
public:
explicit iterator(std::list<int*>::const_iterator i) : it(i) {}
// you should define all the other operators
// that a std::list iterator has!
iterator& operator++()
{
++it;
return *this;
}
iterator operator++(int)
{
it++;
return *this;
}
// just dereference to get the value
const int& operator*() const { return **it; }
// these two are quite important for basic functionality
bool operator==(const iterator& rhs) const { return it == rhs.it; }
bool operator!=(const iterator& rhs) const { return it != rhs.it; }
};
public:
Range(std::list<int*>& _list)
: first(_list.begin()), last(_list.end())
{}
public:
iterator begin() { return iterator(first); }
iterator end() { return iterator(last); }
};

Circular array (Queue) Iterator

I have an iterator class for a queue (implemented as a circular array). I attach the code below. The problem is with the ++ operator. Once it reaches the end of the array, it circles back to its beginning, so the iterator points to the 1st element. It works fine but I have no way of implementing then end() iterator using this method. The functions that return the begin() and end() iterators inside the queue class can be seen at the bottom. The end() iterator should point to the rear of the queue, but when the array is full and rear equals the size of the array is does not work, the ++ operator will circle back instead of allowing it to the return the true end(), which is the element pointed by the rear. Any suggestions regarding this issue?
class IteratorForwQueue : std::iterator<std::forward_iterator_tag, P*> {
public:
typedef IteratorForwQueue iter;
IteratorForwQueue(P* e, Queue* q) : elem(e), _queue(q) {}
IteratorForwQueue(const IteratorForwQueue& it, Queue* q) :
elem(it.elem), _queue(q) {}
iter& operator++() {
if(elem >= (_queue->_elems + (_queue->_size - 1)) &&
_queue->_rear != _queue->_size)
elem = &(_queue->_elems[0]); // circle back passed the array
else
++elem;
return *this;
}
P& operator*() { return *elem;}
P* operator->() { return elem; }
bool operator==(const iter& it) { return elem == it.elem; }
bool operator==(const P& e) { return e == *elem; }
bool operator!=(const iter& it) { return elem != it.elem; }
bool operator!=(const P& e) { return e != *elem; }
private:
P* elem;
Queue<P>* _queue;
}; // end of iterator class
// ....
IteratorForwQueue begin() { return IteratorForwQueue(_elems + _front, this); }
IteratorForwQueue end() { return IteratorForwQueue(_elems + _rear, this); }
How about this, based on the idea that end does not have to be a "past-the-end" iterator, it can be anything:
class IteratorForwQueue : std::iterator<std::forward_iterator_tag, P*> {
public:
typedef IteratorForwQueue iter;
IteratorForwQueue(P* e, size_t sz, size_t rear) : elem(e), _base(e), _sz(sz), _rear(rear) {}
IteratorForwQueue(const IteratorForwQueue& it, size_t sz) :
elem(it.elem), _base(it.elem), _sz(sz) {}
iter& operator++() {
if(elem - _base == _rear) {
elem = nullptr;
_base = 0;
_sz = 0;
_rear = 0;
return *this;
}
if(elem >= _base + (_sz - 1))
elem = _base; // circle back passed the array
else
++elem;
return *this;
}
P& operator*() { return *elem;}
P* operator->() { return elem; }
bool operator==(const iter& it) { return elem == it.elem; }
bool operator==(const P& e) { return e == *elem; }
bool operator!=(const iter& it) { return elem != it.elem; }
bool operator!=(const P& e) { return e != *elem; }
private:
P* elem;
P* _base; // can't access members of outer class
size_t _sz; // can't access members of outer class
size_t _rear;
}; // end of iterator class
// ....
IteratorForwQueue begin() { return IteratorForwQueue(_elems + _front, _size, _rear); }
IteratorForwQueue end() { return IteratorForwQueue(nullptr, 0, 0); }
const IteratorForwQueue cbegin() const { return IteratorForwQueue(_elems + _front, _size); }
const IteratorForwQueue cend() const { return IteratorForwQueue(_elems + _rear, _size); }
BTW, it would be better to have a pointer back to the original datastructure, make the iterator a friend class and fetch _base, _sz and _rear on demand.

Forward Iterator on a Stack

i have to implement a forward iterator on a stack based on arrays. I can't use std::vectors or anything, i just need that. My development of this program stopped when i begun with forward iterator, and in particular with the operators.
I have a method that takes a generic sequence, and from that, given an offset, creates a stack:
template <typename IterT>
stack(IterT begin, IterT end) : _stack(0), _size(0), _capacity(0) {
try {
for(; begin!=end; ++begin) {
push(static_cast<T>(*begin));
}
}
catch(...) {
clear(); //my method to destroy the stack
throw;
}
}
In my main i do the following:
int a[5] = {1, 2, 3, 4, 5};
stack<int> sint(a, a+5);
cout << sint << endl;
But when the code runs the stack is created but not printed. Can somebody help me? And also give me other helps(on code indentation, improvements, etc...) Thank you, I will post the iterator code forward.
class const_iterator {
const T* data;
unsigned int index;
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
const_iterator() : data(0){}
const_iterator(const T* arr) : data(arr) {}
const_iterator(const const_iterator &other)
: data(other.data){ }
const_iterator& operator=(const const_iterator &other) {
data = other.data;
return *this;
}
~const_iterator() {
data = 0;
}
reference operator*() const {
return *data;
}
pointer operator->() const {
return &(data);
}
const_iterator operator++(int) {
const_iterator tmp(*this);
++*this;
return tmp;
}
const_iterator& operator++() {
++data;
return *this;
}
bool operator==(const const_iterator &other) const {
return data[index] == other.data[index];
}
bool operator!=(const const_iterator &other) const {
return data[index] != other.data[index] ;
}
private:
friend class stack;
const_iterator(unsigned int ind) :
index(ind){}
}; // class const_iterator
const_iterator begin() const {
cout << "begin" << _stack[_size-1] << endl;
return const_iterator(_stack[_size-1]);
}
const_iterator end() const {
cout << "end" << _stack[0] << endl;
return const_iterator(_stack[0]);
}
Last but not least i redefined the << operator to fit the iterator:
template <typename T>
std::ostream &operator<<(std::ostream &os, const stack<T> &st) {
typename stack<T>::const_iterator i, ie;
for(i = st.begin(), ie = st.end(); i!=ie; ++i){
os << *i << std::endl;
}
return os;
}
The code for the stack is the following (I omitted something for readability).
stack()
: _capacity(0), _size(0), _stack(0){}
void push (const T &value){
if (_size == _capacity){ //raddoppio la dimensione
if(_capacity == 0)
++_capacity;
_capacity *= 2;
T* tmp = new T[_capacity];
copy_n(_stack, _size, tmp);
swap(_stack, tmp);
delete[] tmp;
}
_stack[_size] = value;
++_size;
}
void pop(){
T _tmp;
if(!is_empty()){
_tmp = _stack[_size-1];
--_size;
}
}
If you want to create an iterator that looks like a pointer, you don't need index, because data plays its role. Comparison operator should compare datas, not values:
bool operator==(const const_iterator &other) const {
return data == other.data;
}
If you want to create a reverse iterator, it is slightly more complex. First, operator++ should decrement data. Second, dereference operator should return not *data, but *(data - 1). Third, data in the begin() iterator should point to stack[size], and data in the end() iterator should point to stack[0]. You don't need a destructor in any case.
I followed the previous advices and here's the edited result, still i can't figure out how to properly use the constructor in the private section
class const_iterator {
const T *data;
public:
/* ITERATOR TRAITS HERE */
const_iterator() : data(0){}
const_iterator(const T* arr) : data(arr) {}
const_iterator(const const_iterator &other)
: data(other.data){ }
const_iterator& operator=(const const_iterator &other) {
data = other.data;
return *this;
}
~const_iterator() {
data = 0;
}
reference operator*() const {
return *data;
}
pointer operator->() const {
return &(data);
}
const_iterator operator++(int) {
const_iterator tmp(*this);
++*this;
return tmp;
}
const_iterator& operator++() {
++data;
return *this;
}
bool operator==(const const_iterator &other) const {
return data == other.data;
}
bool operator!=(const const_iterator &other) const {
return data != other.data;
}
private:
friend class stack;
const_iterator(const T *d) {
data = d;
}
}; // classe const_iterator
const_iterator begin() const {
return const_iterator(_stack[_size-1]);
}
const_iterator end() const {
return const_iterator(_stack[0]);
}

Custom C++ Iterator (Similar to std::find)

I need to create an iterator able to "find" a value base on input criteria. I am currently stumped on how I should approach this. (Custom vector class)
Here's my code for my iterator
class iterator {
public:
//Variables
int _index;
vector* _v;
//Constructors
iterator() : _index(0), _v(0) { }
iterator(int index, vector * vec) : _index(index), _v(vec) { }
//Functions
iterator& operator=(const iterator& itr){
_v = itr._v;
_index = itr._index;
return *this;
}
friend bool operator==(const iterator& itr_a, const iterator& itr_b){
return (itr_a._index == itr_b._index && itr_a._v == itr_b._v);
}
friend bool operator!=(const iterator& itr_a, const iterator& itr_b){
return !(itr_a == itr_b);
}
iterator& operator++ () { _index++; return *this; }
iterator& operator++ (int) { _index++; return *this; }
iterator& next() { _index++; return *this; }
int& operator*() { return (*_v)[_index]; }
};
Now this iterator code works then trying to use
for(vector::iterator i = vec.begin(); i != vec.end(); i++) std::cout << *i std::end;
Though now I need to create a "find" function in my main class (vector) that can wort with this following code
vector::iterator i = vec.find(5); //Prints out "5" x amount of times
for(; j != vec.end(); j.next()) std::cout << *j << std::endl;
The only issue I find confusing is, since in my iterator class next is classified as "add 1 to index", though if I return an iterator find, it's next would still be "add 1 to index" versus "find next value of element".
How can I go about trying to achieve this? It feels like a super simple answer. (Note, yes this is a school assignment to replicate features of std::vector, I will template my code eventually, I want to make it work with default ints first)

testing if my iterator is at the front of the list when decrementing

So I need to make try/catch for when the iterator is at the front of the list and the -- operator is used. I've tried checking to see if the iterator is equal to begin() but that doesn't seem to work.
template <typename E>
class List : public SLinkedList<E> {
public:
// NOTE THE DIFFERENT LETTER – IT IS ONLY USED HERE!
// Use E everywhere else! m
// For a nested class, methods are declared and defined *INSIDE*
// the class declaration.
template <typename I>
class Iterator {
public:
// Give List access to Iterator private fields.
friend class List<E>;
// These are the minimum methods needed.
E operator*() { return nodePosition->elem; } //dereference the iterator and return a value
Iterator<E> operator++() {
try {
if (nodePosition->next == nullptr)
throw OutsideList("\nIterator:Error: attempted acess position outside of list.");
nodePosition = nodePosition->next;
}
catch (OutsideList err) {
cout << err.getError() << endl;
}
return *this;
} //increment the iterator
Iterator<E> operator--() {
try {
if (nodePosition == llist.begin())
throw OutsideList("\nIterator:Error: attempted acess position outside of list.");
nodePosition = nodePosition->prev;
}
catch (OutsideList err) {
cout << err.getError() << endl;
}
return *this;
} //decrement the iterator
bool operator==(const Iterator<E> p) {
return (nodePosition == p)
} //test equality of iterators
bool operator!=(const Iterator<E> p) {
return (nodePosition != p.nodePosition);
} //test inequality of iterators
private:
// Constructors & destructor here since only want List class to access.
// List constructor called from List::begin(). Use initializer list or
// create class copy constructor and assignment overload.
Iterator(const List<E>* sl) : llist(sl) {
nodePosition = sl->head;
}
// Class fields.
const List<E>* llist; //give Iterator class a handle to the list
Node<E>* nodePosition; //abstracted position is a pointer to a node
}; /** end Iterator class **/
/* The Iterator class is now fully defined. The rest of these
statements must go AFTER the Iterator class or the compiler
won’t have complete information about their data types.
*/
// REQUIRED: While not necessary for the code to work, my test suite needs
// this defined. Create a less cumbersome name for Iterator<E>. Use
// anywhere you would have used List<E>::Iterator<E> in class List. Allows
// this syntax in main() -- List<int>::iterator instead of List<int>::Iterator<int>.
typedef typename List<E>::Iterator<E> iterator;
/*** All method declarations and fields for the List class go here.
Any method that returns an iterator must be defined here.
***/
iterator begin() const { //return an iterator of beginning of list
// Call iterator constructor with pointer to List that begin() was
// called with.
return iterator(this);
}
E back();
E pop_back();
void push_back(const E e);
iterator end() const {
iterator itr = iterator(this);
while (itr.nodePosition->next != nullptr) {
++itr;
}
++itr;
return itr;
}
void insert(int itr, E elem);
void erase(int itr);
};
bool operator==(const Iterator<E> p) {
return (nodePosition == p);
}
does not seem right. The main problem is that you are comparing nodePosition of the current object, the object on the LHS, with the object on the RHS.
You don't need Iterator<E>. Just Iterator is good enough.
There is no point in making the argument const Iterator. It can be just Iterator, or better const Iterator&.
Make the member function a const member function.
The comparison needs to be made with the corresponding nodePositions.
Here's what I think it should be:
bool operator==(const Iterator& p) const {
return (nodePosition == p.nodePosition);
}