Hello,

I am trying to make a constant iterator data structure, I have a normal Iterator data structure done, but I'm a little stumped with the const iterators.. can you please point me in the right direction?

This is my data structure for the iterators:

template <typename T>
class Iter : public std::iterator<std::forward_iterator_tag, T, void>
{
public:
	//default constructor
	Iter() : cur (0), the_list (0) { }
	//copy constructor
	Iter(Iter<T>& i) {
		cur = i.cur;
		the_list = i.the_list;
	}
	
	bool operator==(Iter<T>& i) const
	{ 
		return cur == i.cur;
	};
	
	bool operator != (Iter<T>& i) const 
	{
		return cur != i.cur;
	};

	//Iterator basic functions
	T& operator*();	//retrieve data
	Iter& operator++();	//increment
	Iter operator++ (int);
	Iter& operator--();	//decrement
	Iter operator--(int);

private:	//data member of class Iter.
	friend class List <T>;
	friend class cIter<T>;
	Node<T> *cur;
	List<T> *the_list;	//hooked to a particular list
	//constructor accessible only from friend class
	Iter (List<T> *l, Node<T> *n) : the_list(l), cur(n) { }; 
};

Thanks so much.

What's teh problem with:

typedef const std::iterator<std::forward_iterator_tag, T, void> Iter;
Iter my_const_iter;

In any case post the problems you have..

What's teh problem with:

typedef const std::iterator<std::forward_iterator_tag, T, void> Iter;
Iter my_const_iter;

Of course "T" has to be replaced with actual type for typdef to work.

Also remembered, almost all STL structs I've worked with provide both iterator and const_iterator as types.
E.g.

#include <vector>

using std::vector;

int main() {
    typedef vector<int> intvec;
    intvec vi(6) ;
    int arr[] = {1,2,3,4,5,6};
    std::copy(arr, arr+6, vi.begin());

    intvec::iterator it = vi.begin();
    for ( ; it < vi.end(); it++ )
        printf("%d\n", *it);

    intvec::const_iterator cit = vi.begin();
    for ( ; cit < vi.end(); cit++ )
        printf("%d\n", *cit);

   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.