小编Art*_*ner的帖子

Stroustrup:reserve() 的 C++ 实现

我不明白 Bjarne Stroustrup - Programming Chapter 19.5.6 RAII for vector 中给出的代码:

template<typename T, typename A>
struct vector_base {
  A alloc;
  T* elem;
  int sz;
  int space;
  vector_base(const A& a, int n) : alloc{a}, elem{alloc.allocate(n)}, sz{n}, space{n}{}
  ~vector_base() {alloc.deallocate(elem,space);}
}

//--------- vector class ------------
template<typename T, typename A = allocator<T>>
class vector : private vector_base<T,A> {
   // ...
};

//--------- vector reserve ------------
template<typename T, typename A>
void vector<T,A>::reserve(int newalloc)
{
  if (newalloc <= this->space) return;
  vector_base<T,A> b(this->alloc,newalloc);
  uninitialized_copy(b.elem, &b.elem[this->sz], this->elem); …
Run Code Online (Sandbox Code Playgroud)

c++

8
推荐指数
1
解决办法
263
查看次数

标签 统计

c++ ×1