a unique set of unsigned char-arrays?

H

Henrik Goldman

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?



Thanks in advance.



-- Henrik
 
K

Kai-Uwe Bux

Henrik said:
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<T> looks
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
 
D

Daniel T.

"Henrik Goldman said:
Hi,

I'd like to use std::set to get a uniqueness between a set of (unsigned char
*) with fixed known lengths.

typedef unsigned char* Hash;
const size_t hash_size = /* whatever */;

struct hash_less : binary_function< unsigned char*, bool >
{
bool operator()( Hash lhs, Hash rhs ) const {
return memcmp( lhs, rhs, hash_size ) < 0;
}
};

int main() {
set<Hash, hash_less> mySet;
}

I don't have a compiler handy, but I'm pretty sure the above will work.
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top