const_iterator to iterator

Started by
10 comments, last by snooty 16 years, 6 months ago
Hi, Is it possible to turn a const_iterator to an iterator and vice versa?
Advertisement
Not without some ugly hacks. The point in a const_iterator is that it's const, and you can't change it. I'm not actually sure if it's possible the other way around.

What are you trying to do exactly?
It it iterator is for a random access container such as std::vector, you may be able to do this:

void foo( std::vector<Foo> &foos, std::vector<Foo>::const_iterator it ){    int index = std::distance(foos.begin(),it);    std::vector<Foo>::iterator nonConst = foos.begin() + index;    // use nonConst}
In some cases, you can determine the iterator's position in the sequence using std::distance and, using the non-constant version of the sequence, advance a non-constant iterator by that amount.

If all you have is a constant iterator (that is, you don't have access to the non-constant sequence), then it's impossible and nonsensical to extract a non-constant iterator from a constant one.
If you have access to the original container c, then you can convert a const_iterator ci into the container into a non-const iterator with:
iterator i = c.begin();std::advance(i, std::distance<const_iterator>(i, ci));


Highly inefficient for non-random access iterator, but it works.
Just to clarify Evil Steve's "other way around" scenario: an iterator should be convertible to a const_iterator. Some STL implementations (Dinkumware for example) from memory make iterator inherit from const_iterator to handle this.
I just thought, since iterators are essentially pointers, and there is const_cast for pointers, there should be some non-expensive ways to apply the effects of const_cast on iterators.
Quote:Original post by snooty
I just thought, since iterators are essentially pointers, and there is const_cast for pointers, there should be some non-expensive ways to apply the effects of const_cast on iterators.


No.

It's actually the reverse: pointers are iterators. The vast majority of iterators are not pointers.
Quote:Original post by snooty
I just thought, since iterators are essentially pointers, and there is const_cast for pointers, there should be some non-expensive ways to apply the effects of const_cast on iterators.


Can we see more of the surrounding code?

The real question, of course, is: why is the iterator const in the first place, if you wish to use it in a non-const fashion?
The original code is rather complicated, so let me try to abstract it:

class C{public:   std::vector<int> v;   mutable std::vector<int>::const_iterator citer;   //line 1   //a funstion that sets up citer for Read()   void BeginRead() const   {      citer = v.begin();   //line 2   }   //a function that reads 1 element from v each time   int Read() const   {      return *(citer++);   }   //a function that erase an element from v   void Remove()   {      citer = v.erase(citer);   //line 3   }};

Line 3 needs a non-const iterator for erase().
If I use a non-const iterator in line 1, line 3 will be fine but line 2 will fail because v is const.

This topic is closed to new replies.

Advertisement