Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

Storing my own key in map

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tommo - 18 Jul 2005 05:16 GMT
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] - 18 Jul 2005 05:40 GMT
> 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.

Signature

Doug Harrison
Microsoft MVP - Visual C++

Hendrik Schober - 20 Jul 2005 10:56 GMT
> > I want to have my own class as a key in the STL map.
> >
[quoted text clipped - 4 lines]
> 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

Signature

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 - 26 Jul 2005 00:53 GMT
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] - 26 Jul 2005 02:09 GMT
> Ok, I am having some problems trying to store my KeyType as a pointer.
>
[quoted text clipped - 32 lines]
> 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.

Signature

Doug Harrison
Microsoft MVP - Visual C++


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.