Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector<bool> guaranteed to default all entries to false?

Tags:

c++

vector

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.

like image 322
Josh Brittain Avatar asked Apr 10 '14 09:04

Josh Brittain


People also ask

What is wrong with STD vector bool?

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.

What is default value of Boolean vector?

The default value of the bool type is false .

Is vector bool deprecated?

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.

Is the default value of Bool() false in C++?

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.

What is bool vector in C++?

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.

What is the default value of a vector in C++?

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:

Why is my Bool value- initialized to false?

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


3 Answers

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 bools 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.

like image 82
user657267 Avatar answered Oct 03 '22 03:10

user657267


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

like image 21
Basile Starynkevitch Avatar answered Oct 03 '22 02:10

Basile Starynkevitch


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.

like image 31
Baptiste Wicht Avatar answered Oct 03 '22 02:10

Baptiste Wicht