updating STL element in list.

Member
Posts: 321
Joined: 2004.10
Post: #1
Is it legal to alter or update elements of an STL container on the fly?
I'm trying to update one field in an element of a list. Everything compiles
and runs, but when I'm in the debugger the new value never seems to
be saved between successive occurrances of the following.

I've looked at all the methods of STL sequence containers and studied
many Tutorials, but nothing seems to address this fairly particular question.


for (list<Unit *>::iterator i = wCompIter->transitoryObjects.begin();
i != wCompIter->transitoryObjects.end(); i++)
{
Unit *plane = *i;

plane->canIndex = 27; // new value
}
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #2
In your example, you have a list of pointers, so if you dereference one of the pointers and modify the data it points to, the value will stick regardless of what the STL container is doing. Is that the actual code you are using?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
that should work just fine.
Quote this message in a reply
Member
Posts: 321
Joined: 2004.10
Post: #4
Just when I think I've got a good grasp of C++ something crops up to make
me question my competence. The line,

plane->canIndex = 27; // new value

is really a call to a function which returns an index.

The function ended with a

return(index++);

That didn't work. When I changed it to

return(++index);

the update of list works fine. I take it that the former returned the value
of index and then incremented it.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
that's what the postfix ++ operator does, yes Rasp
Quote this message in a reply
Post Reply