473,946 Members | 15,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(CUni que &Lhs, CUnique &Rhs) const

{

return memcmp(Lhs.m_sH ash, 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 2354
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()(CUni que &Lhs, CUnique &Rhs) const

{

return memcmp(Lhs.m_sH ash, 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::lexicograp hical_compare
( this->m_sHash, this->m_sHash+FIXED_ SIZE,
rhs.m_sHash, rhs.m_sHash+FIX ED_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(t m) 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_stri ng< unsigned char >.

Best

Kai-Uwe Bux
Aug 5 '06 #2
In article <44************ ***********@dre ad15.news.tele. dk>,
"Henrik Goldman" <he************ @mail.tele.dkwr ote:
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
11331
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.... void MakeRandomArray(unsigned long **a, unsigned long size) { unsigned long *data = new unsigned long ; double sizef = (double)(size - 1);
5
602
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 for this table is as follows: CREATE TABLE `WEBSTRINGS` ( `STRINGID` INT NOT NULL AUTO_INCREMENT,
3
31528
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 template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
3
41986
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 0x%x\n",(unsigned char)b); }
9
2796
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 a similar thing with macros. Details: I have a c project to fit into a small microprocessor and need to save some ram. I have a significant number of flags all over the place that currently use whole byte storage. I thought if I had a way to...
5
7823
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 of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this function:
5
2861
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
11829
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
17
1559
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 C!! Not sure if this is a strict "C" issue, but I am sure you will let me know !! :-) The code is simply this, with 2 command line arguments: #include <stdio.h>
29
10051
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
10150
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9975
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11552
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11141
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10679
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7403
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4928
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4524
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.