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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:22:52  来源:igfitidea点击:

What does iterator->second mean?

c++stliterator

提问by Noich

In C++, what is the type of a std::map<>::iterator?

在 C++ 中, a 的类型是std::map<>::iterator什么?

We know that an object itof type std::map<A,B>::iteratorhas an overloaded operator ->which returns a std::pair<A,B>*, and that the std::pair<>has a firstand secondmember.

我们知道一个it类型的对象std::map<A,B>::iterator有一个重载operator ->,它返回 a std::pair<A,B>*,并且std::pair<>有 afirstsecond成员。

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 Xobjects, 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::pairs. When you dereference one of these iterators, you get a std::paircontaining the key and its associated value.

当您迭代 a 时std::map,您就是在迭代所有这些std::pairs。当您取消引用这些迭代器之一时,您会得到一个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::pairfor the first element in the map.

在这里,如果您现在执行*it,您将获得std::pair地图中第一个元素的 。

Now the type std::pairgives you access to its elements through two members: firstand second. So if you have a std::pair<X, Y>called p, p.firstis an Xobject and p.secondis a Yobject.

现在,该类型std::pair使您可以通过两个成员访问其元素:firstsecond。因此,如果您有一个std::pair<X, Y>被调用的p,p.first是一个X对象并且p.second是一个Y对象。

So now you know that dereferencing a std::mapiterator gives you a std::pair, you can then access its elements with firstand second. For example, (*it).firstwill give you the key and (*it).secondwill give you the value. These are equivalent to it->firstand it->second.

所以现在您知道取消引用std::map迭代器会给您 a std::pair,然后您可以使用firstand访问其元素second。例如,(*it).first会给你钥匙,(*it).second会给你价值。这些等价于it->firstit->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 Kand value is Vis std::pair<const K, V>- the key is constto prevent you from interfering with the internal sorting of map values.

an 的元素类型std::map(也是通过解引用该映射的迭代器获得的表达式的类型),其键是K,值是Vis std::pair<const K, V>- 关键是const防止您干扰映射值的内部排序。

std::pair<>has two members named firstand second(see here), with quite an intuitive meaning. Thus, given an iterator ito a certain map, the expression:

std::pair<>有两个名为firstand second(见这里)的成员,具有相当直观的含义。因此,给定i某个映射的迭代器,表达式:

i->first

Which is equivalent to:

这相当于:

(*i).first

Refers to the first(const) element of the pairobject 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即映射中的对应