Should I prefer iterators over const_iterators?(我应该更喜欢迭代器而不是 const_iterators 吗?)
问题描述
最近有人提出 Scott Meyers 的文章说:
Someone here recently brought up the article from Scott Meyers that says:
- 更喜欢
iterators而不是const_iterators(pdf 链接).
- Prefer
iteratorsoverconst_iterators(pdf link).
其他人评论说这篇文章可能已经过时了.我想知道你的意见是什么?
Someone else was commenting that the article is probably outdated. I'm wondering what your opinions are?
这是我的:这篇文章的主要观点之一是您不能在 const_iterator 上擦除或插入,但我认为将其用作反对 const_iterators<的论据很有趣/代码>.我认为 const_iterators 的全部意义在于您根本不修改范围,既不是通过替换它们的值来修改元素本身,也不是通过插入或擦除来修改范围.还是我错过了什么?
Here is mine: One of the main points of the article is that you cannot erase or insert on a const_iterator, but I think it's funny to use that as an argument against const_iterators. I thought the whole point of const_iterators it that you do not modify the range at all, neither the elements themselves by substituting their values nor the range by inserting or erasing. Or am I missing something?
推荐答案
我完全同意你的看法.我认为答案很简单:使用 const_iterators,其中 const 值是正确的,反之亦然.在我看来,那些反对 const_iterators 的人通常必须反对 const...
I totally agree with you. I think the answer is simple: Use const_iterators where const values are the right thing to use, and vice versa. Seems to me that those who are against const_iterators must be against const in general...
这篇关于我应该更喜欢迭代器而不是 const_iterators 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该更喜欢迭代器而不是 const_iterators 吗?
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
