In C++, will the vector function push_back increase the size of an empty array?

瘦欲@ 提交于 2019-12-07 08:04:24

问题


Quick question. Let's say I declare a vector of size 20. And then I want to add a few integers to it using push_back.

vector<int> myVector(20);
myVector.push_back(5);
myVector.push_back(14);

Is the capacity of my vector now 22, or is it still 20? Were 5 and 14 added to indices [19] and [20], respectively? Or are they at [0] and [1]?


回答1:


After those statements its capacity is implementation-defined. (Please note that is different from its size.)


vector<int> myVector(20);

This creates a vector filled with twenty 0's. Its size is twenty, exactly, and its capacity is at least twenty. Whether or not it's exactly twenty is implementation-defined; it may have more (probably not, in practice).

myVector.push_back(5);

After this, the twenty-first element of the array is 5, and the capacity is once again implementation-defined. (If the capacity had been exactly twenty before, it is now increased in an unspecified manner.)

myVector.push_back(14);

Likewise, now the twenty-second element of the array is 14, and the capacity is implementation-defined.


If you want to reserve space, but not insert elements, you'd do it like this:

vector<int> myVector;
myVector.reserve(20); // capacity is at least twenty, guaranteed not
                      // to reallocate until after twenty elements are pushed

myVector.push_back(5); // at index zero, capacity at least twenty.
myVector.push_back(14); // at index one, capacity at least twenty.



回答2:


  • size is the number of elements in the vector container.
  • capacity is the size of the allocated storage space
  • push_back effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call.

More info: http://www.cplusplus.com/reference/stl/vector/




回答3:


push_back increases the size of the std::vector and places the new elements at the back of the vector (other containers also have a push_front method to do the same thing at the front as well).

However, there is a difference between the size and the capacity of a vector. The size refers to how many items are actually in the vector right now; the capacity refers to the total number of items that the vector can hold without reallocating memory. It's possible to reserve() memory if you know that you're going to add several elements and don't want to grow the vector piecemeal.




回答4:


As the vector is not empty but has a size of 20 (contains 20 elements) and you push 2 elements to the back, it now contains 22 elements. But the new elements are not placed at indices 19 and 20, but 20 and 21.

If you really want to only reserve enough memory for the vector to hold 20 elements (without actually containing any elements), to prevent costly reallocations, then you should call

std::vector<int> myVector;
myVector.reserve(20);

In this case the vector is still empty, but it has enough memory to add at least 20 elements (using push_back, for example) without needing to reallocate its internal storage. In this case the vector only contains the 2 elements you pushed_back.




回答5:


push_back will increase the capacity of the vector to at least the new size of the vector, but possibly (i.e. probably) somewhat larger.

Because push_back is required to run in O(1) amortized time, each reallocation will be to some multiple of the old capacity. In a typical implementation that multiple is 2.

But the exact capacity increase is not specified. If you require precise control over the capacity, use reserve.

...

Re-reading your question, I am not sure you understand the difference between a vector's size and its capacity. The size is the number of elements. The capacity is the number of elements the vector can hold without performing a reallocation. That is, you can push_back capacity()-size() elements before a reallocation happens.

In your example, 5 and 14 will appear at myVector[20] and myVector[21], respectively.




回答6:


Well, vector has the member function push_back. Other sequences like deque have push_front.

0, 1, 2, ......, final

after addition:

0, 1, 2, ....., final, addition, ...

You may recall that:

capacity() returns the number of elements in the vector sufficient,
without allocating additional memory.
This number can be greater or equal to size.

That is, you can not add at front or the middle, since the vector is specialized for quick access to elements by index. If you want to add at front and back, you can use deque which is similar to vector. If you want to add to front, back and anywhere you can use list. Note that list doesn't provide indexing like deque and vector.

However, a vector is assumed to have more capacity than its actual size. When you add elements to it, it doesn't necessary allocate additional memory. It does only if the capacity equals the size. On many compilers, the new capacity will be double of the old one. After allocating, it copies all elements in the new location. Such behavior may be expensive in terms of memory, however.



来源:https://stackoverflow.com/questions/7774938/in-c-will-the-vector-function-push-back-increase-the-size-of-an-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!