|
-
July 22nd, 2010, 05:00 AM
#15
Re: All combinations of 0's and 1's in 128 bit character array?
 Originally Posted by itsmeandnobodyelse
The vector already holds an internal 16-byte elements array.
If you do
Code:
unsigned char * p = (unsigned char *)&v[0];
some_otherfunction_expecting_a_16bytes_array(p);
where v is one of the permutations you got the pointer p would point to a 16-elements array.
If you want to copy the array to fixed-sized char array, you would/could do
Code:
unsigned char arr[16] = { '\0' };
unsigned char * p = (unsigned char *)&v[0];
memcpy(arr, p, min(sizeof(arr), v.size() * sizeof(v[0])));
The min is only to make sure that the vector really has 16 elements of type char, same as the arr.
Regards, Alex
That is all very true, but the problem remains that if you store bytes inside the vector, when you call next_permutation, you will be per mutating bytes, and not bits. Sure, you can recover the chars easily, but they won't be properlly shifted
A good solution would have been to use a vector<bool>, as it stores bit(-like) objects. This would work perfectly if you could write &v[0] on a vector bool, but you can't (at least not to my knowledge).
IMO, it's a lose-lose situation, where you have to get creative and daisy chain several solutions.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|