Если вы хотите что-то повторно использовать, вы можете написать пару вспомогательных функций:
// Non-mutating version of string::resize
std::string resize_copy(std::string const & str, std::size_t new_sz)
{
std::string copy = str;
copy.resize(new_sz);
return copy;
}
void resize_to(std::string const & str, std::string & dest)
{
dest = resize_copy(str, dest.size());
}
int main()
{
std::string a = "this is a string";
std::string b(4, ' ');
std::string c(20, ' ');
resize_to(a, b);
resize_to(a, c);
std::cout << b << "|\n" << c << "|\n";
}
Это печатает:
this|
this is a string |