Re: Using for each, in with STL containers with non-const iterators



Wyck wrote:
I want to use the "for each, in" syntax with an STL container, and still
modify the elements of the container while iterating.

I was expecting this to work:
[..]
for each( int& num in lst ) {
num++;
}

This 'for each' syntax doesn't exist in C++.

This is valid code:
for each( const int& cref in lst ) { /**/ }

...but 'cref' is not an l-value, so you can't modify the elements of the
list while iterating!

How are you compiling this? This for sure is not valid C++!

Uli


.



Relevant Pages

  • Re: Iterators and versioning
    ... are you saying while iterating through a ... you have a method to modify it?.. ... iterating through the list ... note that the compiler-generated enumerator has no version checking. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Remove items from a list
    ... Always a bad idea to modify the structure of a list ... (deleting or inserting items) ... while iterating through it, but it's so easy ... > Dan Perl wrote: ...
    (comp.lang.python)
  • Re: how to explain such codes, pythons bug or mine?
    ... "It is not safe to modify the sequence being iterated over in the loop (this ... can only happen for mutable sequence types, such as lists). ... to modify the list you are iterating over (for example, ...
    (comp.lang.python)
  • Re: Sorting troubles
    ... def insertSort: ... The insertion sort, however, does modify the list. ... by iterating over the old one. ...
    (comp.lang.python)
  • Re: Help - need to change an attribute value
    ... All I need to change is the syntax - we have it set at "Case Sensitive ... ' Retrieve attribute value. ... strDepartment = LCase ... To modify users in many OU/Containers throughout, ...
    (microsoft.public.windows.server.active_directory)