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::insert
member function with invalid parameters: there are two integer values provided; but there must be std::pair<int, int>
. Please see the reference: std::map::insert.
For convenience (just not to repeat the map type parameters), create a typedef
for the map:
typedef std::map<int, int> IntMap;
The std::map
has type definition for std::pair
(pair representation) – std::map::value_type
.
So, for example, if there is a std::map<int, int>
the std::map::value_type
would be std::pair<int, int>
.
Use the std::map::value_type
constructor (IntMap::value_type
in this case):
class Row {
public:
void Row::addNumber(int num, int pos)
{
m_numMap.insert(IntMap::value_type(num, pos));
}
private:
typedef std::map<int, int> IntMap;
IntMap m_numMap;
};
Use std::make_pair()
function:
#include <utility>
...
void Row::addNumber(int num, int pos)
{
numMap.insert(std::make_pair(num, pos));
}
Directly use std::pair
constructor:
void Row::addNumber(int num, int pos)
{
numMap.insert(std::pair<int, int>(num, pos));
}
A bit more pedantic than Sergey's answer (which also definitely works), instead Use:
typedef std::map<int, int, std::greater<int> > MyMap;
MyMap numMap;
void Row::addNumber(int num, int pos)
{
numMap.insert(MyMap::value_type(num, pos));
}
The benefit being that if you change the type of the map, you have less code to change later. And much less likely but still possible, if the implementation of std::map
changes its value_type
from std::pair
to something else (in a future version of the stl
), you are impervious to that change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With