Help with STL

A

angela.xuli

I have following code:

class ABC
{
...
....
int getValueA()

}

deque<ABC *> ABCptrque;

deque<ABC*>::iterator it;

for (it=ABCptrque.end(); it !=ABCptrque.begin(); it--)
{

int m = (*it)->getValueA();

}

Program crashes at line: int m = (*it)->getValueA();

Thank you in advance.

Angela
 
V

Victor Bazarov

I have following code:

class ABC
{
..
...
int getValueA()

}
;
deque<ABC *> ABCptrque;

deque<ABC*>::iterator it;

for (it=ABCptrque.end(); it !=ABCptrque.begin(); it--)
{

int m = (*it)->getValueA();

}

Program crashes at line: int m = (*it)->getValueA();

If you want iterate backward, you should use reverse_iterator:

for (deque<ABC*>::reverse_iterator it = ABCptrque.rbegin();
it != ABCptrque.rend(); ++it)
{
...
}

Otherwise you're trying to dereference the 'end()' iterator,
which has undefined behaviour.

V
 
M

Mark P

I have following code:

class ABC
{
..
...
int getValueA()

}

deque<ABC *> ABCptrque;

deque<ABC*>::iterator it;

for (it=ABCptrque.end(); it !=ABCptrque.begin(); it--)
{

int m = (*it)->getValueA();

}

Program crashes at line: int m = (*it)->getValueA();

You can't dereference an iterator at the end-- remember that end() is
"one past" the last element of the container. Try a reverse iterator
instead:

for( deque<ABC*>::reverse_iterator it = ABCptrque.rbegin();
it != ABCptrque.rend(); ++it)
{
int m = (*it)->getValueA();
}

Or, if you insist on using a forward iterator:

for( it = ABCptrque.end(); it !=ABCptrque.begin(); )
{
--it; // decrement once before using
int m = (*it)->getValueA();
}

But you can probably see that the reverse iterator is a more natural fit
here.

-Mark
 
A

angela.xuli

Yes, reverse_iterator helps. However, I need to insert after I find
the right position. insert only takes iterator instead of reverse
iterator.
 
V

Victor Bazarov

Yes, reverse_iterator helps. However, I need to insert after I find
the right position. insert only takes iterator instead of reverse
iterator.

Use .base() . And RTFM. And get a good book.

V
 
B

BobR

Mark P wrote in message...
You can't dereference an iterator at the end-- remember that end() is
"one past" the last element of the container. Try a reverse iterator
instead:

for( deque<ABC*>::reverse_iterator it = ABCptrque.rbegin();
it != ABCptrque.rend(); ++it)
{
int m = (*it)->getValueA();
}

Or, if you insist on using a forward iterator:

for( it = ABCptrque.end(); it !=ABCptrque.begin(); )
{
--it; // decrement once before using
int m = (*it)->getValueA();
}

But you can probably see that the reverse iterator is a more natural fit
here.
-Mark

There was a thread a while back about *const* reverse iterators.
So, I'll post this (in case the OP runs into the problem):

{
std::vector<int> v;
for(int i = 0; i != 10; ++ i) v.push_back(i);
for( std::vector<int>::const_reverse_iterator i = v.rbegin();
i != static_cast<std::vector<int>const&>(v).rend(); ++i){
std::cout<<(*i)<<" ";
} // for(rev-itor)
std::cout<<std::endl;
}
 
A

Andrew Koenig

Yes, reverse_iterator helps. However, I need to insert after I find
the right position. insert only takes iterator instead of reverse
iterator.

General form for using a bidirectional iterator to iterate backward through
a container:

iter = c.end();
while (iter != c.begin()) {
--iter;
// Work with the element at *iter here
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top