std::string str1 = "Hello"; std::string str2 = "World!"; str1.resize(8); // Resizes str1 to size 8 by appending null characters str2.resize(4); // Trims str2 to size 4 std::cout << str1 << " " << str2 << std::endl; // Output: "Hello\0\0\0 World"
std::string str = "ABCD"; str.resize(10, 'X'); // Resizes str to size 10 by appending 'X' std::cout << str << std::endl; // Output: "ABCDXXXXXX"Here, `str` is resized to a size of 10 by appending the character 'X' until it reaches the new size. The `resize()` function is part of the C++ Standard Library.