What is the difference between boost::container::vector and std::vector?
1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.
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.
Vectors are sequence containers representing arrays that can change in size.
A case where you may need the boost version instead of the standard version is when you encounter the <bool>
specialization.
std::vector<bool>
is implemented as a bitset, it doesn't store its element as an array of bool
.
This means for instance the following code will not work:
template<T>
void handleElement(T &element);
// suppose we get a bool vector:
std::vector<bool> v = ....;
// then this fails because v[i] is a proxy object
handleElement(v[0]);
boost::container::vector<bool>
has no such specialization.
There are several differences that I could compile:
° No specialization of boost::container::vector<bool>
(source @roeland)
decltype(std::vector<bool>(10)[0]) == std::_Bit_reference
decltype(boost::container::vector<bool>(10)[0]) == bool&
° Uses Boost allocator infrastructure, which (especially in C++1x) is more flexible that the standard allocator, doesn't ignore certain traits that are provided by the allocator. (source: http://www.boost.org/doc/libs/1_59_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.containers_explained.stl_container_requirements)
std::vector<double>::allocator_type == std::allocator<double>
boost::container::vector<double>::alloctor_type == boost::container::new_allocator<double>
In particular, one can still specify the reference
and pointer
types to be different from T&
and T*
(see Is it still possible to customize STL vector's "reference" type?)
° Support for recursive containers (source: The Boost C++ Libraries by Boris Schäling).
Some (old?) implementations of STL didn't support incomplete value types (they were not required in the first place), in particular recursive containers.
using boost::container::vector;
struct animal{
vector<animal> children; // may not work with std::vector
};
int main(){
animal parent;
animal child1;
animal child2;
parent.children.push_back(child1);
parent.children.push_back(child2);
}
° std::vector
is a specification not an implementation.
There is only one implementation boost::container::vector
across all platforms, so more assumptions can be made (for example originally std::vector
was not required to use contiguous memory) (source: The Boost C++ Libraries by Boris Schäling).
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