The vector::capacity() function is a built-in function which 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 to or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion. The 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.
Parameters: The function does not accept any parameters.
Return Value: The function returns the size of the storage space currently allocated for the vector, expressed in terms of elements.
Below programs illustrate the above functions:
The size of vector is 10 The maximum capacity is 16
The size of vector is 100 The maximum capacity is 128
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- vector emplace() function in C++ STL
- vector data() function in C++ STL
- vector max_size() function in C++ STL
- Passing vector to a function in C++
- vector shrink_to_fit() function in C++ STL
- vector insert() function in C++ STL
- vector rbegin() and rend() function in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::front() and vector::back() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- vector::at() and vector::swap() in C++ STL
- Vector in C++ STL
- vector :: assign() in C++ STL
- Deque vs Vector in C++ STL
- How to reverse a Vector using STL in C++?
- vector::operator= and vector::operator[ ] in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.