I was trying to make a map sort by value using a custom comparator but I couldn\'t figure out why I kept getting the error of \"no matching call to compareByVal\"
Here\'
This may be an X-Y issue.
If you need to sort by both key and value, then a single std::map
may not be the most efficient choice.
In database theory, all the data would be placed into a single table. An index table would be created describing the access or sorting method. Data that needs to be sorted in more than one method would have multiple index tables.
In C++, the core table would be a std::vector
. The indices would be std::map
, std::map
, where vector_index
is the index of the item in the core table.
Example:
struct Record
{
int age;
std::string name;
};
// Core table
std::vector database;
// Index by age
std::map age_index_table;
// Index by name
std::map name_index_table;
// Fetching by age:
unsigned int database_index = age_index_table[42];
Record r = database[database_index];
// Fetching by name:
unsigned int database_index = name_index_table["Harry Potter"];
Record r = database[database_index];
You can learn more by searching the internet for "database index tables c++".
If it looks like a database and smells like a database ...