【问题标题】:What is the difference between boost::container::vector and std::vectorboost::container::vector 和 std::vector 有什么区别
【发布时间】:2015-10-01 22:32:33
【问题描述】:

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

【问题讨论】:

  • 我发现了一个相关问题,可能也回答了这个问题:stackoverflow.com/questions/22584685/…
  • IIRC,Boost.Container 与 Boost.Move 兼容,这意味着它的容器在没有 C++11 的情况下支持移动语义。
  • @jfritz42 同意,因为您使用的是矢量,所以我的评论与您更相关

标签: c++ boost vector


【解决方案1】:

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

std::vector<bool> 实现为位集,它不会将其元素存储为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&lt;bool&gt; 没有这样的专业化。

【讨论】:

    【解决方案2】:

    我可以编译几个不同的地方:

    ° boost::container::vector&lt;bool&gt; 没有特化(来源@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&amp;T*(参见Is it still possible to customize STL vector's "reference" type?

    ° 支持递归容器(来源:Boris Schäling 的 Boost C++ 库)。

    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 Schäling 的 Boost C++ 库)。

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 2023-04-09
      • 2021-03-23
      • 1970-01-01
      • 2021-11-07
      • 2020-12-27
      • 2011-02-28
      相关资源
      最近更新 更多