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!

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; 
        loop_statement 
    } 
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.