我看的vector存储的是start\ end \end_of_storage三个指针,分别代表存储空间的开头,结尾和已使用的部分。push_back如下,先判断空间是否足够,足够的话,直接调用allocator的构造函数进行构造。 void push_back(const value_type& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,__x); ++this->_M_impl._M_finish; } 追踪_Alloc_traits到下面者一句。 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits; 去看下construct函数的行为 // uses placement-new to construct an object of type _Tp at location __p from the arguments __args... static void std::allocator_traits< _Alloc >::construct ( _Alloc & __a, //一个allocator _Tp * __p, //要将对象构造再哪个位置,placement new的参数 _Args &&... __args //用于构造的参数 ) 回到之前push_back函数中的调用 _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,__x); ++this->_M_impl._M_finish; 第一个参数为allocator、第二个为finish指针,其实就是end_of_storage,第三个为push_back传进来的参数。按照construct的行为,会 把对像构造在finish指针的位置,也就是说vector中存的是对象,不是指针
点赞 评论

相关推荐

可以不说话:笔试a了3道半,今天说是挂了😭😭
投递汇丰科技等公司8个岗位
点赞 评论 收藏
分享
Yki_:以下条件优先录用: 喜欢去缅北当猪仔的
点赞 评论 收藏
分享
牛客网
牛客企业服务