Jump to content
  • Advertisement
Sign in to follow this  
snooty

const_iterator to iterator

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!