unordered_map C++

发布于 2024-09-30 18:12:25 字数 154 浏览 0 评论 0原文

我正在使用无序映射,例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┊风居住的梦幻卍 2024-10-07 18:12:25

假设您正在使用 unordered_map ,迭代器是:

unorderedmap.insert(make_pair(5, 6)).first;

Assuming you are using this unordered_map, the iterator is:

unorderedmap.insert(make_pair(5, 6)).first;
凉世弥音 2024-10-07 18:12:25

首先,unordered_map 不是标准容器。但是,如果您使用编译器提供的 unordered_map,它提供类似于 this 然后它返回一对 。可以使用 .first 访问该对的 iterator 部分,它将为您提供元素插入的位置(如果插入成功,可以使用布尔第二)。

First of all unordered_map is not a standard container. But if you are using a unordered_map provided by the compiler which provides an API similar to this then it returns a pair of <iterator,bool>. The iterator 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 bool second).

別甾虛僞 2024-10-07 18:12:25

这取自 http://www.cplusplus.com/reference/stl/map/ insert/

因此,如前所述,使用pair::first 来获取迭代器。

第一个版本返回一对,其中
其成员对::first设置为
迭代器指向新的
插入的元素或元素
已经具有相同的价值
地图。该对::中的第二个元素
如果有新元素,则pair设置为true
如果元素已插入或为 false
存在相同的值。

This is taken from http://www.cplusplus.com/reference/stl/map/insert/

So as stated before, use the pair::first to get the iterator.

The first version returns a pair, with
its member pair::first set to an
iterator pointing to either the newly
inserted element or to the element
that already had its same value in the
map. The pair::second element in the
pair is set to true if a new element
was inserted or false if an element
with the same value existed.

故人如初 2024-10-07 18:12:25

unordered_map 接口与 set 接口或 map 接口类似,因为 insert 方法返回一个 std::pair

  • 中已存在具有相似键的元素,则不会发生插入
  • 如果插入发生,则 bool 为 true,如果容器 iterator 指向正确的元素:根据情况,可以是已经存在的元素,也可以是新插入的元素

因此,如果您只想访问该元素,您可以简单地使用:

map.insert(std::make_pair(key,value)).first

但是请检查您是否这样做不需要知道插入是否成功,一般来说关心它:)

The unordered_map interface is similar to the set interface or the map interface in that the insert method returns a std::pair<iterator,bool>:

  • the bool is true if the insertion took place, it did not take place if an element with a similar key already existed in the container
  • the iterator points to the right element: either the already existing one or the newly inserted one depending on the case

Therefore, if you only wish to access the element, you can simply use:

map.insert(std::make_pair(key,value)).first

However do check that you do not need to know if the insert was successful or not, in general one care about it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文