greg_roberts
asked on
C++ STL MAP and complex keys
I have variable key types which need to index to a record pointer. Within a map the type of key will be the same. So either i handle keys generically or have multiple map classes. However as one of these is a byte array i am not sure how to handle this.
Key types are :
ulong, string (case and case insensitive) (length varies), byte array (essentially a string but a binary string, it can have any chars in it) length fixed.
I have handled the the numeric case and string before (not the case insensitive but that is just forcinf the key to always be lowercase) but nor an arbitary number of binary bytes.
I tried using a class as the key via herbet schildt's STL book but can get the operators you need to do the < and == comapres to compile let alone work.
>> Suggestions, code samples would be appreaciated particular of a key which is a class or a binary stream
Thanks
Key types are :
ulong, string (case and case insensitive) (length varies), byte array (essentially a string but a binary string, it can have any chars in it) length fixed.
I have handled the the numeric case and string before (not the case insensitive but that is just forcinf the key to always be lowercase) but nor an arbitary number of binary bytes.
I tried using a class as the key via herbet schildt's STL book but can get the operators you need to do the < and == comapres to compile let alone work.
>> Suggestions, code samples would be appreaciated particular of a key which is a class or a binary stream
Thanks
Give some examples
>>but can get the operators you need to do the < and == comapres to compile let alone work.
You need global operators like
class BLOB;
bool operator < (const BLOB& l, const BLOB& r);
bool operator == (const BLOB& l, const BLOB& r);
class BLOB
friend bool operator < (const BLOB& l, const BLOB& r);
friend bool operator == (const BLOB& l, const BLOB& r);
public:
//...
private:
BYTE m_strBinary[255];
};
bool operator < (const BLOB& l, const BLOB& r) {
return 0 > memcmp(l.m_strBinary,r.m_s trBinary,2 55);
}
bool operator == (const BLOB& l, const BLOB& r) {
return 0 == memcmp(l.m_strBinary,r.m_s trBinary,2 55);
}
You need global operators like
class BLOB;
bool operator < (const BLOB& l, const BLOB& r);
bool operator == (const BLOB& l, const BLOB& r);
class BLOB
friend bool operator < (const BLOB& l, const BLOB& r);
friend bool operator == (const BLOB& l, const BLOB& r);
public:
//...
private:
BYTE m_strBinary[255];
};
bool operator < (const BLOB& l, const BLOB& r) {
return 0 > memcmp(l.m_strBinary,r.m_s
}
bool operator == (const BLOB& l, const BLOB& r) {
return 0 == memcmp(l.m_strBinary,r.m_s
}
Ooops, there's a parenthesis missing, that should be
class BLOB {
friend bool operator < (const BLOB& l, const BLOB& r);
friend bool operator == (const BLOB& l, const BLOB& r);
public:
//...
private:
BYTE m_strBinary[255];
};
class BLOB {
friend bool operator < (const BLOB& l, const BLOB& r);
friend bool operator == (const BLOB& l, const BLOB& r);
public:
//...
private:
BYTE m_strBinary[255];
};
ASKER
Thanks, this is close to what i have so i will give it another try and post a result and your points soon.
ASKER
My code, my tabs are 4 chars, this form system seems to use 8 , sorry
>> It would be good to resolve this however, in my case i ned to insert keys to records,
find them quickly and delete them. I don't technically need to sort them or iterate through them sorted ...
So are these operators needed by STL to build up fast accessors or are they only needed for sort operations ? i.e. could i survive without them ?
I included ythe code below , thanks !!
Compiler errors
Error 1 error C2804: binary 'operator <' has too many parameters d:\Dev\Panama\Source\api\c
Error 2 error C2333: 'SymKeyClass::operator <' : error in function declaration; skipping function body d:\Dev\Panama\Source\api\c
Error 3 error C2804: binary 'operator ==' has too many parameters d:\Dev\Panama\Source\api\c
Error 4 error C2333: 'SymKeyClass::operator ==' : error in function declaration; skipping function body d:\Dev\Panama\Source\api\c
class SymKeyClass;
typedef std::map<SymKeyClass, void*> SYM_KEY_PAIR;
struct SymKeyObj
{
union
{
unsigned long ulong;
unsigned char* uText;
} sKey;
unsigned short sKeyLength;
char sKeyType;
};
class SymKeyClass
{
public:
SymKeyClass(SymKeyObj* pKey)
{
// Fill out class based on key type
m_Key.sKeyType = pKey->sKeyType;
if (IsSymTypeVarLength(m_Key.
{
m_Key.sKeyLength = (unsigned short)strlen((const char*)pKey->sKey.uText);
}
else if (IsSymTypeFixedLength(m_Ke
{
m_Key.sKeyLength = pKey->sKeyLength;
}
else
{
m_Key.sKeyLength = sizeof(unsigned long);
}
if (IsSymTypePointer(m_Key.sK
{
m_Key.sKey.uText = new unsigned char [m_Key.sKeyLength];
memcpy(m_Key.sKey.uText, pKey->sKey.uText, m_Key.sKeyLength);
}
else
{
m_Key.sKey.ulong = pKey->sKey.ulong;
}
}
~SymKeyClass()
{
// Only if the type was a buffer pointer do we deallocate
if (IsSymTypePointer(m_Key.sK
{
delete m_Key.sKey.uText;
m_Key.sKey.uText = NULL;
}
}
public:
SymKeyObj* get()
{
return &m_Key;
}
// Is one less than the other
bool operator< (SymKeyClass& A, SymKeyClass& B)
{
bool bLess = false;
if (A->m_Key.sKeyType == B->m_Key.sKeyType)
{
if (A->m_Key.sKeyLength == B->m_Key.sKeyLength)
{
if (IsSymTypePointer(A->m_Key
{
bLess = memcmp(A->m_Key.sKey.uText
}
else
{
bLess = (A->m_Key.sKey.ulong < B->m_Key.sKey.ulong);
}
}
}
return bLess;
}
// Are these keys equal
bool operator==(SymKeyObj& A, SymKeyObj& B)
{
bool bEqual = false;
if (A->sKeyType == B->sKeyType)
{
if (A->sKeyLength == B->sKeyLength)
{
if (IsSymTypePointer(A->sKeyT
{
bEqual = memcmp(A->sKey.uText, B->sKey.uText, A->sKeyLength);
}
else
{
bEqual = (A->sKey.ulong == B->sKey->ulong);
}
}
}
return bEqual;
}
private:
SymKeyObj m_Key;
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so the example if the STL book was wrong, it showed both items.
>> Can you tell me if these operators are actually needed if you do not need a sorted order, but just want fast key to record lookup ?
Thanks
>> Can you tell me if these operators are actually needed if you do not need a sorted order, but just want fast key to record lookup ?
Thanks
ASKER
P.S: Is a class the only way to have a search for a key with X bytes but X is known at the time of the search ?
More points on offer
More points on offer
>> P.S: Is a class the only way to have a search for a key with X bytes but X is known at the time of the search ?
No
No