Henrik Goldman wrote:
Hi,
I'd like to use std::set to get a uniqueness between a set of (unsigned
char *) with fixed known lengths.
So I thought I would write my own class which holds this:
class CUnique
{
public:
unsigned char m_sHash[FIXED_SIZE];
bool operator()(CUnique &Lhs, CUnique &Rhs) const
{
return memcmp(Lhs.m_sHash, Rhs.m_sHash, FIXED_SIZE) != 0;
}
};
The problem however is that std::set doesn't seem to pickup my compare
function. I'm not sure if it's correctly written either but it's best
guess from the sparse examples I found on the net.
Can someone help me on getting the compare function right and thus fix the
compilation problems?
The std::set container does not need a test for equal/non-equal. It does its
thing using a comparison less/greater. The place, where std::set<Tlooks
for such a comparison function by default is std::less<T>, which in turn
defaults to comparing two objects of type T by using operator<. Thus, the
most straight forward way to use your CUnique class with std::set is:
#include <algorithm>
class CUnique {
static unsigned const FIXED_SIZE=20;
unsigned char m_sHash[FIXED_SIZE];
public:
bool operator< ( CUnique const & rhs ) const {
return ( std::lexicographical_compare
( this->m_sHash, this->m_sHash+FIXED_SIZE,
rhs.m_sHash, rhs.m_sHash+FIXED_SIZE ) );
}
};
Here, we define operator< as a member, thus we do not need to pass the lhs
as an argument. You could also define operator< as a freestanding friend.
A few remarks:
a) The class CUnique is not named appropriately. There is nothing unique
about a CUnique. The uniqueness is a property of the set container. As it
stands, you could also store CUnique objects in a std::multiset and
uniqueness would go away.
b) The class CUnique essentially is a little constant-length array class.
You might consider using boost::array< unsigned char instead. It will do
TheRightThing(tm) out of the box in most cases. This, however, depends on
how you want to use this elsewhere in your code. Another alternative is to
use std::string or std::basic_string< unsigned char >.
Best
Kai-Uwe Bux