``const auto`有什么意义?

编程入门 行业动态 更新时间:2024-10-17 01:17:27
本文介绍了``const auto`有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我认为这个问题很清楚。将 auto 关键字自动检测常量,或者总是返回一个非const类型,即使有eg。函数的两个版本(一个返回 const ,另一个不返回)。

I think the question is clear enough. Will the auto keyword auto-detect const-ness, or always return a non-const type, even if there are eg. two versions of a function (one that returns const and the other that doesn't).

记录,我在我的for循环之前使用 const auto end = some_container.end(),但我不知道这是否必要,甚至不同于正常 auto 。

Just for the record, I do use const auto end = some_container.end() before my for-loops, but I don't know if this is necessary or even different from normal auto.

推荐答案

也许你很困惑 const_iterator 和 const iterator 。第一个循环遍历const元素,第二个不能迭代,因为你不能使用运算符 ++和 - 。

Maybe you are confusing const_iterator and const iterator. The first one iterates over const elements, the second one cannot iterate at all because you cannot use operators ++ and -- on it.

注意,你很少从 container.end()中迭代。通常你会使用:

Note that you very seldom iterate from the container.end(). Usually you will use:

const auto end = container.end(); for (auto i = container.begin(); i != end; ++i) { ... }

更多推荐

``const auto`有什么意义?

本文发布于:2023-11-02 16:56:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么意义   const   auto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!