C++11 standard template (STL) std::vector<bool> (2)

Defined in the header file <vector>

template<class Allocator>
class vector<bool, Allocator>;

 std::vector<bool> is a space-efficient specialization of std::vector for type bool.

The space-efficient behavior of std::vector<bool> (and whether it has optimizations) is implementation-defined. One potential optimization involves unioning the elements of the vector so that each element occupies a single bit, rather than sizeof(bool) bytes.

std::vector<bool> behaves like std::vector, but to save space it:

  • elements don't have to be stored as contiguous arrays (so &v[0] + n != &v[n] )
  • Expose class std::vector<bool>::reference as methods for accessing individual bits. In particular, the class of this type is returned by value by operator[].
  • Bit values ​​are not constructed using std::allocator_traits::construct.
  • There is no guarantee that different elements in the same container can be modified concurrently by different threads.

Proxy class representing a reference to a single bool

std::vector<bool>::reference

class reference;

 The std::vector<bool> specialization defines std::vector<bool>::reference as a publicly accessible nested class. std::vector<bool>::reference proxies the behavior of accessing a single bit in a std::vector<bool>.

The basic use of std::vector<bool>::reference is to provide an lvalue that can be operator[]returned from .

Any read or write to the vector that occurs through a std::vector<bool>::reference will potentially read or write the entire underlying vector.

member function

(Constructor)

Construct reference, accessible only to std::vector<bool> itself
(public member function)

(destructor)

destroy reference
(public member function)

operator=

Assign bool to the referenced bit
(public member function)

operator bool

Returns the referenced bit
(public member function)

flip

Flip the referenced bit
(public member function)

std::vector<bool>::reference::~reference

~reference();

Destroy references.

std::vector<bool>::reference::operator=

reference& operator=( bool x );
reference& operator=( const reference& x );

(until C++11)

reference& operator=( bool x ) noexcept;
reference& operator=( const reference& x ) noexcept;

(since C++11)

Assign to the referenced bit.

parameter

x - the value to assign

return value

*this

std::vector<bool>::reference::operator bool

operator bool() const;

(until C++11)

operator bool() const noexcept;

(since C++11)

Returns the referenced bit.

parameter

(none)

return value

The referenced bit.

std::vector<bool>::reference::flip

void flip();

(until C++11)

void flip() noexcept;

(since C++11)

Inverts the referenced bit.

parameter

(none)

vector<bool> specific modifier

flip all bits

std::vector<bool,Allocator>::flip

void flip();

 Flips each bool in vector (replaces it by its opposite value).

parameter

(none)

return value

(none)

swaps two std::vector<bool>::references

std::vector<bool,Allocator>::swap

static void swap(reference x, reference y);

xSwaps  ythe contents of and .

parameter

x - the std::vector<bool>::reference value to yswap
y - the std::vector<bool>::reference value to xswap

return value

(none)

the complexity

constant.

help class

hashing support for std::vector<bool>

std::hash (std::vector<bool>)

template <class Allocator> struct hash<vector<bool, Allocator>>;

(since C++11)

 std::hash The template specialization for std::vector<bool> allows the user to obtain a hash of an object of type std::vector<bool>.

call example

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <vector>

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        bool b = std::rand() % 2 == 0;
        return b;
    };

    std::vector<bool> vecor_bool_1(6);
    std::generate(vecor_bool_1.begin(), vecor_bool_1.end(), generate);
    std::cout << "vecor_bool_1:  ";
    std::copy(vecor_bool_1.begin(), vecor_bool_1.end(),
              std::ostream_iterator<bool>(std::cout, "   "));
    std::cout << std::endl;
    std::cout << std::endl;

    //翻转 vector 中的每个 bool (以其反对值替换)。
    std::cout << "void flip():  " << std::endl;
    vecor_bool_1.flip();
    std::cout << "vecor_bool_1:  ";
    std::copy(vecor_bool_1.begin(), vecor_bool_1.end(),
              std::ostream_iterator<bool>(std::cout, "   "));
    std::cout << std::endl;
    std::cout << std::endl;

    //交换 x 与 y 的内容。
    std::cout << "static void swap(reference x, reference y):  " << std::endl;
    vecor_bool_1.swap(vecor_bool_1[0], vecor_bool_1[1]);
    std::cout << "vecor_bool_1:  ";
    std::copy(vecor_bool_1.begin(), vecor_bool_1.end(),
              std::ostream_iterator<bool>(std::cout, "   "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

 output

 

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/130545226