C++11 has implemented data()
member function on std::vector
, which gives you a pointer to the memory array. Does this mean the template specialization std::vector<bool>
have this member as well? Since this specialization doesn't store the data in terms of bool *
, what kind of behavior can you expect from calling data()
?
vector data() function in C++ STL The std::vector::data() is an STL in C++ which returns a direct pointer to the memory array used internally by the vector to store its owned elements. Parameters: The function does not accept any parameters.
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.
A std::vector can never be faster than an array, as it has (a pointer to the first element of) an array as one of its data members. But the difference in run-time speed is slim and absent in any non-trivial program. One reason for this myth to persist, are examples that compare raw arrays with mis-used std::vectors.
It won't compile, unless your implementation has a non-standard extension. The specialisation of std::vector<bool>
, as specified in C++11 23.3.7/1, doesn't declare a data
member.
This page documenting the class explicitely indicates that the specialization does not provide this method.
The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization.
This other page as well as §23.3.7 of the C++ specifications do confirm it.
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