I’m creating a std::map<int, int> in C++ that I’d prefer to have they keys sorted from highest to lowest instead of the default sort order. My research lead me to std::greater which looked promising but when trying to use it I’m getting a compile error:
invalid type argument of unary ‘*’ (have ‘int’)
My map declaration is:
std::map<int, int, std::greater<int> > numMap;
And the error is getting thrown from this function:
void Row::addNumber(int num, int pos) {
numMap.insert(num, pos);
}
Answers to similar questions such as this include parenthesis in the declaration, i.e. std::greater() – but when I include those I get multiple errors regarding a function returning a function.
The problem – call of
std::map::insertmember function with invalid parameters: there are two integer values provided; but there must bestd::pair<int, int>. Please see the reference: std::map::insert.Preferable option
For convenience (just not to repeat the map type parameters), create a
typedeffor the map:The
std::maphas type definition forstd::pair(pair representation) –std::map::value_type.So, for example, if there is a
std::map<int, int>thestd::map::value_typewould bestd::pair<int, int>.Use the
std::map::value_typeconstructor (IntMap::value_typein this case):Alternatives:
Use
std::make_pair()function:Directly use
std::pairconstructor: