unordered_map<ll,vector> extra;
for(auto p:extra){
sort(p.second.begin(),p.second.end(),greater());
}
can someone tell me how to sort the vector when it is taken as key in map
I don’t know what your actual code looks like as you haven’t formatted it, but in the code you’ve provided, for it to work p
should be taken by reference, not value.
e.g.
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
unordered_map<int,vector<int>> extra;
extra[5] = {1,7,6,4};
for(auto& p:extra){
sort(p.second.begin(),p.second.end(),greater<>());
}
for (const auto& x : extra[5])
{
cout << x << " ";
}
cout << endl;
}
1 Like
thanks mahn!
1 Like