C++ const_iterator 和 iterator 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5346890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:58:12  来源:igfitidea点击:

what is the difference between const_iterator and iterator?

c++stliteratorconst-iterator

提问by user658266

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?

关于 STL 内部的实现,这两者之间有什么区别。性能有什么区别?我想当我们以“只读方式”遍历向量时,我们更喜欢const_iterator,对吧?

Thank you.

谢谢你。

采纳答案by ysdx

There is no performance difference.

没有性能差异。

A const_iteratoris 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.

Aconst_iterator是一个指向 const 值的迭代器(就像一个const T*指针);取消引用它返回对常量值 ( const T&) 的引用并防止修改引用的值:它强制执行const-correctness

When you have a const reference to the container, you can only get a const_iterator.

当你有一个对容器的 const 引用时,你只能得到一个const_iterator.

Edited:I mentionned “The const_iteratorreturns constant pointers” which is not accurate, thanks to Brandon for pointing it out.

编辑:我提到了const_iterator不准确的“返回常量指针”,感谢布兰登指出。

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::stringuse COW.)

编辑:对于 COW 对象,获取非常量迭代器(或取消引用它)可能会触发复制。(一些已过时且现在不允许std::string使用 COW 的实现。)

回答by AbhimanyuAryan

Performance wise there is no difference. The only purpose of having const_iteratorover iteratoris to manage the accessesibility of the container on which the respective iterator runs. You can understand it more clearly with an example:

性能方面没有区别。拥有const_iteratorover的唯一目的iterator是管理相应迭代器运行的容器的可访问性。你可以通过一个例子更清楚地理解它:

std::vector<int> integers{ 3, 4, 56, 6, 778 };

If we were to read & write the members of a container we will use iterator:

如果我们要读写容器的成员,我们将使用迭代器:

for( std::vector<int>::iterator it = integers.begin() ; it != integers.end() ; ++it )
       {*it = 4;  std::cout << *it << std::endl; }

If we were to only read the members of the container integersyou might wanna use const_iterator which doesn't allow to write or modify members of container.

如果我们只读取容器的成员,integers您可能想要使用不允许写入或修改容器成员的 const_iterator。

for( std::vector<int>::const_iterator it = integers.begin() ; it != integers.end() ; ++it )
       { cout << *it << endl; }

NOTE: if you try to modify the content using *it in second case you will get an error because its read-only.

注意:如果您尝试在第二种情况下使用 *it 修改内容,您将收到错误,因为它是只读的。

回答by Seenivasan

if you have a list a and then following statements

如果你有一个列表 a 然后下面的语句

list<int>::iterator it; // declare an iterator
    list<int>::const_iterator cit; // declare an const iterator 
    it=a.begin();
    cit=a.begin();

you can change the contents of the element in the list using “it” but not “cit”, that is you can use “cit” for reading the contents not for updating the elements.

您可以使用“it”而不是“cit”来更改列表中元素的内容,即您可以使用“cit”来读取内容而不是更新元素。

*it=*it+1;//returns no error
    *cit=*cit+1;//this will return error