So, i want to sort a map<string, vector<int>>
by value in this kind of algorithm. There are 3 integers in the vector and i want to sort the map by the third value in the vector in the map in descending. If the third value is the same, sort by the second value descending, if the second value is the same then the first value.
Basically it goes like this:
P0001 10 100 100
P0002 0 0 200
P0003 1 100 100
After sorting:
P0002 0 0 200
P0001 10 100 100
P0003 1 100 100
My code:
#include <bits/stdc++.h>
using namespace std;
struct comp {
template <typename T>
// Comparator function
bool operator()(const T& l,
const T& r) const
{
if(l.second[2] != r.second[2]){
return l.second > r.second;
}
else{
if(l.second[1] != r.second[1]){
return l.second > r.second;
}
else{
if(l.second[0] != r.second[0]){
return l.second > r.second;
}
}
}
}
};
void sort(map<string, vector<int>>& M)
{
set<pair<string, vector<int>>, comp> S(M.begin(), M.end());
for (auto& it : S) {
cout << '\t' << it.first << '\t';
for (int i = 0; i < 3; i++)
{
cout << it.second[i] << "\t";
}
cout << endl;
}
}
int main(){
int Q, N, input;
string masuk, inputkey;
map<string, vector<int>> peserta;
vector<int> inputval;
cin >> Q >> N >> masuk;
for (int i = 0; i < Q; i++)
{
cin >> inputkey;
for (int i = 0; i < 3; i++)
{
cin >> input;
inputval.push_back(input);
}
peserta.insert(pair<string, vector<int>>(inputkey, inputval));
inputval.clear();
}
sort(peserta);
return 0;
}
I have read
https://www.geeksforgeeks.org/sorting-a-map-by-value-in-c-stl/
https://www.geeksforgeeks.org/map-of-vectors-in-c-stl-with-examples/
map of vectors in STL?
So far the inserting of the value works, only the sorting gone wrong.