473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

a unique set of unsigned char-arrays?

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

Aug 5 '06 #1
2 2323
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
Aug 5 '06 #2
In article <44***********************@dread15.news.tele.dk> ,
"Henrik Goldman" <he************@mail.tele.dkwrote:
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_lessmySet;
}

I don't have a compiler handy, but I'm pretty sure the above will work.
Aug 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lallous | last post by:
Hello, This code works fine when 'size' is less than 32768 however when size is bigger this function never returns. Can't find out why?! If I break into the code I can see that 'i' is 32768.......
5
by: Laphan | last post by:
Hi All I know you can set a row to be unique, but I want expand on this in that I want the DB schema to only disallow an entry if the row isn't unique across 3 fields. Is it possible? My DDL...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
9
by: userblue | last post by:
Hi Does anyone know if there is a way to define what is effectively a single globally visible, enumerated list whilst actually defining the entries across several different modules? or somehow do...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
17
by: mdh | last post by:
I am working on 5.11 ( once again). So, just stepping through code in the debugger, and get this...and cannot proceed. Have posted to the Xcode forum, but my guess is the real gurus are here in...
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.