When should I use the new ranged-for and can I combine it with the new cbegin/cend?

ぃ、小莉子 提交于 2019-12-30 08:02:08

问题


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
  • Did I get the "idiom" right? Any additions?
  • When should I use cbegin?
  • Do I miss something with ranged-for, looking for begin() only?

Edit: correction of error Value vs Iterator


回答1:


cbegin() allows you to get const_iterators 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);



回答2:


I would use cbegin/cend in in the for loop if the intention is not to modify the elements in the range. That's the obvious reason for adding them in the first place.

This is hardly idiomatic yet, as the new standard isn't even off the presses!



来源:https://stackoverflow.com/questions/5814553/when-should-i-use-the-new-ranged-for-and-can-i-combine-it-with-the-new-cbegin-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!