Hello all!

When would it be advisable to use string iterators vs the "for char c in string" loop that is a feature in C++-11 or vs the for (int i.....) loop? And why?

The only two situations I see myself using string iterators is for the STL algorithms and for traversing a string in reverse (crbegin/crend/rbegin/rend) instead of going forward as it is harder to implement going backwards using a for-loop without using iterators.

Thanks for looking here and thanks in advance!

Edited by diafol: Move to c++

2 Years
Discussion Span
Last Post by NathanOliver

Doing most things with standard containers involves using iterators. Even using the new ranged based for loops does. If you take for example:

for ( range_declaration : range_expression ) loop_statement

This becomes:

    auto && __range = range_expression ; 
    for (auto __begin = begin_expr, __end = end_expr; __begin != __end; ++__begin) 
        range_declaration = *__begin; 

Most things deal with iterators as it makes for generating portable function a lot easier. Virtually anything that implements a begin() and end() can be used in functions. The main cavet is that the type of iterator the function requires.

Source for code example: http://en.cppreference.com/w/cpp/language/range-for

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.