Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

vector<bool> Class

 

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

The vector<bool> class is a partial specialization of vector for elements of type bool. It has an allocator for the underlying type that's used by the specialization, which provides space optimization by storing one bool value per bit.

template <class Allocator = allocator<bool>>  
class vector<bool, Allocator>  

This class template specialization behaves like vector, except for the differences explained in this article.

Operations that deal with the bool type correspond to values in the container storage. allocator_traits::construct is not used to construct these values.

Typedefs

const_pointerA typedef to a const_iterator that can serve as a constant pointer to a Boolean element of the vector<bool>.
const_referenceA typedef for bool. After initialization, it does not observe updates to the original value.
pointerA typedef to an iterator that can serve as a pointer to a Boolean element of the vector<bool>.

Member Functions

flipReverses all bits in the vector<bool>.
swapExchanges the elements of two vector<bool>s.
operator[]Returns a simulated reference to the vector<bool> element at a specified position.
atFunctions the same as the unspecialized vector::at function, except that it uses the proxy class vector<bool>::reference. Also see operator[].
frontFunctions the same as the unspecialized vector::front function, except that it uses the proxy class vector<bool>::reference. Also see operator[].
backFunctions the same as the unspecialized vector::back function, except that it uses the proxy class vector<bool>::reference. Also see operator[].

Proxy Class

vector<bool> reference ClassA class that acts as a proxy to simulate bool& behavior, and whose objects can provide references to elements (single bits) within a vector<bool> object.

Header: <vector>

Namespace: std

A type that describes an object that can serve as a constant pointer to a Boolean element of the sequence contained by the vector<bool> object.

typedef const_iterator const_pointer;  

A type that describes an object that can serve as a constant reference to a Boolean element of the sequence contained by the vector<bool> object.

typedef bool const_reference;  

Remarks

For more information and code examples, see vector<bool>::reference::operator=.

Reverses all bits in a vector<bool>.

void flip();

Example

// vector_bool_flip.cpp  
// compile with: /EHsc /W4  
#include <vector>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    cout << boolalpha; // format output for subsequent code  
  
    vector<bool> vb = { true, false, false, true, true };  
    cout << "The vector is:" << endl << "    ";  
    for (const auto& b : vb) {  
        cout << b << " ";  
    }  
    cout << endl;  
  
    vb.flip();  
  
    cout << "The flipped vector is:" << endl << "    ";  
    for (const auto& b : vb) {  
        cout << b << " ";  
    }  
    cout << endl;  
}  
  

Returns a simulated reference to the vector<bool> element at a specified position.

vector<bool>::reference operator[](size_type Pos);

vector&<bool&>::const_reference operator[](size_type Pos) const;

Parameters

ParameterDescription
PosThe position of the vector<bool> element.

Return Value

A vector<bool>::reference or vector<bool>::const_reference object that contains the value of the indexed element.

If the position specified is greater than or equal to the size of the container, the result is undefined.

Remarks

If you compile with _ITERATOR_DEBUG_LEVEL set, a run-time error occurs if you attempt to access an element outside the bounds of the vector. For more information, see Checked Iterators.

Example

This code example shows the correct use of vector<bool>::operator[] and two common coding mistakes, which are commented out. These mistakes cause errors because the address of the vector<bool>::reference object that vector<bool>::operator[] returns cannot be taken.

  
// cl.exe /EHsc /nologo /W4 /MTd   
#include <vector>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    cout << boolalpha;  
    vector<bool> vb;  
  
    vb.push_back(true);  
    vb.push_back(false);  
  
    //    bool* pb = &vb[1]; // conversion error - do not use  
    //    bool& refb = vb[1];   // conversion error - do not use  
    bool hold = vb[1];  
    cout << "The second element of vb is " << vb[1] << endl;  
    cout << "The held value from the second element of vb is " << hold << endl;  
  
    // Note this doesn't modify hold.  
    vb[1] = true;  
    cout << "The second element of vb is " << vb[1] << endl;  
    cout << "The held value from the second element of vb is " << hold << endl;  
}  
  

A type that describes an object that can serve as a pointer to a Boolean element of the sequence contained by the vector<bool> object.

typedef iterator pointer;  

The vector<bool>::reference class is a proxy class provided by the vector<bool> Class to simulate bool&.

Remarks

