I'm trying to sort a list of structs. I'm not very familiar with the stl list.
Could anyone explain the sort function and how it operates?
The Program:
I read in from a file into a map. Then I transfer the information from the map into a linked list of structs. All of that works properly. I need to sort the list according to the int count and then display. Any suggestions would be appreciated. It is not required that I use a linked list, I just thought it might be easier.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <list>
usingnamespace std;
struct Data
{
string key;
int count;
};
bool compare(Data first, Data second)
{
if (first.count > second.count) returntrue;
returnfalse;
}
int main()
{
list<Data> dataList;
// do things
dataList.sort(compare);
}