std::vector::assign | | |
what happens memory wise with this scenerio:
std::vector<int> vecInt(20, 999);
VecInt.assign(20, 0);
do all the 999 values get overwritten or does the vector reassign the
memory lso?
thanks | | | | re: std::vector::assign
scooter wrote:[color=blue]
> what happens memory wise with this scenerio:
>
> std::vector<int> vecInt(20, 999);
>
> VecInt.assign(20, 0);[/color]
You have the parameters backwards.
[color=blue]
> do all the 999 values get overwritten or does the vector reassign the
> memory lso?[/color]
Do you want the memory to be freed and reallocated?
If so, I know that
std::vector <int> vecInt (999, 20);
{
std::vector <int> temp ();
temp.swap (vecInt)
}
will do it. IIRC, vector::assign works identically to
constructing a new vector and using operator=, and in
that case (again IIRC) the memory is not guaranteed to
be released and reallocated.
Regards,
Buster. | | | | re: std::vector::assign
Buster Copley wrote:[color=blue]
> scooter wrote:
>[color=green]
>> what happens memory wise with this scenerio:
>>
>> std::vector<int> vecInt(20, 999);
>>
>> VecInt.assign(20, 0);[/color]
>
>
> You have the parameters backwards.[/color]
I'm sorry, I misunderstood the next sentence. You're fine.
[color=blue][color=green]
>> do all the 999 values get overwritten or does the vector reassign the
>> memory lso?[/color][/color]
I think the 999s get overwritten.
[color=blue]
> Regards,
> Buster.[/color] | | | | re: std::vector::assign
On Wed, 17 Sep 2003 20:18:39 +0100, Buster Copley <buster@none.com>
wrote:
[color=blue]
>scooter wrote:[color=green]
>> what happens memory wise with this scenerio:
>>
>> std::vector<int> vecInt(20, 999);
>>
>> VecInt.assign(20, 0);[/color]
>
>You have the parameters backwards.
>[color=green]
>> do all the 999 values get overwritten or does the vector reassign the
>> memory lso?[/color]
>
>Do you want the memory to be freed and reallocated?
>If so, I know that
>
>std::vector <int> vecInt (999, 20);
>
>{
> std::vector <int> temp ();[/color]
The above declares a function called temp, which isn't what you
wanted.
[color=blue]
> temp.swap (vecInt)
>}[/color]
The canonical way to do this (which very few people seem to get right
for some reason), is:
std::vector<int>().swap(vecInt);
Tom | | | | re: std::vector::assign
"tom_usenet" <tom_usenet@hotmail.com> wrote>
[color=blue][color=green]
> >Do you want the memory to be freed and reallocated?
> >If so, I know that
> >
> >std::vector <int> vecInt (999, 20);
> >
> >{
> > std::vector <int> temp ();[/color]
>
> The above declares a function called temp, which isn't what you
> wanted.[/color]
Yes, silly of me. Thanks.
[color=blue][color=green]
> > temp.swap (vecInt)
> >}[/color]
>
> The canonical way to do this (which very few people seem to get right
> for some reason), is:
>
> std::vector<int>().swap(vecInt);[/color]
I have a question about that. I thought I read somewhere that a
temporary is not modifiable. Isn't the result of the expression
'std::vector <int> ()' (when it is an expression...) a temporary?
So what am I missing?
Regards,
Buster. | | | | re: std::vector::assign
On Thu, 18 Sep 2003 14:53:41 +0100, "Buster" <noone@nowhere.com>
wrote:
[color=blue]
>I have a question about that. I thought I read somewhere that a
>temporary is not modifiable. Isn't the result of the expression
>'std::vector <int> ()' (when it is an expression...) a temporary?
>So what am I missing?[/color]
Temporaries are modifiable, it's just that you can't bind them to
non-const references. e.g. this is illegal:
vecInt.swap(std::vector<int>());
Here a temporary is passed as the argument to swap. This argument is a
non-const reference, so the code is illegal.
So, to summarize, you can call member functions (const or non-const)
on non-const temporaries, but you can't bind them to non-const
references. This is why the standard swap technique works, but some
alternatives don't.
Tom |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|