473,473 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Storing my own key in map

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

Nov 17 '05 #1
4 1070
On 17 Jul 2005 21:16:08 -0700, Tommo wrote:
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
Microsoft MVP - Visual C++
Nov 17 '05 #2
Doug Harrison [MVP] <ds*@mvps.org> wrote:
On 17 Jul 2005 21:16:08 -0700, Tommo wrote:
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.

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

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

Schobi

--
Sp******@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
Nov 17 '05 #3
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

Nov 17 '05 #4
On 25 Jul 2005 16:53:04 -0700, Tommo wrote:
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.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark | last post by:
I have a website with an increasing amount of articles and news reports and so I am thinking of moving away from storing each article as a seperate page to having a single page and storing articles...
4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
5
by: Don Vaillancourt | last post by:
I'm building a system when one can upload a document to the website. I will be storing the document on the hard-drive for quick/easy access, but I was also thinking of storing it in an existing...
6
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
4
by: Rednelle | last post by:
Greetings all, As a newbie, using Access 2000, I would appreciate advice on the best way to include pictures. I have developed a 'Home Inventory' database which can include jpeg thumbnails of...
2
by: Robert Hanson | last post by:
I am new to the asp.net application building and I have read the information regarding the storing of information using session vs cookies vs viewstate. I am asking for suggestions/guidance as to...
2
by: jakk | last post by:
Below is the exception that Iam getting. It says that the DataView that Iam storing in the session is not Serializable. BUt works fine if I store in the inproc session and fails if I switch to...
6
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to...
2
by: Mythran | last post by:
We followed an example found on MSDN to create an encrypted FormsAuthenticationTicket and storing the ticket in a cookie. Is this the "correct" way to store the authentication ticket? We are...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.