I have this 2 codes,
Code 1
#include<bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<int,int>m;
int nums[]={1,1,3,4,5};
for(auto x:nums)
{
m[x]++;
}
for(auto x:m)
{
cout<<x.first<<" "<<m[x.first]<<endl;
}
return 0;
}
Code 2
#include<bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<int,int>m;
int nums[]={1,1,3,4,5}; int k=2;
for(auto x:nums)
{
m[x]++;
}
for(auto x:m)
{
cout<<x.first<<" "<<m[x.first+k]<<endl;
}
return 0;
}
The output of the first code is
5 1
4 1
1 2
3 1
But the output of the second code is
5 0
4 0
5 0
7 0
I am not getting why the output of the 2nd code is like this, shouldn't it be like
5 0 //5 is the index and m[5+2]=0 hence 0
4 0
1 1 //since m[1+2]=1
3 1
......................................................................................................................................................................................................................