const_iterator和iterator之间有什么区别? [英] what is the difference between const_iterator and iterator?
问题描述
这两个有关STL中的实现有什么区别。
关于性能有什么区别?
我想当它是只读,我们喜欢const_iterator。
谢谢。
。
const_iterator
返回对常量值的引用( const T& code>)并阻止修改引用的值:它强制执行
const
-correctness 。
当你有一个const引用容器时,你只能得到一个 const_iterator
。
已编辑:我提到 const_iterator
返回常量指针
std :: string
的实现使用COW。) What is difference between these two regarding implementation inside STL. what is the difference regarding performance? I guess when it is read only, we prefer const_iterator. right?
Thank you.
There is no performance difference.
The const_iterator
returns a reference to a constant value (const T&
) and prevents modification of the referenced value: it enforces const
-correctness.
When you have a const reference to the container, you can only get a const_iterator
.
Edited: I mentionned "The const_iterator
returns constant pointers" which is not accurate, thanks to Brandon for pointing it out.
Edit: For COW objects, getting a non-const iterator (or dereferencing it) will probably trigger the copy. (Some obsolete and now disallowed implementations of std::string
use COW.)
这篇关于const_iterator和iterator之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!