[英]understanding the type of dereference - const_iterator
I have this declaration: 我有这个声明:
list<string*>::const_iterator iter;
I am trying to understand whether the type of *iter
is: string*
, const string*
or something else. 我试图了解
*iter
的类型是: string*
, const string*
还是其他类型。
I read that cost_iterator
returns reference. 我读到
cost_iterator
返回参考。 On the one hand the list contains string*
and the iterator points to those. 一方面,列表包含
string*
,并且迭代器指向这些string*
。 on the other hand it is const_iterator
so the value it points to should be const
and it acts as if the data in the list is const
? 另一方面,它是
const_iterator
因此它指向的值应该是const
并且它的作用就像列表中的数据是const
? despite it is not really? 尽管不是真的吗? not sure what am I missing and what is the right answer.
不确定我缺少什么,正确的答案是什么。
std::list<T>::const_iterator::operator*
returns a const T&
. std::list<T>::const_iterator::operator*
返回const T&
。 Since your list stores a string*
that means you'll get a reference to a string * const
as the const
applies to T
, not to what T
points to if it is a pointer. 由于您的列表存储了一个
string*
,这意味着您将获得对string * const
的引用,因为const
适用于T
,而不是T
指向的指针(如果它是指针)。
The general answer is const T &
, which I prefer to write T const &
because it composes correctly in the next paragraph. 通用答案是
const T &
,我更喜欢写T const &
因为它在下一段中可以正确组成。
For T=string*
, we get string * const &
, or a reference to a constant pointer to a string . 对于
T=string*
,我们得到string * const &
,或对指向string的常量指针的引用 。
This means you can't mutate the list element (via a const_iterator) by pointing it to a different string, but you can indirectly mutate the list by altering the string to which it points. 这意味着您不能通过将列表元素指向另一个字符串来对其进行变异(通过const_iterator),但是可以通过更改其指向的字符串来间接地对列表进行变异。
You can read the definitions of std::list<T>
, but it isn't as clear as it could be about the iterator behaviour - it works out like so: 您可以阅读
std::list<T>
的定义,但是关于迭代器的行为尚不清楚,它的工作原理如下:
std::list<T>::value_type
is the same as T
std::list<T>::value_type
与T
相同
std::list<string*>::value_type
is string*
. std::list<string*>::value_type
是string*
。 std::list<T>::const_iterator
models a const
LegacyBidirectionalIterator std::list<T>::const_iterator
为const
LegacyBidirectionalIterator建模
LegacyBidirectionalIterator is a LegacyForwardIterator which must satisfy LegacyInputIterator LegacyBidirectionalIterator是一个LegacyForwardIterator ,必须满足LegacyInputIterator
LegacyInputIterator must support the expression *i
returning type reference
LegacyInputIterator必须支持表达式
*i
返回类型reference
and finally, from the LegacyForwardIterator we rather skipped over 最后,我们从LegacyForwardIterator中跳过了
The type std::iterator_traits::reference must be exactly
类型std :: iterator_traits :: reference必须完全相同
... const T& otherwise (It is constant),
... const T&否则(它是常数),
(where T is the type denoted by std::iterator_traits::value_type)
(其中T是由std :: iterator_traits :: value_type表示的类型)
which is how we get from the type named reference
to the const T &
we started with. 这就是我们如何从名为
reference
的类型获取到const T &
开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.