我认为这个问题很清楚。将 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`有什么意义?
发布评论