C++ STL 中的 const_iterator 和非常量迭代器有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/309581/
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-27 14:32:30  来源:igfitidea点击:

What is the difference between const_iterator and non-const iterator in the C++ STL?

c++stliteratorconst

提问by Konrad

What is the difference between a const_iteratorand an iteratorand where would you use one over the other?

aconst_iterator和 an之间有什么区别,iterator你会在哪里使用一个而不是另一个?

采纳答案by Dominic Rodger

const_iterators don't allow you to change the values that they point to, regular iterators do.

const_iterators 不允许您更改它们指向的值,常规iterators 可以。

As with all things in C++, always prefer const, unless there's a good reason to use regular iterators (i.e. you want to use the fact that they're not constto change the pointed-to value).

与 C++ 中的所有内容一样,总是更喜欢const,除非有充分的理由使用常规迭代器(即您想使用它们不const更改指向值的事实)。

回答by jalf

They should pretty much be self-explanatory. If iterator points to an element of type T, then const_iterator points to an element of type 'const T'.

它们应该是不言自明的。如果 iterator 指向类型为 T 的元素,则 const_iterator 指向类型为“const T”的元素。

It's basically equivalent to the pointer types:

它基本上等同于指针类型:

T* // A non-const iterator to a non-const element. Corresponds to std::vector<T>::iterator
T* const // A const iterator to a non-const element. Corresponds to const std::vector<T>::iterator
const T* // A non-const iterator to a const element. Corresponds to std::vector<T>::const_iterator

A const iterator always points to the same element, so the iterator itselfis const. But the element it points to does not have to be const, so the element it points to can be changed. A const_iterator is an iterator that points to a const element, so while the iterator itself can be updated (incremented or decremented, for example), the element it points to can not be changed.

const 迭代器总是指向同一个元素,所以迭代器本身是 const 的。但是它指向的元素不一定是const,所以它指向的元素是可以改变的。const_iterator 是指向 const 元素的迭代器,因此虽然迭代器本身可以更新(例如递增或递减),但它指向的元素不能更改。

回答by Magnus Andermo

Unfortunaty, a lot of the methods for the STL containers takes iteratorsinstead of const_iteratorsas parameters. So if you have a const_iterator, you can't say "insert an element before the element that this iterator points to" (saying such a thing is not conceptually a const violation, in my opinion). If you want do that anyway, you have to convert it to a non-const iterator using std::advance()or boost::next(). Eg. boost::next(container.begin(), std::distance(container.begin(), the_const_iterator_we_want_to_unconst)). If containeris a std::list, then the running time for that call will be O(n).

不幸的是,STL 容器的很多方法都将迭代器而不是const_iterators作为参数。所以如果你有一个const_iterator,你不能说“在这个迭代器指向的元素之前插入一个元素”(在我看来,说这样的事情在概念上并不是const违规)。如果您无论如何都想这样做,则必须使用std::advance()boost::next()将其转换为非常量迭代器。例如。boost::next(container.begin(), std::distance(container.begin(), the_const_iterator_we_want_to_unconst))。如果容器std::list,则该调用的运行时间将为O(n)

So the universal rule to add const wherever it is "logical" to do so, is less universal when it comes to STL containers.

因此,在“合乎逻辑”的地方添加 const 的通用规则在 STL 容器方面不太通用。

However, boost containers take const_iterators (eg. boost::unordered_map::erase()). So when you use boost containers you can be "const agressive". By the way, do anyone know if or when the STL containers will be fixed?

但是,boost 容器采用 const_iterators(例如 boost::unordered_map::erase())。因此,当您使用 boost 容器时,您可能会变得“具有攻击性”。顺便说一句,有谁知道 STL 容器是否或何时修复?

回答by Naveen

Use const_iteratorwhenever you can, use iteratorwhen you have no other choice.

尽可能使用const_iterator,在别无选择时使用iterator

回答by Mr Coder

ok Let me explain it with very simple example first without using constant iterator consider we have collection of random integers collection "randomData"

好的让我先用非常简单的例子来解释它而不使用常量迭代器考虑我们有随机整数集合“randomData”的集合

    for(vector<int>::iterator i = randomData.begin() ; i != randomData.end() ; ++i)*i = 0;
for(vector<int>::const_iterator i = randomData.begin() ; i!= randomData.end() ; ++i)cout << *i;

As can be seen for writing/editing data inside collection normal iterator is used but for reading purpose constant iterator has been used . If you try using constant iterator in first for loop you will get error . As a thumb rule use constant iterator to read data inside collection .

可以看出,在集合内部写入/编辑数据时,使用了普通迭代器,但出于读取目的,使用了常量迭代器。如果您尝试在第一个 for 循环中使用常量迭代器,则会出现错误。作为一个经验法则,使用常量迭代器来读取 collection 中的数据。

回答by Trey Hymanson

(as others have said) const_iterator doesn't allow you modify the elements to which it points, this is useful inside of const class methods. It also allows you to express your intent.

(正如其他人所说) const_iterator 不允许您修改它指向的元素,这在 const 类方法中很有用。它还允许您表达您的意图。