requirements for std::map keys

C

Christopher

What are the requirements for map keys?
If I remember correctly, they just need a operator < ?

I need to be able to look up objects on the fly depending on this an
array of these structures from another library:

typedef struct D3D10_INPUT_ELEMENT_DESC
{
LPCSTR SemanticName;
UINT SemanticIndex;
DXGI_FORMAT Format;
UINT InputSlot;
UINT AlignedByteOffset;
D3D10_INPUT_CLASSIFICATION InputSlotClass;
UINT InstanceDataStepRate;
} D3D10_INPUT_ELEMENT_DESC;

Everything equates to an int or enum except the lowsy LPCSTR which is
a long pointer to a c style string, yuck.

I think I could wrap it and provide comparison operators if need be. I
am not sure how to compare the LPCSTR though. If I create a
std::string from it, is there a built string comparison operators or
do I have to write one?
 
J

James Kanze

Christopher a écrit :
What are the requirements for map keys?

Copiable and assignable. You also have to provide an ordering
function.
If I remember correctly, they just need a operator < ?

They don't need operator<. They do need an ordering function.
I need to be able to look up objects on the fly depending on this an
array of these structures from another library:
typedef struct D3D10_INPUT_ELEMENT_DESC
{
LPCSTR SemanticName;
UINT SemanticIndex;
DXGI_FORMAT Format;
UINT InputSlot;
UINT AlignedByteOffset;
D3D10_INPUT_CLASSIFICATION InputSlotClass;
UINT InstanceDataStepRate;
} D3D10_INPUT_ELEMENT_DESC;
Everything equates to an int or enum except the lowsy LPCSTR
which is a long pointer to a c style string, yuck.
I think I could wrap it and provide comparison operators if
need be. I am not sure how to compare the LPCSTR though. If I
create a std::string from it, is there a built string
comparison operators or do I have to write one?

The obvious solution is to define your own C++ type, which has a
constructor from the above type, with string, unsigned int,
etc., and use that as a key.

The other solution is more complex. Depending on what the
LPCSTR (a char const*?) points to, or more precisely, the
lifetime of the C style string, this class may not support copy
and assignment, in which case, you simply cannot use it.
Otherwise, the function std::strcmp (in <cstring>), or its C
equivalent, can be used for the comparison.
 
J

Juha Nieminen

Sam said:
Aside from a copy constructor, yes.

No, operator< is not required if you give a proper comparator type to
the map. Maps can be used even with existing classes which don't have an
operator<, as long as you can write a comparator for them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top