Vector capacity and size | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite

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

31st May 2019, 12:22 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
2 Réponses
+ 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."
31st May 2019, 12:35 PM
Anna - avatar
+ 2
thanks Anna
31st May 2019, 12:44 PM
Saurabh Tiwari
Saurabh Tiwari - avatar