简体   繁体   English

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

[英]What is the difference between boost::container::vector and std::vector

boost :: container :: vectorstd :: vector有什么区别?

A case where you may need the boost version instead of the standard version is when you encounter the <bool> specialization. 当您遇到<bool>专业化时,可能需要升级版本而不是标准版本的情况。

std::vector<bool> is implemented as a bitset, it doesn't store its element as an array of bool . std::vector<bool>是作为bitset实现的,它不会将其元素存储为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. boost::container::vector<bool>没有这样的专业化。

There are several differences that I could compile: 我可以编译几个不同之处:

° No specialization of boost::container::vector<bool> (source @roeland) °没有boost::container::vector<bool>专业化(来源@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. °使用Boost分配器基础结构,它(特别是在C ++ 1x中)比标准分配器更灵活,不会忽略分配器提供的某些特性。 (source: http://www.boost.org/doc/libs/1_59_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.containers_explained.stl_container_requirements ) (来源: 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? ) 特别是,仍然可以指定referencepointer类型与T&T* (请参阅是否仍然可以自定义STL向量的“引用”类型?

° Support for recursive containers (source: The Boost C++ Libraries by Boris Schäling). °支持递归容器(来源:BorisSchäling的Boost C ++库)。

Some (old?) implementations of STL didn't support incomplete value types (they were not required in the first place), in particular recursive containers. 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 is a specification not an implementation. ° std::vector是一个规范而非实现。 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). 所有平台上只有一个实现boost::container::vector ,因此可以做出更多假设(例如,最初std::vector不需要使用连续内存)(来源:BorisSchäling的Boost C ++库)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM