473,503 Members | 2,238 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char* to vector<bool>

Hi. How can I create a vector<bool> efficiently from a char* or a
vector<char> ?

For example, if char* c[2] == (8,10) I want vector<bool> v to be:
(0000100000001010)

Jul 22 '05 #1
3 3444
Alexandros escribió:
Hi. How can I create a vector<bool> efficiently from a char* or a
vector<char> ?

For example, if char* c[2] == (8,10) I want vector<bool> v to be:
(0000100000001010)


It would be enough to be able to get access to each bit of the byte
vector, without creating a vector<bool>. I'd like the most efficient
solution.

Jul 22 '05 #2
"Alexandros" <em***@company.com> wrote...
Alexandros escribió:
Hi. How can I create a vector<bool> efficiently from a char* or a
vector<char> ?

For example, if char* c[2] == (8,10) I want vector<bool> v to be:
(0000100000001010)


It would be enough to be able to get access to each bit of the byte
vector, without creating a vector<bool>. I'd like the most efficient
solution.


Wrap your char array in a class with a special member function to
access those bits:

class MyVectorBool {
int size;
unsigned char *storage;
public:
MyVectorBool(const char* str)
: size(strlen(str))
, storage(new char[size+1])
{
std::copy(str, str + size + 1, storage);
}

// don't forget the other three: d-tor, copy c-tor, assignment op.

bool get_bit(std::size_t n) const
{
unsigned char which_bit = 1 << (n % CHAR_BIT);
return storage[n / CHAR_BIT] & which_bit;
}

void set_bit(std::size_t n, bool towhat)
{
...
}

void clear_bit(std::size_t n)
{
...
}
};

Should be efficient enough...

Victor
Jul 22 '05 #3
On Sat, 27 Dec 2003 12:13:15 +0000, Alexandros wrote:
Alexandros escribió:
Hi. How can I create a vector<bool> efficiently from a char* or a
vector<char> ?

For example, if char* c[2] == (8,10) I want vector<bool> v to be:
(0000100000001010)


It would be enough to be able to get access to each bit of the byte
vector, without creating a vector<bool>. I'd like the most efficient
solution.


Efficient in what? Memory? Lookup time? Programmer time? These are
almost orthogonal quantities for this problem.

And are you sure you need the most efficient solution? On todays hardware
a few cycles and/or bytes are hardly noticed, if at all.

I personally would use a std::bitset if the size is known beforehand. If
not, I might have a look at vector<bool>, but as that has many
limitations, I would also look if a vector<char> (wrapped in a suitable
class) would be acceptable. Also a set<bool> might be acceptable, if only
a few bits are set it might even be very efficient.

Back to your original problem. A vector<bool> has the problem that it does
not resize on access through [], so either resize it on every setting of a
bit, or find the maximum value in c and resize to that maximum. From there
it is a simple loop (or for_each with a suitable functor) to set each bit.

HTH,
M4

Jul 22 '05 #4

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

Similar topics

3
14354
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a program in which I need to store a series of Boolean values. A vector<bool> would be ideal. However, I'm concerned about this data structure because of Scott Meyers'...
3
4130
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
11
1539
by: Michael | last post by:
Righty, 1: Is there a standard library that contain matrices and complex numbers. I need to find eigen values of a 3x3 matrix. 2: Is there a way of getting the pointer to the start of an...
1
2396
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
8
3525
by: Bo Peng | last post by:
Dear list, I am using std::vector<bool> (bit_vector) to store my bit sequence. To access the same sequence from C (to expose to a python module), I need to know the pointer and offset of...
12
6682
by: Piotr | last post by:
In effective STL, it said one should not use vector<bool> but use dequeue<bool> instead. But can dequeue<bool> has random access iterator? and I do this? dequeue<bool> myboolarray; if...
2
4749
by: huomingxu | last post by:
in gdb, when print an element of vector<bool>, it returns the offset of the bi t instead of the value. e.g: (visible is of type vector<bool>) (gdb) p layout._visible $15 = {_M_p = 0x9817d00,...
6
6781
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
8
5354
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
0
7093
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
7291
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,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
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...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
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...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.