 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alexandros Guest
|
Posted: Sat Dec 27, 2003 11:59 am Post subject: 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)
|
|
| Back to top |
|
 |
Alexandros Guest
|
Posted: Sat Dec 27, 2003 12:13 pm Post subject: Re: char* to vector<bool> |
|
|
Alexandros escribió:
| Quote: | 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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Dec 27, 2003 4:03 pm Post subject: Re: char* to vector<bool> |
|
|
"Alexandros" <email (AT) company (DOT) com> wrote...
| Quote: | 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
|
|
| Back to top |
|
 |
Martijn Lievaart Guest
|
Posted: Tue Dec 30, 2003 4:01 pm Post subject: Re: char* to vector<bool> |
|
|
On Sat, 27 Dec 2003 12:13:15 +0000, Alexandros wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|