A simulated reference is required because C++ does not natively allow direct references to bits. vector<bool> uses only one bit per element, which can be referenced by using this proxy class. However, the reference simulation is not complete because certain assignments are not valid. For example, because the address of the vector<bool>::reference object cannot be taken, the following code that uses vector<bool>::operator[] is not correct:

    vector<bool> vb;  
    //...  
    bool* pb = &vb[1]; // conversion error - do not use  
    bool& refb = vb[1];   // conversion error - do not use  

vector<bool>::reference::flip

Inverts the Boolean value of a referenced vector<bool> element.

void flip();

Remarks

Example

// vector_bool_ref_flip.cpp  
// compile with: /EHsc /W4  
#include <vector>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
    cout << boolalpha;  
  
    vector<bool> vb = { true, false, false, true, true };  
  
    cout << "The vector is: " << endl << "    ";  
    for (const auto& b : vb) {  
        cout << b << " ";  
    }  
    cout << endl;  
  
    vector<bool>::reference vbref = vb.front();  
    vbref.flip();  
  
    cout << "The vector with first element flipped is: " << endl << "    ";  
    for (const auto& b : vb) {  
        cout << b << " ";  
    }  
    cout << endl;  
}  
/* Output:  
The vector is:  
    true false false true true  
The vector with first element flipped is:  
    false false false true true  
    */  
  

vector<bool>::reference::operator bool

Provides an implicit conversion from vector<bool>::reference to bool.

operator bool() const;

Return Value

The Boolean value of the element of the vector<bool> object.

Remarks

The vector<bool> object cannot be modified by this operator.

vector<bool>::reference::operator=

Assigns a Boolean value to a bit, or the value held by a referenced element to a bit.

reference& operator=(const reference& Right);
reference& operator=(bool Val);

Parameters

Right
The element reference whose value is to be assigned to the bit.

Val
The Boolean value to be assigned to the bit.

Example

// vector_bool_ref_op_assign.cpp  
// compile with: /EHsc  
#include <vector>  
#include <iostream>  
#include <string>  
  
using namespace std;  
  
template <typename C> void print(const string& s, const C& c) {  
    cout << s;  
  
    for (const auto& e : c) {  
        cout << e << " ";  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    cout << boolalpha;  
  
    vector<bool> vb = { true, false, false, true, true };  
  
    print("The vector is: ", vb);  
  
    // Invoke vector<bool>::reference::operator=()  
    vector<bool>::reference refelem1 = vb[0];  
    vector<bool>::reference refelem2 = vb[1];  
    vector<bool>::reference refelem3 = vb[2];  
  
    bool b1 = refelem1;  
    bool b2 = refelem2;  
    bool b3 = refelem3;  
    cout << "The original value of the 1st element stored in a bool: " << b1 << endl;  
    cout << "The original value of the 2nd element stored in a bool: " << b2 << endl;  
    cout << "The original value of the 3rd element stored in a bool: " << b3 << endl;  
    cout << endl;  
  
    refelem2 = refelem1;  
  
    print("The vector after assigning refelem1 to refelem2 is now: ", vb);  
  
    refelem3 = true;  
  
    print("The vector after assigning false to refelem1 is now: ", vb);  
  
    // The initial values are still stored in the bool variables and remained unchanged  
    cout << "The original value of the 1st element still stored in a bool: " << b1 << endl;  
    cout << "The original value of the 2nd element still stored in a bool: " << b2 << endl;  
    cout << "The original value of the 3rd element still stored in a bool: " << b3 << endl;  
    cout << endl;  
}  
/* Output:  
The vector is: true false false true true  
The original value of the 1st element stored in a bool: true  
The original value of the 2nd element stored in a bool: false  
The original value of the 3rd element stored in a bool: false  
  
The vector after assigning refelem1 to refelem2 is now: true true false true true  
The vector after assigning false to refelem1 is now: true true true true true  
The original value of the 1st element still stored in a bool: true  
The original value of the 2nd element still stored in a bool: false  
The original value of the 3rd element still stored in a bool: false  
*/  
  

Static member function that exchanges two elements of Boolean vectors ( vector<bool>) by using the proxy class vector<bool>::reference.

static void swap(
    reference Left,  
    reference Right);

Parameters

Left
The element to be exchanged with the Right element.

Right
The element to be exchanged with the Left element.

Remarks

This overload supports the special proxy requirements of vector<bool>. vector::swap has the same functionality as the single-argument overload of vector<bool>::swap().

Thread Safety in the C++ Standard Library
Standard Template Library

Show:
© 2017 Microsoft