# I am having a problem with storing an iterator to an object

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

## Recommended Posts

Hi, I am using C++. I am storing an iterator pointing to an 'apple' in a class called 'orange'. I am trying to use the iterator to set a variable directly rather than iterating through all of the apples. However, upon trying to set a variable to false using the iterator, my app quits/crashes.
//Class Orange:
list<CApple>::iterator m_itApple;

//I set the iterator by iterating through the apples at the begining and
//then setting the iterator,eg it1=it2.

//Somewhere in Class Pear (in a function) I try to set a variable to false.
//But it crashes.
Orange.m_itApple->m_bVariable=false;


I have tried doing it this way once: list<CApple*>::iterator m_itApple; That works, but I don't want to make all my apples into pointers. Anyone able to help?

##### Share on other sites
Are you sure the iterator really points to a valid list element, and not, say, to the end of the list?

##### Share on other sites
That's not the problem.

I have found another way to do it actually. I will store the index to my vector.
eg
//I've decided to go with vector now
vector<CApple>::iterator m_itApple;

And access like this:
Orange.m_itApple[nIndex].m_bVariable=false;

-----
Now, I only have one question.

How do I get the current index from an iterator, eg
iterator=begin
index would = 0

iterator++
index would = 1

##### Share on other sites
For (almost) any iterator type, std::distance(begin, itor) or, for std::vector, which has random-access iterators, just itor-begin.

Edit: note that, internally, vector will do begin+index to get to the object, so if it doesn't work one way, it's not likely to work the other.

##### Share on other sites
Quote:
 Original post by FrunyFor (almost) any iterator type, std::distance(begin, itor)

Like this?

nIndex=distance(m_vecApple.begin(),itApple);

Quote:
 Original post by Fruny or, for std::vector, which has random-access iterators, just itor-begin.

Can you elaborate on this, maybe with some example code? I could not find a function called "itor-begin", so could you elaborate?

##### Share on other sites
That's not a function, it's a difference: operator- operating on the two iterators "itor" (i.e. the current iterator) and "begin" (i.e. the result of .begin()). :)

##### Share on other sites
Quote:
 Original post by ZahlmanThat's not a function, it's a difference: operator- operating on the two iterators "itor" (i.e. the current iterator) and "begin" (i.e. the result of .begin()). :)

Can someone show me how to use these operators?
is it like this:
int nIndex=itApple.operator - m_vecApple.begin()

I tried using the distance function on an iterator for a vector,eg
nIndex=distance(m_vecApple.begin(),itApple);

but my app crashes on that line.

##### Share on other sites
I think that the guy's mean that you need to find the difference between your current interator and the start of the container (conatiner.begin()):

container.currIter - container.begin();

##### Share on other sites
ok thanks. That's what I wanted.