I want all of the entries in my std::vector to be false after I resize it to the desired container size. This appears to be the case on testing, but I can't seem to find any documentation that guarantees this to always be the case.
I realize I could set everything to false myself, but this seems inefficient if it is already guaranteed to default to false for every entry (it is a decent sized vector and will be created in thousands of places).
Is there any guarantee of this? Thanks in advance.
First, what's wrong with vector<bool> ? Because vector<bool> holds bits instead of bools, it can't return a bool& from its indexing operator or iterator dereference. This can play havoc on quite innocent looking generic code.
The default value of the bool type is false .
There is a general consensus among the C++ Standard Committee and the Library Working Group that vector<bool> should be deprecated and subsequently removed from the standard library, while the functionality will be reintroduced under a different name.
Cheers. Yes, a std::vector<bool> is initialized with zeroed bytes, which are false (the default value for initialized bool -s) on most machines. bool (), the default value, is false on ALL c++ systems, not most.
vector<bool> Class. 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.
The default value of a vector is 0. // For declaring vector v1 (size); // For Vector with default value 0 vector v1 (5); We can also specify a random default value for the vector. Inorder to do so, below is the approach:
So your bool ends up being value-initialized, which for bool s means zero-initialization, which means false. If m is a custom allocator and for some reason is missing construct (), it still ends up being value-initialized. As it has been pointed out std::vector<bool> does not use the standard allocator, still resize () is defined as
resize
will default-insert elements, going by the (C++11) standard this calls:
allocator_traits<bool>::construct(m, p)
Where m
is an allocator, and p
points to the target address.
This in turn (assuming m
is a standard allocator) calls
m.construct(p)
which in turn calls
::new((void *)p) bool()
So your bool
ends up being value-initialized, which for bool
s means zero-initialization, which means false
.
If m
is a custom allocator and for some reason is missing construct()
, it still ends up being value-initialized.
As it has been pointed out std::vector<bool>
does not use the standard allocator, still resize()
is defined as
void resize(size_type sz, bool c = false);
so the result is the same.
Yes, a std::vector<bool>
is initialized with zeroed bytes, which are false
(the default value for initialized bool
-s) on most machines.
See the note on std::vector
In my opinion, if you use:
vec.resize(N, false);
Every entry should be set to false
. Isn't that what you need ?
Moreover, if you don't specify false
, it'll take the default value, which is false
for bool
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With