为什么打印 un_ordered map 和 map(dictionary) 的 key 和 value 有异常?
why there is abnormalities in printing the key and value of un_ordered map and map(dictionary)?
这是我的代码,请告诉我为什么它不能从头开始打印,因为在地图中它是以正确的方式打印的
#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 地图以这种方式打印值 为什么不像地图那样打印
std::unordered_map
不按任何特定顺序排列密钥。这就是为什么它被称为 unordered.
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.
这是我的代码,请告诉我为什么它不能从头开始打印,因为在地图中它是以正确的方式打印的
#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 地图以这种方式打印值 为什么不像地图那样打印
std::unordered_map
不按任何特定顺序排列密钥。这就是为什么它被称为 unordered.
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.