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