I simply want to iterate trought a list and if I discover some value at some posittion to change it ,How ?
I have searched over the net, but I have not view the solution.
Any help ? Thanks
typedef list< std::string > Cont;
for( Cont::iterator i = LIST.begin(); i != LIST.end(); ++i ) {
// dereference the iterator to get a reference to the element
std::string & s( *i );
// test it
if( s == "something" )
s = "something else"; // replace
if( s == "another something" )
s = "another something else"; // replace
}
You can assign directly to *i, if that's what you mean. I still suggest encapsulating the test and replace logic in a function object and using a std algorithm.
The variable is a reference and can be created inside the loop--it is not copying anything. References cannot be left uninitialized nor can they be changed. You would have to use a pointer or take the variable by value if you insist on moving the declaration out of the loop.
I know that you probably have to do better things than waste the time with me...
But could you write some code for the example ?
I' working with it, but I can't get nothing....
Thanks