gpt4 book ai didi

c++ - 为什么打印un_ordered map和map(dictionary)的key和value有异常?

转载 作者:行者123 更新时间:2023-11-28 04:23:13 24 4
gpt4 key购买 nike

这是我的代码,请告诉我为什么它不从头开始打印,因为在 map 中它是以正确的方式打印

#include<bits/stdc++.h>
using namespace std;

int main(){
unordered_map<int,int>arr;
for(int i=1;i<=10;i++){
arr[i]=i*i;
}

for(auto it=arr.begin();it!=arr.end();it++){
cout<<it->first<<" "<<it->second<<"\n";
}

cout<<"normal map \n";

map<int,int>arry;
for(int i=1;i<=10;i++){
arry[i]=i*i;
}

for(auto it=arry.begin();it!=arry.end();it++){
cout<<it->first<<" "<<it->second<<"\n";
}

我的输出是

10 100

9 81

8 64

7 49

6 36

5 25

1 1

2 4

3 9

4 16

法线贴图

1 1

2 4

3 9

4 16

5 25

6 36

7 49

8 64

9 81

10 100

为什么 un_ordered map 以这种方式打印值 为什么不像 map 那样打印

最佳答案

std::unordered_map不按任何特定顺序订购 key 。这就是它被称为无序的原因。

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. This allows fast access to individual elements, since once the hash is computed, it refers to the exact bucket the element is placed into.

关于c++ - 为什么打印un_ordered map和map(dictionary)的key和value有异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54987050/

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