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 !
Custom struct in a stl map
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...
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)
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
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 Positionstruct 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
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.
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.
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]
quote:Original post by Ketzerquote: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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement