How to do this in std::list?

Started by
7 comments, last by Ezbez 16 years, 7 months ago
Previously I was using std::vector, and I was using it like this:

for(int i=0;i<(int)actors.size()-1;i++) {
	for(int j=i+1;j<(int)actors.size();j++) {
	}
}

But now I'm using std::list instead, and I'm not sure how to convert that code above to work with it as a list. So far I've got:

typedef std::list<Actor>::iterator ActorsIterator;

for(ActorsIterator i=actors.begin();i!=actors.end();i++) { //need to iterate to second last actor
	for(ActorsIterator j=actors.begin();j!=actors.end();j++) { // need to start at i's current iteration+1 
	}
}

thanks
Advertisement
typedef std::list<Actor>::iterator ActorsIterator;for (ActorsIterator i = actors.begin(); i !=actors.end(); i++) { //need to iterate to second last actor  ActorsIterator j = i;  ++j;    for(; j != actors.end(); j++) { // need to start at i's current iteration+1   }}
I think that you missed the "// Need to iterate to second to last actor" part, SiCrane. Correct me if I'm wrong, but that could be done by doing "i != --actors.end()"

edit: actors.end() not actors.last. Durh.
thanks,

for the i iterator, to make it just goto the second last object, I did:

ActorsIterator k = actors.end();k--;k--;for(ActorsIterator i=actors.begin();i!=k;i++) {


But is there a better way to do it?

edit:
Quote:Original post by Ezbez
I think that you missed the "// Need to iterate to second to last actor" part, SiCrane. Correct me if I'm wrong, but that could be done by doing "i != --actors.end()"



Is it just --actors.end()? Because I thought actors.end() doesnt actually represent the last item, but after it, so I thought it may be ----actors.end()

thx
Quote:Original post by Ezbez
I think that you missed the "// Need to iterate to second to last actor" part, SiCrane. Correct me if I'm wrong, but that could be done by doing "i != --actors.last"


Yes, I missed it, but no, that wouldn't be the way to do it. If your for loop comparison has side-effects, such as using --, then that side effect will happen every time you go through the loop.

Though you could do:
ActorsIterator k = actors.end();std::advance(k, -2);

However, if you do this, I'd do a check to make sure that actors.size() is big enough first.
Actually, by decrementing k twice, you get the third last object. And it doesn't really get any shorter than a simple --k before the loop.

Ezbez: --actors.end() might well be illegal.
Quote:Original post by ToohrVyk
Actually, by decrementing k twice, you get the third last object. And it doesn't really get any shorter than a simple --k before the loop.

Ezbez: --actors.end() might well be illegal.




Quote:
e.insert( --e.end(), TodaysDate() );

Error: The expression "--e.end()" is illegal.

The reason is simple, if a little obscure: vector<Date>::iterator is simply a Date*, and you're not allowed to modify temporaries of builtin type. For example, the following plain-jane code is also illegal:
      Date* f();    // function that returns a Date*      p = --f();    // error, but could be "f() - 1"



I'm not sure I understand what the problem is, like what does that function have to do with .end() ?

Quote:Original post by johnnyBravo
I'm not sure I understand what the problem is, like what does that function have to do with .end() ?


It is not guaranteed that the value returned by actors.end() is modifiable. Therefore, it might happen that, if you change to another compiler, or use another container, an expression such as --actors.end() will cause a compiler error. Because of this, it is preferable to avoid it, and resort to other options which, though a little bit longer, are always safe to compile.

As a side note, your code here could very well be converted to:

for (ActorsIterator j = actors.begin(); j != actors.end(); ++j)  for (ActorsIterator i = actors.begin(); i != j; ++i)    // Do stuff with i, j


Aside from the order in which the (i,j) pairs are visited, this code should have the same result as yours, and not require any prior increment or decrement.
So, since there might be nothing in the std::list, --list::end() could reference something that doesn't exist and the world asplodes?

Edit: Oh I see, it's worse than that.

This topic is closed to new replies.

Advertisement