Why unordered map is not printing key elements in ascending order?

// CPP program to count frequencies of array items

Blockquote

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

void countFreq(int arr[], int n)
{
unordered_map<int, int> mp;

// Traverse through array elements and 
// count frequencies 
for (int i = 0; i < n; i++) 
	mp[arr[i]]++; 

// Traverse through map and print frequencies 
for (auto x : mp) 
	cout << x.first << " " << x.second << endl; 

}

int main()
{
int arr[] = { 10,10,10,8,3,8,5,3,3,1,1,2,2 };
int n = sizeof(arr) / sizeof(arr[0]);
countFreq(arr, n);
return 0;
}

Because it’s unordered.

4 Likes

unordered map doesn’t keep ordering to work a bit faster than normal map.

1 Like