Storing my own key in map

  • Thread starter Thread starter Tommo
  • Start date Start date
T

Tommo

I want to have my own class as a key in the STL map.

Can anyone tell me what operators i need to override?? I think its the
< operator but I think I may be missing something else
 
I want to have my own class as a key in the STL map.

Can anyone tell me what operators i need to override?? I think its the
< operator but I think I may be missing something else

In order to use the default less<K> comparison type, you have to define
operator<, which must induce a strict weak ordering on values of your key
type. Beyond this, your key type must be copy constructible and assignable.
 
Doug Harrison said:
In order to use the default less<K> comparison type, you have to define
operator<, which must induce a strict weak ordering on values of your key
type. Beyond this, your key type must be copy constructible and assignable.


To add to what Doug said, make sure you
allow comparison of const objects:

bool operator<(const MyKey&, const MyKey&);

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 
Ok, I am having some problems trying to store my KeyType as a pointer.

Error message below ( may not be that helpful )

Error: Could not find a match for std::map<MarketKey*, market_item_t*,
std::less<MarketKey*>, std::allocator<std::pair<MarketKey*const,
market_item_t*>>>::find(const MarketKey*).

It seems the find method is failing.

The find method is :

MktIter it = marketsMap->find(key);

where MktIter is : map < MarketKey*, market_item_t* >::iterator
MktIter;

my map is defined as : map < MarketKey*, market_item_t* > *marketsMap;

My MarketKey is:

class MarketKey : public BaseKey
{
public:
MarketKey(){};
virtual ~MarketKey(){};
bool operator<(const MarketKey &rhs) const;

void setMarket(uint8_t market);
uint8_t getMarket() const;

};

Now is there anything special i have to do with operator< now I am
wanting to store pointers as keys?? It works fine with 'by value' keys
but as soon as i try and use pointers i get compiliation errors
 
Ok, I am having some problems trying to store my KeyType as a pointer.

Error message below ( may not be that helpful )

Error: Could not find a match for std::map<MarketKey*, market_item_t*,
std::less<MarketKey*>, std::allocator<std::pair<MarketKey*const,
market_item_t*>>>::find(const MarketKey*).

It seems the find method is failing.

The find method is :

MktIter it = marketsMap->find(key);

where MktIter is : map < MarketKey*, market_item_t* >::iterator
MktIter;

my map is defined as : map < MarketKey*, market_item_t* > *marketsMap;

My MarketKey is:

class MarketKey : public BaseKey
{
public:
MarketKey(){};
virtual ~MarketKey(){};
bool operator<(const MarketKey &rhs) const;

void setMarket(uint8_t market);
uint8_t getMarket() const;

};

Now is there anything special i have to do with operator< now I am
wanting to store pointers as keys?? It works fine with 'by value' keys
but as soon as i try and use pointers i get compiliation errors

Your key type is a pointer, but your operator< is defined on references.
You need to define an operator< on pointers, but you can't write
operator<(T*,T*), because at least one parameter has to be of a
user-defined type, and all pointer types are considered built-in. So you'll
have to write a comparison class, e.g.

struct DerefLess
{
template<class T>
bool operator()(const T* x, const T* y) const
{
return *x < *y;
}
};

This class can take advantage of any type T that defines operator<, and you
could use it like this:

typedef map < MarketKey*, market_item_t*, DerefLess > MarketMap;

Of course, you can write it without using templates or depending on an
operator< defined on MarketKey objects:

struct DerefMarketKeyLess
{
bool operator()(const MarketKey* x, const MarketKey* y) const
{
return x->getMarket() < y->getMarket();
}
};

There's yet another variant that makes DerefLess a class template whose
operator() is just a normal member function, not a member template.
 
Back
Top