我什么时候应该使用新的 ranged-for 以及我可以将它与新的 cbegin/cend 结合使用吗?
当然,C++11 中新的 ranged-for 将非常简洁且有用。据我了解它是如何工作的,它通过尝试 *Argument-Depending-Lookup" (但
另一个补充是所有容器现在都有 cbegin()
和 cend()
来获取 const_iterators 容器。
代码>我是 有点困惑,一方面我想如果我不想修改容器,我应该使用 cbegin() ,另一方面我必须添加一个额外的ranged-for 中的 const
得到相同的结果,
因此,它看起来像这样:
// print all
for(const auto elem : data)
cout << elem
使用 ADT,找到 data.begin(),因此 const 。代码>需要
与
// print everything but the first (a reason not to use range-for)
for(auto it = data.cbegin()+1; it!=data.cend(); ++it)
cout << *it
使用 。 data.cbegin()
,因此不需要 const
不是更“惯用”吗?:
// 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()
的错误 值 ?与迭代器对比
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果目的不是修改范围内的元素,我会在 for 循环中使用 cbegin/cend 。这就是首先添加它们的明显原因。
这还不是惯用语,因为新标准甚至还没有出版!
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!
cbegin()
允许您从非const
容器获取const_iterator
,而无需显式强制转换或转换。如果您有一个 const 容器,那么 begin() 无论如何都会返回一个 const_iterator 。新的
for
构造使用begin()
因为这是最通用的,并且它避免了太多的特殊情况。另外,默认情况下,变量是一个值,而不是迭代器或引用。这避免了在复制元素时修改容器的问题。要获取引用,您需要声明它:
cbegin()
allows you to getconst_iterator
s from a non-const
container without an explicit cast or conversion. If you have aconst
container thenbegin()
will return aconst_iterator
anyway.The new
for
construct usesbegin()
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.This avoids the problem of modifying the container, as the element is copied. To get a reference you need to declare it: