Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given an array , we need to output the frequency of all elements of the array.
arr[]= {1,5,1,2,5,2,g}
Output : 1 2
2 2
5 3

What I have tried:

I tried it using unordered_map as follows-
C++
void count(int arr[] , int n){
    map<int ,int>d;
    for(int i=0;i<n ;i++){
        d[arr[i]]++;
    }
    for(auto i : d){
       cout<<d.first<<" "<<d.second<<"\n";
    }
}

Basically I was using map to store the frequencies of elements of array and I tried it on C++14 , but it gives the following error-
error:‘class std::map’ has no member named ‘first’

I think the map has first and second member inbuilt in it, then Why it is giving the error??
If there are any corrections in the code , please let me know.
Posted
Updated 17-Oct-20 0:14am

It should be i.first, not d.first.

C++
<pre>for(auto i : d){
       cout<<i.first<<" "<<i.second<<"\n";
    }
 
Share this answer
 
Quote:
I think the map has first and second member inbuilt in it
Instead of guessing, make use of the documentation: <map> | Microsoft Docs[^]. It is the pair object in the map that contains the first and second properties. See pair Structure | Microsoft Docs[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900