ccs said:
Pete C. said:
ccs said:
Which way should chose, based on tradeoff, performance etc.?
bool BArray[100];
vector<bool> BArray;
valarray<bool> BArray;
Thanks in advance!
What do you want to do with it? There's std::bitset<100> also.
- Pete
Thanks. It's for recording a group of switches (on or off).
You asked for performance and tradeoffs. In order to anwser that question,
the *meaning* of the bits is not as important as *which algorithms* you
will typically want to apply to the array/vector/valarray/bitset. Will you
need to copy it often, will you need to iterate through it often, do you
need to count the number of non-zero bits, etc.?
However, keep in mind that performance is something to worry about at the
very end. I would suggest to write your programm in a very clean way so
that you can change this particular data structure without too much effort.
Since performance also depends on the compiler and library, you will want
to do some measurements anyway.
If using std::bitset<100>, should "100" be the number of bits?
Could you give an example to set 5th bit to 1? How to check if 7th bit is
1 or 0?
std::bitset<100> bits;
bits[5] = 1;
if ( bits.test(7) ) {
// 1;
} else {
// 0
}
There are many other methods e.g., for flipping bits and counting the
number of bits set.
Best
Kai-Uwe