<i id='KXIQv'><tr id='KXIQv'><dt id='KXIQv'><q id='KXIQv'><span id='KXIQv'><b id='KXIQv'><form id='KXIQv'><ins id='KXIQv'></ins><ul id='KXIQv'></ul><sub id='KXIQv'></sub></form><legend id='KXIQv'></legend><bdo id='KXIQv'><pre id='KXIQv'><center id='KXIQv'></center></pre></bdo></b><th id='KXIQv'></th></span></q></dt></tr></i><div id='KXIQv'><tfoot id='KXIQv'></tfoot><dl id='KXIQv'><fieldset id='KXIQv'></fieldset></dl></div>
  • <small id='KXIQv'></small><noframes id='KXIQv'>

  • <tfoot id='KXIQv'></tfoot>

      • <bdo id='KXIQv'></bdo><ul id='KXIQv'></ul>

      <legend id='KXIQv'><style id='KXIQv'><dir id='KXIQv'><q id='KXIQv'></q></dir></style></legend>

        C++中vector是如何实现的

        时间:2023-10-07

          <bdo id='cAlhW'></bdo><ul id='cAlhW'></ul>

          <small id='cAlhW'></small><noframes id='cAlhW'>

        • <legend id='cAlhW'><style id='cAlhW'><dir id='cAlhW'><q id='cAlhW'></q></dir></style></legend>
          <tfoot id='cAlhW'></tfoot>

              1. <i id='cAlhW'><tr id='cAlhW'><dt id='cAlhW'><q id='cAlhW'><span id='cAlhW'><b id='cAlhW'><form id='cAlhW'><ins id='cAlhW'></ins><ul id='cAlhW'></ul><sub id='cAlhW'></sub></form><legend id='cAlhW'></legend><bdo id='cAlhW'><pre id='cAlhW'><center id='cAlhW'></center></pre></bdo></b><th id='cAlhW'></th></span></q></dt></tr></i><div id='cAlhW'><tfoot id='cAlhW'></tfoot><dl id='cAlhW'><fieldset id='cAlhW'></fieldset></dl></div>

                    <tbody id='cAlhW'></tbody>
                • 本文介绍了C++中vector是如何实现的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在考虑如何从头开始实现 std::vector.

                  I am thinking of how I can implement std::vector from the ground up.

                  它如何调整向量的大小?

                  How does it resize the vector?

                  realloc 似乎只适用于普通的旧结构,还是我错了?

                  realloc only seems to work for plain old stucts, or am I wrong?

                  推荐答案

                  它是一个简单的模板化类,它包装了一个原生数组.它使用malloc/realloc.相反,它使用传递的分配器(默认为 std::allocator).

                  it is a simple templated class which wraps a native array. It does not use malloc/realloc. Instead, it uses the passed allocator (which by default is std::allocator).

                  调整大小是通过分配一个新数组并从旧数组复制构造新数组中的每个元素来完成的(这样对于非 POD 对象是安全的).为了避免频繁分配,它们通常遵循非线性增长模式.

                  Resizing is done by allocating a new array and copy constructing each element in the new array from the old one (this way it is safe for non-POD objects). To avoid frequent allocations, often they follow a non-linear growth pattern.

                  更新:在 C++11 中,如果存储类型可能,元素将被移动而不是复制构造.

                  UPDATE: in C++11, the elements will be moved instead of copy constructed if it is possible for the stored type.

                  除此之外,它还需要存储当前的大小"和容量".大小是向量中实际有多少元素.容量是向量中可以的数量.

                  In addition to this, it will need to store the current "size" and "capacity". Size is how many elements are actually in the vector. Capacity is how many could be in the vector.

                  所以作为起点,向量需要看起来像这样:

                  So as a starting point a vector will need to look somewhat like this:

                  template <class T, class A = std::allocator<T> >
                  class vector {
                  public:
                      // public member functions
                  private:
                      T*                    data_;
                      typename A::size_type capacity_;
                      typename A::size_type size_;
                      A                     allocator_;
                  };
                  

                  另一个常见的实现是存储指向数组不同部分的指针.这稍微降低了 end()(不再需要添加)的成本,但代价是稍微昂贵的 size() 调用(现在需要一个减法).在这种情况下,它可能如下所示:

                  The other common implementation is to store pointers to the different parts of the array. This cheapens the cost of end() (which no longer needs an addition) ever so slightly at the expense of a marginally more expensive size() call (which now needs a subtraction). In which case it could look like this:

                  template <class T, class A = std::allocator<T> >
                  class vector {
                  public:
                      // public member functions
                  private:
                      T* data_;         // points to first element
                      T* end_capacity_; // points to one past internal storage
                      T* end_;          // points to one past last element
                      A  allocator_;
                  };
                  

                  我相信 gcc 的 libstdc++ 使用后一种方法,但这两种方法同样有效且一致.

                  I believe gcc's libstdc++ uses the latter approach, but both approaches are equally valid and conforming.

                  注意:这忽略了一个常见的优化,其中空基类优化用于分配器.我认为这是实现细节的质量,而不是正确性的问题.

                  NOTE: This is ignoring a common optimization where the empty base class optimization is used for the allocator. I think that is a quality of implementation detail, and not a matter of correctness.

                  这篇关于C++中vector是如何实现的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 中的 set 和 unordered_set 有什么区别? 下一篇:C++ 是否存在循环列表的标准实现?

                  相关文章

                  最新文章

                  <legend id='LpvXb'><style id='LpvXb'><dir id='LpvXb'><q id='LpvXb'></q></dir></style></legend>
                  • <bdo id='LpvXb'></bdo><ul id='LpvXb'></ul>
                • <i id='LpvXb'><tr id='LpvXb'><dt id='LpvXb'><q id='LpvXb'><span id='LpvXb'><b id='LpvXb'><form id='LpvXb'><ins id='LpvXb'></ins><ul id='LpvXb'></ul><sub id='LpvXb'></sub></form><legend id='LpvXb'></legend><bdo id='LpvXb'><pre id='LpvXb'><center id='LpvXb'></center></pre></bdo></b><th id='LpvXb'></th></span></q></dt></tr></i><div id='LpvXb'><tfoot id='LpvXb'></tfoot><dl id='LpvXb'><fieldset id='LpvXb'></fieldset></dl></div>
                  1. <small id='LpvXb'></small><noframes id='LpvXb'>

                    <tfoot id='LpvXb'></tfoot>