unordered_multimap元素输出顺序很奇怪

发布于 2025-02-13 16:50:02 字数 675 浏览 0 评论 0原文

我们知道unordered_multimap没有排序元素。(可能...我是菜鸟:) 因此,unordered_multimap中元素的顺序是按输入顺序进行的。 现在,我输入了5个最简单的字母,即A,B,C,D,e。 首先,请猜测元素输出哪个顺序? 输入顺序ABCDE是相同的吗? 输入顺序EDCBA的相反吗? 这是荒谬的edc a b !!! 我测试了许多输入。 Alaways反向顺序和最后两个元素是顺序。 我听不懂。

    int main()
{
    unordered_multimap<char, int> window;
    window.insert(make_pair('A',1));
    window.insert(make_pair('B',1));
    window.insert(make_pair('C',1));
    window.insert(make_pair('D',1));
    window.insert(make_pair('E',1));
    for (unordered_multimap<char, int>::iterator it = window.begin(); it != window.end(); it++)
        cout<<it->first<<endl;
    return 0;
}

We know that unordered_multimap does not sort elements.(Probably...I'm Noob:)
So the order of elements in unordered_multimap is by order of input.
Now I inputed 5 simplest letter that A,B,C,D,E.
First please guess elements output in which order?
Is same at input order A B C D E?
Is the reverse of input order E D C B A?
It is ridiculous E D C A B!!!
I tested many input. Alaways reverse order and the last two elements is order.
I can't understand it.

    int main()
{
    unordered_multimap<char, int> window;
    window.insert(make_pair('A',1));
    window.insert(make_pair('B',1));
    window.insert(make_pair('C',1));
    window.insert(make_pair('D',1));
    window.insert(make_pair('E',1));
    for (unordered_multimap<char, int>::iterator it = window.begin(); it != window.end(); it++)
        cout<<it->first<<endl;
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

明月松间行 2025-02-20 16:50:02

unordered_multimap中元素的顺序是按输入的顺序

!名称为无序;根本没有有用的,可预测的排序。在引擎盖下,它们以

不要依靠 unordered_map中订购;如果您需要订购,则必须自己强加它,或使用类似于Java的linkedHashmap的第三方哈希表结构,该结构将链接列表结构与哈希表结合在一起以获取插入订购的迭代。

the order of elements in unordered_multimap is by order of input

NOPE! The name is unordered; there is no useful, predictable ordering involved at all. Under the hood, they're implemented as hash tables, and the nature of hash tables means that the iteration ordering depends on the hash values of the keys (which are frequently unrelated to their sort order), the size of the underlying table (which resizes as more keys are inserted) and the order in which keys were inserted or deleted. The hashing rules for most built-in types are implementation-defined, so they'll change from compiler to compiler.

Don't rely on any ordering in unordered_map; if you need ordering, you'll have to impose it yourself, or use third-party hash table structures similar to Java's LinkedHashMap that integrate a linked list structure with a hash table to get insertion-ordered iteration.

酷到爆炸 2025-02-20 16:50:02

请参阅 unordered_multimimap

在内部,这些元素不是按任何特定顺序排序的,而是将元素分类为存储桶。将元素放入哪个水桶完全取决于其密钥的哈希。

顺便说一句,因为 unordered_multimap支持等效键,因此不能保证同一密钥的存储顺序被订购。

此容器的迭代顺序不需要稳定(例如,不能使用STD ::等等来比较两个std :: unordered_multimaps),除了每个键的元素都比较等效(比较等于使用Key_eq()作为比较器)在迭代顺序中形成一个连续的子量,也可以使用quare_range()。

访问。

refer to unordered_multimap

Internally, the elements are not sorted in any particular order, but organized into buckets. Which bucket an element is placed into depends entirely on the hash of its key.

By the way, because unordered_multimap supports equivalent keys, the storage order of the same key is not guaranteed to be ordered.

The iteration order of this container is not required to be stable (so, for example, std::equal cannot be used to compare two std::unordered_multimaps), except that every group of elements whose keys compare equivalent (compare equal with key_eq() as the comparator) forms a contiguous subrange in the iteration order, also accessible with equal_range().

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