Thread: How get bool's from std::vector<bool>

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How get bool's from std::vector<bool>

    I don't know how many people know this but a vector of bools is not a container. (see this: http://www.gotw.ca/publications/mill09.htm for more info)

    I have a template'd function that works for everything except a vector of bools (again read the article to see why).

    Code:
    //My function is essentially like this
    template<class T>
    void f( T& t )
    {
        //Neither of these two statements work with bool
        typename T::value_type* p1 = &t[0];
        typename T::value_type* p2 = &*t.begin();
    
        // ... do something with *p1 and *p2 ...
    }
    So my question is: what's the best way to get the data from a vector of bools? Is the only way to loop through it and do it one item at a time?

    I tried:

    #define STD_VECTOR_BOOL_NOT_SPECIAL

    but that didn't do anything (using g++ 3.4.5).

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is there a reason you need it to work with a vector<bool>?

    I don't believe you can get the data out as an array.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You cannot get the data as an array, because the data is not there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by CornedBee View Post
    You cannot get the data as an array, because the data is not there.
    Yeah, I'm gonna have to force it to be a vector of chars instead.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Have you considered using std::basic_string<bool> instead?
    Having the word "string" in the name is obviously confusing, but I believe it should provide functionality that's pretty similar to a vector.
    Scott Meyers seems to agree with my suggestion and included it on his Book Errata list for "Effective STL" http://aristeia.com/BookErrata/estl1...ta_frames.html (search for "cpj").

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're mostly okay with vector<bool> as long as you don't try and take the address of one of the items. Just get a copy of each bool from an iterator or by index, and set it by assigning to the iterator or the setting the bool by array index.
    If that's not an option, then you need to use a vector of chars instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    or a deque<bool>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting true's and false's from bools
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2007, 02:57 PM
  2. WM_CREATE and BOOLs
    By kidburla in forum Windows Programming
    Replies: 3
    Last Post: 09-18-2006, 11:55 PM
  3. bools...
    By Finchie_88 in forum C++ Programming
    Replies: 18
    Last Post: 10-07-2004, 08:05 AM