473,320 Members | 2,052 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,320 software developers and data experts.

vector<bool>

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 (myboolarray[3]) {
// do this...
}

if not, is there another solution?

Thank you.

Feb 3 '06 #1
12 6665
Piotr wrote:
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 (myboolarray[3]) {
// do this...
}


Teach a man to fish...

http://www.sgi.com/tech/stl/Deque.html

(Which Effective C++ Item# is RTFM?)

Luke

P.S. Yes.

Feb 3 '06 #2
Piotr wrote:
In effective STL, it said one should not use vector<bool> but use
dequeue<bool> instead.
The container is called a "deque", not a "dequeue." Leaving aside the
spelling error, that's often good advice, unless you can live with
vector<bool>'s limitations.
But can dequeue<bool> has random access iterator?
Yes, but again, it's a deque<bool>, not dequeue<bool>.
and I do this?
dequeue<bool> myboolarray;
if (myboolarray[3]) {
// do this...
}


Yes, if you fix the spelling error.

Best regards,

Tom

Feb 3 '06 #3
Thanks. I tried this, but it fails with this compiler error:

Matcher.h:11: error: ISO C++ forbids declaration of 'deque' with no
type
Matcher.h:11: error: expected ';' before '<' token

Can you please tell me why?
#include <deque>
#include <algorithm>

class Matcher {

protected:
deque<bool> content;
};

Feb 3 '06 #4
try

std::deque<bool> content;

instead.
Cheers,
Marc

Feb 3 '06 #5
sa***************@gmail.com wrote:
Thanks. I tried this, but it fails with this compiler error:

Matcher.h:11: error: ISO C++ forbids declaration of 'deque' with no
type
Matcher.h:11: error: expected ';' before '<' token

Can you please tell me why?
#include <deque>
#include <algorithm>

class Matcher {

protected:
deque<bool> content;
};


Try std::deque<bool> content;

Alan
Feb 3 '06 #6
sa***************@gmail.com wrote:
Thanks. I tried this, but it fails with this compiler error:
Thanks for what? Please quote!
Matcher.h:11: error: ISO C++ forbids declaration of 'deque' with no
type
Matcher.h:11: error: expected ';' before '<' token

Can you please tell me why?
#include <deque>
#include <algorithm>

class Matcher {

protected:
deque<bool> content;


std::deque<bool> content;

--
Ian Collins.
Feb 3 '06 #7
"Piotr" <ra************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
: 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 (myboolarray[3]) {
: // do this...
: }
:
: if not, is there another solution?

Yet another alternative might be to store bools into a:
std::vector<unsigned char>
This of course has caveats if you rely on implicit
conversions when storing new elements.

You may also want to consider std::bitset, and maybe you don't
need to care about the flaws of std::vector<bool>.
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Feb 3 '06 #8
Ivan Vecerina wrote:
"Piotr" <ra************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
: In effective STL, it said one should not use vector<bool> but use
: dequeue<bool> instead....
is there another solution?

Yet another alternative might be to store bools into a:
std::vector<unsigned char>


The reason to avoid std::vector<bool> is because it doesn't contain
genuine bools. If you can live with std::vector<unsigned char>, you
definitely
don't need genuine bools. So your replacement works only if it is not
needed, sorry.

Michiel Salters

Feb 3 '06 #9
<Mi*************@tomtom.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
: Ivan Vecerina wrote:
: > "Piotr" <ra************@gmail.com> wrote in message
: > news:11**********************@f14g2000cwb.googlegr oups.com...
: > : In effective STL, it said one should not use vector<bool> but use
: > : dequeue<bool> instead....
: > is there another solution?
: >
: > Yet another alternative might be to store bools into a:
: > std::vector<unsigned char>
:
: The reason to avoid std::vector<bool> is because it doesn't contain
: genuine bools. If you can live with std::vector<unsigned char>, you
: definitely don't need genuine bools. So your replacement works only
: if it is not needed, sorry.

This seems like a very decisive all-encompassing statement.
I do not agree that 'needing references to bool elements'
is the only reason to avoid vector<bool>.
The requirement could be to 'store true/false values in a contiguous
array', and then deque<bool> won't do, but vector<uchar> might.
A trade-off does exist.

For example, I happen to use 'unsigned char' buffers to store
B/W 'binary' images - even when only a bool really is needed.
A contiguous buffer addressable with plain pointers facilitates
the implementation of image processing algorithms.

Peace,
Ivan

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Feb 3 '06 #10

"Ivan Vecerina" <IN*****************@ivan.vecerina.com> wrote in message
news:dr**********@news.hispeed.ch...
You may also want to consider std::bitset, and maybe you don't
need to care about the flaws of std::vector<bool>.


Or boost::dynamic_bitset if you need the size to change.

Feb 3 '06 #11
Mi*************@tomtom.com wrote:
The reason to avoid std::vector<bool> is because it doesn't contain
genuine bools. If you can live with std::vector<unsigned char>, you
definitely
don't need genuine bools. So your replacement works only if it is not
needed, sorry.


Isn't there an appreciable space tradeoff, which may matter in some
cases?

Luke

Feb 3 '06 #12

"Piotr" <ra************@gmail.com> skrev i meddelandet
news:11**********************@f14g2000cwb.googlegr oups.com...
In effective STL, it said one should not use vector<bool> but use
dequeue<bool> instead.

But can dequeue<bool> has random access iterator?
Technically, std::vector<bool>::iterator isn't a random access
iterator. It fulfills most of the requirements, but not all. And there
are no bools stored in the vector either, despite its name. That's
where the problems start. It's an almost-container with an
almost-random-access-iterator.

On the other hand, std::deque<bool> does have a proper random access
iterator, and an operator[] to access its members.
and I do this?
dequeue<bool> myboolarray;
Yes.
if (myboolarray[3]) {
// do this...
}
Yes.

That works for std::vector<bool> as well, though.

if not, is there another solution?


It depends on what you try to do. As always. .-)
Bo Persson
Feb 3 '06 #13

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

Similar topics

3
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
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...
6
by: Alexandros | last post by:
Can anyone tell me how to convert several bits stored in a vectro<bool> to bytes (char)? for example: vector<bool> v; v.reserve(8); v.push_back(0); v.push_back(0); v.push_back(0);...
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.