成员变量
//迭代器
//对应begin()iterator start;
//对应end(), 指向可⽤空间下⼀个位置iterator finish;
//指向整个容量的⼀下个位置iterator end_of_storage;
push_back,
将新元素插⼊vector尾端时,先检查是有还有备⽤空间,如果有就直接在备⽤空间上构造元素,并调整迭代器finish。 如果没有备⽤空间,就要扩充空间(重新配置,移动数据,释放原空间)
void push_back(const T& x){
if(finish != end_of_storage) {
construct(finish, x); ++finish; } else
insert_aux(end(), x);}
insert_aux,单个元素插⼊指定位置
template void MVector // 空间⾜够 if (finish != end_of_storage) { construct(finish, *(finish - 1)); ++finish; T x_copy = x; // copy_backward(position, finish - 2, finish - 1); *position = x_copy; } else { // 空间不⾜,重新分配空间 const size_type old_size = size(); const size_type len = old_size != 0 ? 2 * old_size : 1; iterator new_start = data_allocator::allocate(len); iterator new_finish = new_start; try { // 前段拷贝 new_finish = uninitialized_copy(start, position, new_start); // 构造插⼊的元素 construct(new_finish, x); ++new_finish; // 后段拷贝 uninitialized_copy(position, finish, new_finish); } catch (...) { // 回滚 destroy(new_start, new_finish); data_allocator::deallocate(new_start, len); throw; } // 释放⽼内存 destroy(begin(), end()); deallocate(); start = new_start; finish = new_finish; end_of_storage = new_start + len; }} 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- kqyc.cn 版权所有 赣ICP备2024042808号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务