The member function capacity() returns the total number of elements that a vector can hold without requiring reallocation:
#include <iostream>
#include <vector>
using namespace std;
void main()
{
vector <int> vi;
vi.reserve(10); //make room for at least 10 more int's
cout<< vi.size(); //output 0; no elements are stored in vi
cout<< vi.capacity(); //output 10; vi has enough room for 10 elements
}
You can use capacity() before inserting new elements to check whether existing iterators may become invalidated as a result.
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible.
Submit your tip here.