473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

vector<bool>, bit_vector, or something else?

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' Effective STL's Item 18: Avoid using
vector<bool>.

Plus, I'm loath to use bit_vector since SGI's STL implementation says it
will soon be dropped on the floor
(http://www.sgi.com/tech/stl/bit_vector.html).

Additionally, the bitset won't work because I don't know the number of
values I'll need at compile time. Lastly, Meyers' recommendation of using a
dequeue<bool> scares me a little because it appears that it won't pack the
sequence of bools into bits the way that a bitset does (and, frankly, the
way I'd like my data structure to do since there may be a lot of flags in
this container.)

What is the best way to compactly store a sequence of on/off flags and allow
for indexed addressing?

Thanks,
Scott

--
Remove ".nospam" from the user ID in my e-mail to reply via e-mail.

Jul 19 '05 #1
3 14346
In article <be**********@news01.intel.com>, Scott Brady Drummonds
<sc**********************@intel.com> wrote:

| 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' Effective STL's Item 18: Avoid using
| vector<bool>.
|
| Plus, I'm loath to use bit_vector since SGI's STL implementation says it
| will soon be dropped on the floor
| (http://www.sgi.com/tech/stl/bit_vector.html).
|
| Additionally, the bitset won't work because I don't know the number of
| values I'll need at compile time. Lastly, Meyers' recommendation of using a
| dequeue<bool> scares me a little because it appears that it won't pack the
| sequence of bools into bits the way that a bitset does (and, frankly, the
| way I'd like my data structure to do since there may be a lot of flags in
| this container.)
|
| What is the best way to compactly store a sequence of on/off flags and allow
| for indexed addressing?

With all due respect to Mr. Meyers, std::vector<bool>.

Did the committee screw up when they created vector<bool> as opposed to
bitvector? Yes. But the functionality afforded by vector<bool> is
useful nonetheless. It just has a bad name, that's all.

A future standard might deprecate vector<bool>, but at the same time
introduce bitvector. There's no way (imho) that we will deprecate
vector<bool> without reintroducing that same functionality under a
different name. People need it.

You might protect yourself a bit with:

typedef std::vector<bool> MyVectorBool;

And then use MyVectorBool religiously. Then should you be on a
platform that offers bitvector, or should the standard change out from
under you, your maintenance can be easily accomplished during or after
a three martini lunch with a single change.

Don't be afraid of vector<bool> for the functionality it offers. It is
good stuff. Be wary of vector<bool> because it differs in subtle ways
from vector<int>. And I think that is probably the point to take away
from Scott's ESTL #18.

--
Howard Hinnant
Metrowerks
Jul 19 '05 #2
In article <be**********@news01.intel.com>,
sc**********************@intel.com says...
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' Effective STL's Item 18: Avoid using
vector<bool>.
The problems with vector<bool> almost all stem from the fact that it's
required to be specialized to pack the bools.
Additionally, the bitset won't work because I don't know the number of
values I'll need at compile time. Lastly, Meyers' recommendation of using a
dequeue<bool> scares me a little because it appears that it won't pack the
sequence of bools into bits the way that a bitset does (and, frankly, the
way I'd like my data structure to do since there may be a lot of flags in
this container.)

What is the best way to compactly store a sequence of on/off flags and allow
for indexed addressing?


....and another container that packs the bits won't likely be any better
than vector<bool> anyway.

It comes down to a simple choice: you can leave the bools unpacked, and
get normal semantics for the container. You can pack the bools, and get
compact storage. At least the way C++ is defined right now, I don't
know of a way to pack the bools one per bit, and still get normal
semantics for the container, so if packed bools is what you want, then
vector<bool> is at least as good as most of the alternatives.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #3
Pete Becker <pe********@acm.org> wrote in message news:<3F***************@acm.org>...
Scott Brady Drummonds wrote:

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' Effective STL's Item 18: Avoid using
vector<bool>.


If vector<bool> does what you need, use it.
What is the best way to compactly store a sequence of on/off flags and allow
for indexed addressing?


vector<bool>.


I agree. As long as you're aware of the differences with the <bool>
specialization, you should be fine.
Jul 19 '05 #4

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

Similar topics

3
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...
3
by: Alexandros | last post by:
Hi. How can I create a vector<bool> efficiently from a char* or a vector<char> ? For example, if char* c == (8,10) I want vector<bool> v to be: (0000100000001010)
11
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.