I'm not able to understand what this error means, and what is causing it.
Here is my code, please help me to point out the mistake:
class Solution {
public:
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
{
return (a.second < b.second);
}
vector<int> topKFreAquent(vector<int>& nums, int k)
{
unordered_map<int,int> m;
for(int i=0;i<nums.size();i++)
{
m[nums[i]]++;
}
vector<pair<int,int>> v;
for(auto it = m.begin();it != m.end();it++)
{
v.push_back(make_pair(it->first,it->second));
}
sort(v.rbegin(),v.rend(),sortbysec);
v.resize(k);
return v;
}
};
I'm getting the error in the sort() call, the exact error is:
Line 19: Char 34: error: reference to non-static member function must be called
sort(v.rbegin(),v.rend(),sortbysec);
^~~~~~~~~
I searched on StackOverflow and found an explanation for a different code, but I wasn't able to understand it properly.
Instead of this:
This:
Or as the comment said, just declare your sort function static.
Also, your code still won't compile with this change because
vis of typevector<pair<int,int>>and does not match your return type of<vector<int>