The new ranged-for in C++11 will be very concise and useful, of course. As far as I understand how it works, it looks up the "containers" begin
and end
by trying *Argument-Depending-Lookup" (ADT).
But another addition is that all the containers now have cbegin()
and cend()
to get the const_iterators
for the container.
I am a bit confused, on the one hand I guess I should use cbegin()
if I do not want to modify the container, on the other hand I have to add an additional const
inside the ranged-for to get the same thing.
So, it looks like this:
// print all
for(const auto elem : data)
cout << elem
using ADT, finding data.begin(), thus const
needed.
vs
// print everything but the first (a reason not to use range-for)
for(auto it = data.cbegin()+1; it!=data.cend(); ++it)
cout << *it
using data.cbegin()
, thus no const
needed.
But would this not be more "idiomatic"?:
// print everything but the first (a reason not to use range-for)
for(const auto it = data.begin()+1; it!=data.end(); ++it)
cout << *it
cbegin
?begin()
only?Edit: correction of error Value vs Iterator
cbegin()
allows you to get const_iterator
s from a non-const
container without an explicit cast or conversion. If you have a const
container then begin()
will return a const_iterator
anyway.
The new for
construct uses begin()
because that's the most general, and it avoids too many special cases. Also, by default, the variable is a value, not an iterator or a reference.
std::vector<int> v;
for(auto i: v) // i is an int
dostuff(i);
This avoids the problem of modifying the container, as the element is copied. To get a reference you need to declare it:
for(auto &i: v)
dostuff(i);
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