advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 4/5 | Rate this item | 1 user has rated this item.
Expertise: Intermediate
Language: C++
December 17, 1998
Checking a Vector's Capacity
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.
Danny Kalev
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.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement