This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]