dimanche 28 octobre 2018

shrinking stl string to a capacity of less than 15 characters

Consider std::string, and the capacity

 std::string aString = "12345678901234567890";
 std::cout << aString.capacity() << std::endl; // capacity is 20
 aString.clear();
 std::cout << "size: " << aString.size() << std::endl;
 std::cout << aString.capacity() << std::endl; // capacity is 20
 aString.shrink_to_fit();
 std::cout << aString.capacity() << std::endl; // capacity is 15?
 std::string newString;
 std::cout << newString.capacity() << std::endl; // capacity is 15?

Is 15 characters the minimum capacity? Is there any way to shrink it to the actual size of the string?

Aucun commentaire:

Enregistrer un commentaire