In c++11, if I use a range based for loop on vector, will it guarantee the iteration order?
i.e. are the following code blocks guaranteed to produce the same output?
vector<T> output;
vector<U> V;
for( auto v: V) output.push_back(f(v));
vs
for(int i =0; i < V.size(); ++i) output.push_back(f(V[i]));
what if it is not vector
but map
, etc?
So first the condition is checked, then the loop body is executed, then the increment.
Range-based for loop in C++ It executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.
Range-for is as fast as possible since it caches the end iterator[citationprovided], uses pre-increment and only dereferences the iterator once. Then, yes, range-for may be slightly faster, since it's also easier to write there's no reason not to use it (when appropriate).
Yes, it is, there is no restriction about it. In C++ is also very common creating for loops with iterators.
Yes the two codes are guaranteed to do the same. Though I don't have a link to the standard you can have a look here. I quote: You can read that as "for all x in v" going through starting with v.begin() and iterating to v.end().
Yes, they are equivalent. The standard guarantees in 6.5.4:
For a range-based for statement of the form
for ( for-range-declaration : expression ) statement
let
range-init
be equivalent to the expression surrounded by parentheses ( expression )and for a range-based for statement of the form
for ( for-range-declaration : braced-init-list ) statement
let
range-init
be equivalent to the braced-init-list. In each case, a range-based for statement is equivalent to
{
auto && __range = range-init;
for ( auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin ) {
for-range-declaration = *__begin;
statement
}
}
where
__range
,__begin
, and__end
are variables defined for exposition only, and_RangeT
is the type of the expression, andbegin-expr
andend-expr
are determined as follows:— if
_RangeT
is an array type,begin-expr
andend-expr
are__range
and__range + __bound
, respectively, where__bound
is the array bound. If_RangeT
is an array of unknown size or an array of incomplete type, the program is ill-formed;— if
_RangeT
is a class type, the unqualified-idsbegin
andend
are looked up in the scope of class_RangeT
as if by class member access lookup (3.4.5), and if either (or both) finds at least one declaration,begin-expr
andend-expr
are__range.begin()
and__range.end()
, respectively;— otherwise,
begin-expr
andend-expr
arebegin(__range)
andend(__range)
, respectively, wherebegin
andend
are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup, namespacestd
is an associated namespace.
Though your question about map is a bit nonsensical. If it's an ordered map and you iterate through the map properly, then they're equivalent. If it's an unordered map then your question doesn't really make much sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With