Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

requirements for std::map keys

0 views
Skip to first unread message

Christopher

unread,
Jan 31, 2009, 6:13:13 PM1/31/09
to
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?

Sam

unread,
Jan 31, 2009, 7:38:39 PM1/31/09
to
Christopher writes:

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

Aside from a copy constructor, yes.

> 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?

std::string has all comparison iterators defined for the class.

James Kanze

unread,
Feb 1, 2009, 7:36:03 AM2/1/09
to
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.

--
James Kanze

Juha Nieminen

unread,
Feb 1, 2009, 8:13:15 AM2/1/09
to
Sam wrote:
> Christopher writes:
>
>> What are the requirements for map keys?
>> If I remember correctly, they just need a operator < ?
>
> 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.

0 new messages