[v3] basic_string::reserve() shrink-to-fit?

Jonathan Wakely cow@compsoc.man.ac.uk
Thu Dec 11 17:20:00 GMT 2003


On Thu, Dec 11, 2003 at 09:08:33AM -0800, Nathan Myers wrote:

> > ie. reserve() doesn't seem to shrink the string capacity to fit.
> 
> I'm afraid that's what's supposed to happen, in basic_string<> and 
> in vector<>.  The idiom for shrinking to fit is to use swap().
> Minimally,
> 
>   std::swap(std::string(str.data(), str.size()), str);
> 
> This might be a good thing to put in the FAQ.

I'll prepare a FAQ entry tonight if noone beats me too it.

For the purposes of the FAQ, is the above form preferred to either

  std::string(str.data(), str.size()).swap(str);

which the first form will defer to, or

  std::swap(std::string(str.begin(), str.end()), str);

which doesn't use std::string::data() and so works for vectors too?

jon

-- 
"A foolish consistency is the hobgoblin of little minds..."
	- Ralph Waldo Emerson



More information about the Libstdc++ mailing list