Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

logout

Custom struct in a stl map

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I got this struct that I want to store as _Key in a stl map The problem is that once I try to use it I get a error that I have not defined the "<" operator or somthing.. Now I tryed to do this but I cant for the love of the light figure how to do this...
struct Ident
{
     BYTE  S_Size;
     DWORD S_Pos;
};


// now i want to do
std::map< Ident, StackInfo* >  m_SInfo;

//And use m_SInfo.find()
// But i cant since i havent defined someting or what..
// plz help me !
 

Share this post


Link to post
Share on other sites
Advertisement
A map needs to be able to compare its key types. This is not true of its value types.

Therefore, it is necessary for one of the following to be true:

- Overload the operator < on your structs
- Implement a function object which compares them in the manner of less than, and pass that as a third parameter to the template.

There are also some rules on HOW you implement the less than function.

It''s not clear what purpose having S_Size and S_Pos as a key in your map has, so I will use an example from my own code, which uses a struct which stores two integers, x and y (coordinates in a map)


// Comparator for objects of type Position

struct LtPosition {
bool operator()(const Position p1, const Position p2) const {
if (p1.x == p2.x)
return (p1.y < p2.y);
return p1.x < p2.x;
}
};
typedef std::map<Position, Node *, LtPosition> AllNodesType;


In the above example, we are creating a map from Position structs (which have two integers, x and y), on to Node *, using the comparator LtPosition.

Mark

Share this post


Link to post
Share on other sites

It requires < and I think == operator be defined. It is the same if you use hash map too. It might just be < operator though. It is needed for ordering. STL map uses a red-black tree.



Share this post


Link to post
Share on other sites
An easy workaround if you want structs or objects as keys, is to use a pointer to the struct instead, since a pointer is simply a numeric address, and thus compares easily.

Share this post


Link to post
Share on other sites
quote:
Original post by leiavoia
An easy workaround if you want structs or objects as keys, is to use a pointer to the struct instead, since a pointer is simply a numeric address, and thus compares easily.


But ...

struct Ident IA = { 1, 2 };
struct Ident IB = { 1, 2 };
struct StackInfo * something;

m_SInfo[IA] = something; -> m_SInfo[IB] == something

m_SInfo[&IA] = something; -> m_SInfo[&IB] != something

... if understand it correctly - Im just learning to work with the stl myself.

[edited by - Ketzer on June 4, 2004 3:08:43 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Ketzer
quote:
Original post by leiavoia
An easy workaround if you want structs or objects as keys, is to use a pointer to the struct instead, since a pointer is simply a numeric address, and thus compares easily.


But ...

struct Ident IA = { 1, 2 };
struct Ident IB = { 1, 2 };
struct StackInfo * something;

m_SInfo[IA] = something; -> m_SInfo[IB] == something

m_SInfo[&IA] = something; -> m_SInfo[&IB] != something

... if understand it correctly - Im just learning to work with the stl myself.


This is true. Grokking object equality versus object identity is important for both C++ and Java programmers; but they need to grok it in slightly different ways

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!