what is the difference between const_iterator and iterator?(const_iterator 和迭代器有什么区别?)
问题描述
这两者在 STL 内部实现方面有什么区别.性能有什么区别?我想当我们以只读方式"遍历向量时,我们更喜欢 const_iterator,对吧?
What is difference between these two regarding implementation inside STL.
what is the difference regarding performance?
I guess when we are traversing the vector in "read only wise", we prefer const_iterator, right?
谢谢.
推荐答案
没有性能差异.
const_iterator 是一个指向 const 值的迭代器(类似于 const T* 指针);取消引用它会返回对常量值 (const T&) 的引用并防止修改引用的值:它强制执行 const-正确性.
A const_iterator is an iterator that points to const value (like a const T* pointer); dereferencing it returns a reference to a constant value (const T&) and prevents modification of the referenced value: it enforces const-correctness.
当你有一个对容器的const引用时,你只能得到一个const_iterator.
When you have a const reference to the container, you can only get a const_iterator.
已我提到const_iterator 返回常量指针"并不准确,感谢 Brandon 指出.
Edited: I mentionned "The const_iterator returns constant pointers" which is not accurate, thanks to Brandon for pointing it out.
 对于 COW 对象,获取非常量迭代器(或取消引用它)可能会触发复制.(一些过时且现在不允许的 std::string 实现使用 COW.)
 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 和迭代器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:const_iterator 和迭代器有什么区别?
				
        
 
            
        - 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
 - 将 hdc 内容复制到位图 2022-09-04
 - GDB 不显示函数名 2022-01-01
 - 如何提取 __VA_ARGS__? 2022-01-01
 - 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
 - 哪个更快:if (bool) 或 if(int)? 2022-01-01
 - DoEvents 等效于 C++? 2021-01-01
 - 将函数的返回值分配给引用 C++? 2022-01-01
 - XML Schema 到 C++ 类 2022-01-01
 - OpenGL 对象的 RAII 包装器 2021-01-01
 
						
						
						
						
						