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.
Return Value
The Boolean value either designated or referred to.
Example
// vector_bool_ref_op_assign.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector<bool> vb = { true, false, false, true, true };
cout << "The vector is: ";
for (const auto& it : vb) {
cout << it << " ";
}
cout << endl;
vector<bool>::reference refelem1 = vb.at(0);
vector<bool>::reference refelem2 = vb.at(1);
vector<bool>::reference refelem3 = vb.at(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;
cout << "The vector after asignment of ref1 to ref2 is now: ";
for (const auto& it : vb) {
cout << it << " ";
}
cout << endl << endl;
refelem3 = true;
cout << "The vector after assigning false to ref1 is now: ";
for (const auto& it : vb) {
cout << it << " ";
}
cout << endl << endl;
// 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: 1 0 0 1 1
The original value of the 1st element stored in a bool: 1
The original value of the 2nd element stored in a bool: 0
The original value of the 3rd element stored in a bool: 0
The vector after asignment of ref1 to ref2 is now: 1 1 0 1 1
The vector after assigning false to ref1 is now: 1 1 1 1 1
The original value of the 1st element still stored in a bool: 1
The original value of the 2nd element still stored in a bool: 0
The original value of the 3rd element still stored in a bool: 0
Requirements
Header: <vector>
Namespace: std