gpt4 book ai didi

c++ - equal_range 和 std::unordered_multiset

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:21:51 26 4
gpt4 key购买 nike

我的理解是,我们不能对无序...容器中元素的顺序做出任何假设(即使它们是通过哈希表实现的)。如果那是正确的,那么 std::unordered_multiset::equal_range() 如何返回一系列相等的值?

例如:

typedef std::unordered_multiset<int> int_set;
typedef int_set::iterator int_set_iterator;

int_set set;
set.insert(1);
set.insert(2);
set.insert(1);
set.insert(3);
set.insert(1);

// equal_range seems to assume all 1s are located next to each other
std::pair<int_set_iterator, int_set_iterator> range = set.equal_range(1);

size_t range_size = std::distance(range.first, range.second); // the value is 3

最佳答案

标准规定了 [unord.req]/6 中相等元素如何位于范围内:

In containers that support equivalent keys, elements with equivalent keys are adjacent to each other in the iteration order of the container. Thus, although the absolute order of elements in an unordered container is not specified, its elements are grouped into equivalent-key groups such that all elements of each group have equivalent keys. Mutating operations on unordered containers shall preserve the relative order of elements within each equivalent-key group unless otherwise specified.

关于c++ - equal_range 和 std::unordered_multiset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com