On Dec 11, 6:16 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-11 12:13:39 -0500, mathieu <mathieu.malate...@gmail.comsaid:
Is there anything else that I could have reused ?
Let the compiler do the work:
struct eight_bools
{
unsigned bool0 : 1;
unsigned bool1 : 1;
unsigned bool2 : 1;
unsigned bool3 : 1;
unsigned bool4 : 1;
unsigned bool5 : 1;
unsigned bool6 : 1;
unsigned bool7 : 1;
};
eight_bools data = { 0 };
data.bool6 = true;
data.bool6 = false;
If he's putting this into a map, it probably won't take anymore
data space than a tr1::array< bool >, or a bool[3]. (I think he
said he only had three bools.) Whatever structure he uses will
be placed in a map node, which will also contain the key and a
couple of pointers. And be aligned and padded for pointers, at
least---on a typical machine, that means that anything less than
four bytes will in fact occupy four (or eight) bytes.
In fact, using the tr1::array<boolwill actually result in a
small space saving, because the size of the code needed to read
and write the values will be smaller.
If there's more data than just the 3 bool's, of course, bit
fields may be just the solution. Throw in a couple of ints with
very limited ranges, and the size gain can be very significant.
But of course, if he's worried about memory use, std::map may
not be the data structure he needs.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34