Checking a Vector’s Capacity

Checking a Vector’s Capacity

The member function capacity() returns the total number of elements that a vector can hold without requiring reallocation:

 #include #include using namespace std;void main() {     vector  vi; vi.reserve(10);  //make room for at least 10 more int's cout

You can use capacity() before inserting new elements to check whether existing iterators may become invalidated as a result.


Share the Post: