C++ 迭代器->秒是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15451287/
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
What does iterator->second mean?
提问by Noich
In C++, what is the type of a std::map<>::iterator
?
在 C++ 中, a 的类型是std::map<>::iterator
什么?
We know that an object it
of type std::map<A,B>::iterator
has an overloaded operator ->
which returns a std::pair<A,B>*
, and that the std::pair<>
has a first
and second
member.
我们知道一个it
类型的对象std::map<A,B>::iterator
有一个重载operator ->
,它返回 a std::pair<A,B>*
,并且std::pair<>
有 afirst
和second
成员。
But, what do these two members correspond to, and why do we have to access the value stored in the map as it->second
?
但是,这两个成员对应什么,为什么我们必须访问存储在地图中的值it->second
?
回答by Joseph Mansfield
I'm sure you know that a std::vector<X>
stores a whole bunch of X
objects, right? But if you have a std::map<X, Y>
, what it actually stores is a whole bunch of std::pair<const X, Y>
s. That's exactly what a map is - it pairs together the keys and the associated values.
我确定您知道 astd::vector<X>
存储了一大堆X
对象,对吗?但是如果你有一个std::map<X, Y>
,它实际上存储的是一大堆std::pair<const X, Y>
s。这正是映射的含义——它将键和关联的值配对在一起。
When you iterate over a std::map
, you're iterating over all of these std::pair
s. When you dereference one of these iterators, you get a std::pair
containing the key and its associated value.
当您迭代 a 时std::map
,您就是在迭代所有这些std::pair
s。当您取消引用这些迭代器之一时,您会得到一个std::pair
包含键及其关联值的对象。
std::map<std::string, int> m = /* fill it */;
auto it = m.begin();
Here, if you now do *it
, you will get the the std::pair
for the first element in the map.
在这里,如果您现在执行*it
,您将获得std::pair
地图中第一个元素的 。
Now the type std::pair
gives you access to its elements through two members: first
and second
. So if you have a std::pair<X, Y>
called p
, p.first
is an X
object and p.second
is a Y
object.
现在,该类型std::pair
使您可以通过两个成员访问其元素:first
和second
。因此,如果您有一个std::pair<X, Y>
被调用的p
,p.first
是一个X
对象并且p.second
是一个Y
对象。
So now you know that dereferencing a std::map
iterator gives you a std::pair
, you can then access its elements with first
and second
. For example, (*it).first
will give you the key and (*it).second
will give you the value. These are equivalent to it->first
and it->second
.
所以现在您知道取消引用std::map
迭代器会给您 a std::pair
,然后您可以使用first
and访问其元素second
。例如,(*it).first
会给你钥匙,(*it).second
会给你价值。这些等价于it->first
和it->second
。
回答by Andy Prowl
The type of the elements of an std::map
(which is also the type of an expression obtained by dereferencing an iterator of that map) whose key is K
and value is V
is std::pair<const K, V>
- the key is const
to prevent you from interfering with the internal sorting of map values.
an 的元素类型std::map
(也是通过解引用该映射的迭代器获得的表达式的类型),其键是K
,值是V
is std::pair<const K, V>
- 关键是const
防止您干扰映射值的内部排序。
std::pair<>
has two members named first
and second
(see here), with quite an intuitive meaning. Thus, given an iterator i
to a certain map, the expression:
std::pair<>
有两个名为first
and second
(见这里)的成员,具有相当直观的含义。因此,给定i
某个映射的迭代器,表达式:
i->first
Which is equivalent to:
这相当于:
(*i).first
Refers to the first(const
) element of the pair
object pointed to by the iterator - i.e. it refers to a keyin the map. Instead, the expression:
指的是迭代器指向的对象的第一个( const
) 元素pair
——即它指的是映射中的一个键。相反,表达式:
i->second
Which is equivalent to:
这相当于:
(*i).second
Refers to the secondelement of the pair
- i.e. to the corresponding valuein the map.
指的是-的第二个元素,pair
即映射中的对应值。