Connecting Tech Pros Worldwide Forums | Help | Site Map

Storing my own key in map

Tommo
Guest
 
Posts: n/a
#1: Nov 17 '05
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


Doug Harrison [MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Storing my own key in map


On 17 Jul 2005 21:16:08 -0700, Tommo wrote:
[color=blue]
> 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[/color]

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
Microsoft MVP - Visual C++
Hendrik Schober
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Storing my own key in map


Doug Harrison [MVP] <dsh@mvps.org> wrote:[color=blue]
> On 17 Jul 2005 21:16:08 -0700, Tommo wrote:
>[color=green]
> > 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[/color]
>
> 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.[/color]


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

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

Schobi

--
SpamTrap@gmx.de 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


Tommo
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Storing my own key in map


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

Doug Harrison [MVP]
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Storing my own key in map


On 25 Jul 2005 16:53:04 -0700, Tommo wrote:
[color=blue]
> 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[/color]

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.

--
Doug Harrison
Microsoft MVP - Visual C++
Closed Thread