boost::container::vector与std::vector的区别是什么?

What is the difference between boost::container::vector and std::vector

本文关键字:vector 区别 是什么 container boost std      更新时间:2023-10-16

boost::container::vector和std::vector有什么区别?

当您遇到<bool>专门化时,您可能需要boost版本而不是标准版本。

std::vector<bool>是作为bitset实现的,它不将其元素存储为bool的数组。

这意味着例如下面的代码将不能工作:

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>没有这种专门化

我可以编译以下几个差异:

°boost::container::vector<bool>没有专门化(来源@roeland)

decltype(std::vector<bool>(10)[0]) == std::_Bit_reference
decltype(boost::container::vector<bool>(10)[0]) == bool&

°使用Boost分配器基础结构,它(特别是在c++ 1x中)比标准分配器更灵活,不会忽略分配器提供的某些特征。(来源: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>

特别是,仍然可以指定referencepointer类型不同于T&T*(参见是否仍然可以自定义STL矢量's "reference")类型?)

°支持递归容器(来源:Boost c++ Libraries by Boris Schäling)。

STL的一些(旧的?)实现不支持不完全值类型(一开始就不需要),特别是递归容器。

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是规范而不是实现。在所有平台上只有一个boost::container::vector实现,所以可以做更多的假设(例如,最初std::vector不需要使用连续内存)(来源:Boris的Boost c++ Libraries Schäling)。