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

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

      1. <small id='RZF1G'></small><noframes id='RZF1G'>

        <legend id='RZF1G'><style id='RZF1G'><dir id='RZF1G'><q id='RZF1G'></q></dir></style></legend>
        <tfoot id='RZF1G'></tfoot>

        C++中vector是如何实现的

        How is vector implemented in C++(C++中vector是如何实现的)

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

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

                  <tfoot id='GWLVS'></tfoot>
                1. 本文介绍了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是如何实现的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                2. <small id='ZX95B'></small><noframes id='ZX95B'>

                  1. <tfoot id='ZX95B'></tfoot>

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

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