473,659 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6715
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.goo glegroups.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<uns igned 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<boo l>.
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.goo glegroups.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<uns igned char>


The reason to avoid std::vector<boo l> is because it doesn't contain
genuine bools. If you can live with std::vector<uns igned 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.goo glegroups.com.. .
: Ivan Vecerina wrote:
: > "Piotr" <ra************ @gmail.com> wrote in message
: > news:11******** **************@ f14g2000cwb.goo glegroups.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<uns igned char>
:
: The reason to avoid std::vector<boo l> is because it doesn't contain
: genuine bools. If you can live with std::vector<uns igned 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

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

Similar topics

3
14361
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' 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).
3
4138
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 (Row,Col) in matrix*/ template <class num_type,template <class T> class functor> num_type & matrix<num_type,functor >::operator()(const int Row,const int Col) {if (Row<rows && Col<cols) {vector<num_type> & x=matrix_core; //num_type & y=x;<-is also...
6
615
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); v.push_back(0);
3
3455
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
1555
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 array from the data stored in a std vector. I load loads of floats into a vector at the moment to store vertex infomration for openGL, but to render them, I need to pass a pointer to the beginning of the array to OpenGL, not a vector. At the
1
2405
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
3541
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 vector::<bool>::iterator (or reference). However, given a std::vector<bool> a, all a.begin(), a etc are instances of a proxy class so I can not do things like &*a.begin(). Is there a safe way to get the information I need? Many thanks in advance.
6
6807
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 vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
8
5359
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 possible to "spoof" std::vector into implementing a "true" vector of bool rather than the specialisation? Say I do: typedef bool boolreally; std::vector<booleallybvec;
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8628
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.