0

Vector capacity and size

In sololearn whenever I use the vector's capacity function it always shows me the size of the array but shouldn't it show that how much that particular array can accommodate before asking for reallocating memory? P.S. ignore the commented code https://code.sololearn.com/cBiF9vHUQm47/?ref=app

31st May 2019, 12:22 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
2 Answers
+ 3
"size_type capacity() const noexcept; Return size of allocated storage capacity Returns the size of the storage space currently allocated for the vector, expressed in terms of elements. This capacity is not necessarily equal to the vector size. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion. Notice that this capacity does not suppose a limit on the size of the vector. When this capacity is exhausted and more is needed, it is automatically expanded by the container (reallocating it storage space). The theoretical limit on the size of a vector is given by member max_size. The capacity of a vector can be explicitly altered by calling member vector::reserve." http://www.cplusplus.com/reference/vector/vector/capacity/
31st May 2019, 12:35 PM
Anna
Anna - avatar
+ 2
thanks Anna
31st May 2019, 12:44 PM
Saurabh Tiwari
Saurabh Tiwari - avatar