unordered_map C++
我正在使用无序映射,例如
unorderedmap.insert(make_pair(5, 6));
如何获取键位置(值 5)的迭代器?我是否需要再次使用 find() 或者我可以如何从返回值中获取它?
谢谢。
I am using unordered map like
unorderedmap.insert(make_pair(5, 6));
How could I get the iterator for the position of the key (value 5)? Do I need to use find() again or could I some how get it from the return value?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您正在使用 此
unordered_map
,迭代器是:Assuming you are using this
unordered_map
, the iterator is:首先,
unordered_map
不是标准容器。但是,如果您使用编译器提供的unordered_map
,它提供类似于 this 然后它返回一对
。可以使用.first
访问该对的iterator
部分,它将为您提供元素插入的位置(如果插入成功,可以使用布尔第二
)。First of all
unordered_map
is not a standard container. But if you are using aunordered_map
provided by the compiler which provides an API similar to this then it returns a pair of<iterator,bool>
. Theiterator
part of the pair which can be accesses using.first
will give you the location where the element got inserted (if it was inserted succesfully, this can be tested using the boolsecond
).这取自 http://www.cplusplus.com/reference/stl/map/ insert/
因此,如前所述,使用pair::first 来获取迭代器。
This is taken from http://www.cplusplus.com/reference/stl/map/insert/
So as stated before, use the pair::first to get the iterator.
unordered_map
接口与set
接口或map
接口类似,因为insert
方法返回一个std::pair
:bool
为 true,如果容器iterator
指向正确的元素:根据情况,可以是已经存在的元素,也可以是新插入的元素因此,如果您只想访问该元素,您可以简单地使用:
但是请检查您是否这样做不需要知道插入是否成功,一般来说关心它:)
The
unordered_map
interface is similar to theset
interface or themap
interface in that theinsert
method returns astd::pair<iterator,bool>
:bool
is true if the insertion took place, it did not take place if an element with a similar key already existed in the containeriterator
points to the right element: either the already existing one or the newly inserted one depending on the caseTherefore, if you only wish to access the element, you can simply use:
However do check that you do not need to know if the insert was successful or not, in general one care about it :)