how to cast unsigned char to char?

Z

zl2k

hi,
I am trying to use std hash set to contain the 3D points (x, y, z are of
unsigned char type). The easy way comes up is to change the 3 unsigned
char to char* so that the std hash set can handle it. Is there any easy
way to cast the unsigned char to char? Thanks a lot.
zl2k
 
M

Me

zl2k said:
hi,
I am trying to use std hash set to contain the 3D points (x, y, z are of
unsigned char type). The easy way comes up is to change the 3 unsigned
char to char* so that the std hash set can handle it. Is there any easy
way to cast the unsigned char to char? Thanks a lot.

First, hash_set isn't standard (unordered_set is the relevant class in
the TR1 update to C++). Second, your question is how to cast an
unsigned char to char * not how to cast unsigned char to char. But
casting it to char* is not what you want because hash<char*> assumes
you've passed a C-string to it which will likely crash (if you're
lucky). What you probably want is something like:

#include <climits> /* CHAR_BIT */

struct point {
unsigned char x, y, z;
};

struct point_hash {
size_t operator()(const point &a) const
{
size_t ret = a.z;
ret = ret<<CHAR_BIT | a.y;
ret = ret<<CHAR_BIT | a.x;
return ret;
}
};

hash_set<point, point_hash> pointtable;
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top