Hi there I decide to create a personalised Container for some specific reason and now I would like to implement an Iterator that will basically allow me to iterate over the elements of the container.
I looked into the following website in order to get an example, but I end in trouble when I tried to get elements out of the container in their initial format.
http://universalvoid.blogspot.com/20...or-in-stl.html
In order to be clear I will first tell what exactly I would like to do! I want to add some elements to my container then to transverse it using an Iterator. So basically I would like to make the following code to work:
Code:
Set teste;
for(int i=0; i<5; i++)
teste.add(i);
Set::Iter r = teste.begin();
for(; r != teste.end(); ++r) {
int result = (*r);
cout << result << " ";
}
cout << endl;
But I am getting the following error regarding the int result = (*r); line:
-error: invalid conversion from ‘const Set*’ to ‘int’
If I substitute int with const void* the error goes away! What I would like to achieve is to be able to cast the pointer (*rt) to int , which is not possible using my current implementation.
Can someone have a look at my Iterator implementation and suggest some changes that would solve this problem?
In the following two files is my Container and Iterator implementation!
Set.hpp - the nested class Iter it is my Iterator implementation.
Code:
#ifndef SET_HPP_
#define SET_HPP_
#include <iostream>
#include <list>
#include <assert.h>
using std::list;
class Set
{
unsigned int _size_;
list <int> s; // Set of elements
list <int> newlist(); // private function
public:
Set();
virtual ~Set();
Set(const Set&);
friend std::ostream& operator << (std::ostream&, const Set&);
int size();
int pop();
void add(int);
bool empty();
void clear();
// nested class
class Iter {
friend class Set;
typedef list <int>::iterator LI;
typedef list <int>::const_iterator cLI;
const Set* pC;
cLI i;
size_t n;
public:
Iter() : pC(0), n(0) { };
Iter(const Set* p_, cLI i_) : pC(p_), i(i_), n(0) { };
Iter(const Iter & i_) : pC(i_.pC), i(i_.i), n(i_.n) { };
Iter& operator++() {
if(pC) {
if( ++n < (pC->_size_) )
++i;
else
pC = 0;
return *this;
}
}
operator const Set* () {
return pC;
}
Iter& operator* () {
return (Iter&)(*i);
}
};
Iter begin() {
return Iter(this, s.begin());
}
Iter end() {
return Iter();
}
friend class Iter;
};
#endif /*SET_HPP_*/
Set.cpp - In this file there is just some functions implementation body considering only the Set class.
Code:
#include "Set.hpp"
/*
* Constructor
*/
Set::Set() {
_size_ = 0;
}
/*
* Terminator
*/
Set::~Set() {
this->clear();
}
/*
* Copy Constructor
*/
Set::Set(const Set& a) {
_size_ = a._size_;
s = newlist();
list <int> tmp = a.s;
list<int>::iterator rt;
for(rt = tmp.begin(); rt!= tmp.end() ; rt++)
s.push_back( (*rt) );
}
std::ostream& operator << (std::ostream& os, const Set& f) {
list<int> tmp = f.s;
list<int>::iterator rt;
os << "Set: ";
rt = tmp.begin();
for( rt = tmp.begin(); rt != tmp.end(); rt++ )
os << (*rt) << " ";
os << "Size: " << f._size_;
os << std::endl;
return os;
}
list <int> Set::newlist() {
list <int> p;
return p;
}
int Set::size() {
return _size_;
}
int Set::pop() {
assert( _size_ > 0 );
int value = 0;
list<int>::iterator rt;
rt = s.begin();
value = (*rt);
s.pop_front();
_size_--;
return value;
}
void Set::add(int c) {
s.push_back(c);
_size_++;
}
bool Set::empty() {
return s.empty();
}
void Set::clear() {
s.clear();
_size_ = 0;
}