#include#include int main() { std::string str(20, ' '); std::cout << "Initial capacity: " << str.capacity() << std::endl; return 0; }
#include#include int main() { std::string str(10, 'a'); std::cout << "Initial capacity: " << str.capacity() << std::endl; str.reserve(50); std::cout << "New capacity: " << str.capacity() << std::endl; return 0; }
Initial capacity: 10 New capacity: 50In this example, a std::string is created with 10 'a' characters as its initial content. The capacity() member function is called to retrieve the initial capacity, which is 10. The reserve() member function is then called to increase the string's capacity to 50. Finally, the capacity() member function is called again to retrieve the new capacity, which is 50. These examples use the std namespace provided by the C++ standard library.