vector<bool> push_back

This is a discussion on vector<bool> push_back within the C++ Programming forums, part of the General Programming Boards category; I have a piece of code which converts a string into a vector&lt;bool&gt;. To do that, I have to use ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    vector<bool> push_back

    I have a piece of code which converts a string into a vector<bool>. To do that, I have to use push_back() 8 times for every byte, and it can get into millions of bytes. Is there a way I can speed this up?

    //edit: Also, how are bool's represented in memory? Can I do a union between chars and bools and do it that way?
    Last edited by ygfperson; 03-02-2003 at 09:41 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    Also, how are bool's represented in memory? Can I do a union between chars and bools and do it that way?
    Straight from The C++ Standard Library by Nicolai Josuttis, 6.2.6 Class vector<bool>:
    For Boolean elements of a vector, the C++ standard library provides a specialization of vector. The goal is to have a version that is optimized to use less size than a usual implementation of vector for type bool. Such a usual implementation would reserve at least 1 byte for each element. The vector<bool> specialization usually uses internally only 1 bit for an element, so it is typically eight times smaller. Note that such an optimization also has a snag: In C++, the smallest addressable value must have a size of at least 1 byte. Thus, such a specialization of a vector needs special handling for references and iterators.

    As a result, a vector<bool> does not meet all requirements of other vectors (for example, a vector<bool>::reference is not a true lvalue and vector<bool>::iterator is not a random access iterator). Therefore, template code might work for vectors of any type except bool. In addition, vector<bool> might perform slower than normal implementations because element operations have to be transformed into bit operations. However, how vector<bool> is implemented is implementation specific. Thus, the performance (speed and memory) might differ.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Is there a way to push more than one bit at once into the vector?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Never use vector<bool>. It sucks. Use deque<bool> instead and deal efficiently with real bools and treat it as any other container that supports random access iterators.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Contest_Master
    Guest
    The bitset may be of use: http://www.sgi.com/tech/stl/bitset.html
    The only downside I see is it's size cannot be changed.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21