This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [v3] basic_string::reserve() shrink-to-fit?
- From: Nathan Myers <ncm at cantrip dot org>
- To: libstdc++ at gcc dot gnu dot org
- Date: Thu, 11 Dec 2003 09:08:33 -0800
- Subject: Re: [v3] basic_string::reserve() shrink-to-fit?
- References: <3FD88478.3070309@eso.org>
On Thu, Dec 11, 2003 at 03:51:36PM +0100, Neil Ferguson wrote:
>
> Initial capacity: 0
> After 6 char assign: 6
> After reserve(1000000): 1003491
> After reserve(): 1003491
>
> 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.
When this was raised as a Defect Report, the committee recognized
it as clunky, but not, technically, a Defect, and thus beyond their
charter to change at that time. For the next standard they have
more latitude.
Nathan Myers
ncm-nospam@cantrip.org