string::capacity
Returns the size of the allocated storage space in the
string object.
Notice that the capacity is not necessarily equal to the number of
characters that conform the content of the string (this can be obtained
with members
size or
length), but the capacity of the allocated space, which is either equal or greater than this content size.
Notice also that this capacity does not suppose a limit to the length
of the string. If more space is required to accomodate content in the
string object, the capacity is automatically expanded, or can even be explicitly modified by calling member
reserve.
The real limit on the size a
string object can reach is returned by member
max_size.
size_t capacity ( ) const;
1 // comparing size, length, capacity and max_size
2 #include <iostream>
3 #include <string>
4 using namespace std;
5
6 int main ()
7 {
8 string str ("Test string");
9 cout << "size: " << str.size() << "\n";
10 cout << "length: " << str.length() << "\n";
11 cout << "capacity: " << str.capacity() << "\n";
12 cout << "max_size: " << str.max_size() << "\n";
13 return 0;
14 }