Does the standard guarantee, that std::string::res

2020-04-06 08:07发布

I need to frequently make a string empty and then append some chars into it. std::string::clear() may realloc Does std::string::resize(0) do realloc? The standard's words didn't garentee any about it.

1条回答
▲ chillily
2楼-- · 2020-04-06 08:54

I think the best possible answer to this is the "Notes" section at http://en.cppreference.com/w/cpp/string/basic_string/clear.

Unlike for std::vector::clear, the C++ standard does not explicitly require that capacity is unchanged by this function, but existing implementations do not change capacity.

And if the capacity is unchanged, that would almost certainly mean that no allocation or freeing functions are called. That's probably the best you can do, short of looking at each and every implementation you care about.

查看更多
登录 后发表回答