• <tfoot id='ZY8pN'></tfoot>
      <bdo id='ZY8pN'></bdo><ul id='ZY8pN'></ul>

  • <legend id='ZY8pN'><style id='ZY8pN'><dir id='ZY8pN'><q id='ZY8pN'></q></dir></style></legend>

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

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

        push_back 是如何在 STL 向量中实现的?

        时间:2023-09-15

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

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

              • <tfoot id='CR1xp'></tfoot>
                  <bdo id='CR1xp'></bdo><ul id='CR1xp'></ul>
                  本文介绍了push_back 是如何在 STL 向量中实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在一次采访中被问到这个问题.

                  I was asked this question in an interview.

                  我回答的点是这样的

                  1) 指向当前位置的索引;

                  1) an index pointing to the current position;

                  2) 必要时调整大小.

                  2) resize if neccessary.

                  谁能说得详细点?

                  推荐答案

                  STL vector 有一个 size(当前存储元素的数量)和一个 capacity(当前分配的存储空间).

                  An STL vector has a size (current number of stored elements) and a capacity (currently allocated storage space).

                  • 如果 size <容量push_back 只是将新元素放在最后,并将size 增加 1.
                  • 如果 size == capacitypush_back 之前,一个新的更大的数组被分配(两倍的大小是常见的,但这是依赖于实现的afaik),所有复制当前数据(包括新元素),并释放旧分配的空间.如果分配失败,这可能会引发异常.
                  • If size < capacity, a push_back simply puts the new element at the end and increments the size by 1.
                  • If size == capacity before the push_back, a new, larger array is allocated (twice the size is common, but this is implementation-dependent afaik), all of the current data is copied over (including the new element), and the old allocated space is freed. This may throw an exception if the allocation fails.

                  操作的复杂性摊销 O(1),这意味着在导致调整大小的push_back期间,它不会是一个恒定时间的操作(但总的来说,在许多操作中,它是).

                  The complexity of the operation is amortized O(1), which means during a push_back that causes a resize, it won't be a constant-time operation (but in general over many operations, it is).

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

                  上一篇:向量<无符号字符>vs 二进制数据的字符串 下一篇:std::lower_bound 对于 std::vector 比 std::map::find 慢

                  相关文章

                  最新文章

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

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

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

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