By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,611 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,611 IT Pros & Developers. It's quick & easy.

vector<bool>

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
try

std::deque<bool> content;

instead.
Cheers,
Marc

Feb 3 '06 #5

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
<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

P: n/a

"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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.