Re: How to write previous element in STL list
cornelis van der bent wrote:
In my code I want to go through all combinations of two items in a
list. Here is my code:
list<Instance*>::iterator i;
for (i = instances.begin(); i != --instances.end(); i++)
{
list<Instance*>::iterator j;
for (j = i + 1; j < instances.end(); j++)
{
// Do something!
}
}
I get a big error message at i + 1.
Such an operation is only defined for random-access iterators, and the
list iterator isn't one.
> I got a similar message when I
wrote
i != instances.end() -1, but fixed this by writing --instances.end().
My question what must I write instead of i + 1?
Duplicate it and increment. Something like
// presume the list does not change
list<Instance*>::iterator i = instances.begin(),
last = --instances.end(),
e = instances.end();
for (; i != last; ++i)
{
list<Instance*>::iterator j = i;
while (++j != e)
{
// Do something
}
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask