boost::container::vector与std::vector的区别是什么?
What is the difference between boost::container::vector and std::vector
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>
特别是,仍然可以指定reference
和pointer
类型不同于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)。
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.back()和vector.end()有什么区别
- C++ std::vector 创建对象然后添加对象与在向量中创建对象之间的区别?
- std::vector<int> 和 std::vector* 有什么区别<int>?
- "auto x = vector<int>()"和"vector<int> x"有什么区别?
- vector.size()= 0和vector.empty()之间是否有区别
- std::vector和llvm::SmallVector之间有什么区别?什么时候用哪一个
- std::vector::front() 和 begin() 之间的区别
- vector::p ush_back 和 string::p ush_back 之间的区别
- Vector
与OpenCV中的Vector 的区别 - 使用 vector<int>::size_type 和普通整数有什么区别?
- 使用 std::vector 和 include <vector>之间的区别
- 这两个 std::vector 的赋值方法有什么区别?
- STL队列(OR堆栈)的deque和链表(+vector)实现之间有什么区别
- std::vector和std::array初始值设定项列表之间的区别
- "vector<pair<int,int>> q;"和"vector<pair<int,int> > q;"有什么区别
- 做vector<vector<T...>>和vector<vector...有什么区别<T>>
- c++中vector的insert和emplace有什么区别?
- function(vector v)<int>和function(vector<int>& v)有什么区别?
- std::string 和 std::vector 有什么区别<char>?