Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing an element in a vector

33 views
Skip to first unread message

jacobnavia

unread,
Apr 12, 2020, 4:15:23 AM4/12/20
to
How do you replace an element at, say, position 4?

You have to erase the element and then insert a new one?

Thanks in advance

Barry Schwarz

unread,
Apr 12, 2020, 4:36:46 AM4/12/20
to
On Sun, 12 Apr 2020 10:15:11 +0200, jacobnavia
<ja...@jacob.remcomp.fr> wrote:

>How do you replace an element at, say, position 4?

Does
vec[4] = value;
not do what you want?


>You have to erase the element and then insert a new one?

Why would you think that?

--
Remove del for email

Juha Nieminen

unread,
Apr 12, 2020, 5:21:59 AM4/12/20
to
Barry Schwarz <schw...@delq.com> wrote:
> On Sun, 12 Apr 2020 10:15:11 +0200, jacobnavia
> <ja...@jacob.remcomp.fr> wrote:
>
>>How do you replace an element at, say, position 4?
>
> Does
> vec[4] = value;
> not do what you want?

More like vec[3] = value;

(Although, admittedly, "position 4" is ambiguous, as it
could mean either one.)

jacobnavia

unread,
Apr 12, 2020, 5:43:42 AM4/12/20
to
OK, I suppose that any destructors for the old element are called of course.

Thanks for your answers and sorry for this stupid question...

Jorgen Grahn

unread,
Apr 12, 2020, 7:47:55 AM4/12/20
to
On Sun, 2020-04-12, jacobnavia wrote:
> Le 12/04/2020 à 11:21, Juha Nieminen a écrit :
>> Barry Schwarz <schw...@delq.com> wrote:
>>> On Sun, 12 Apr 2020 10:15:11 +0200, jacobnavia
>>> <ja...@jacob.remcomp.fr> wrote:
>>>
>>>> How do you replace an element at, say, position 4?
>>>
>>> Does
>>> vec[4] = value;
>>> not do what you want?
>>
>> More like vec[3] = value;
>>
>> (Although, admittedly, "position 4" is ambiguous, as it
>> could mean either one.)
>
> OK, I suppose that any destructors for the old element are called of
> course.

To be pedantic, no object is destroyed: operator= () is invoked for
the element. But the bottom line is it's a safe thing to do, just
like it would be e.g. with an array of int in C.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Juha Nieminen

unread,
Apr 12, 2020, 11:22:14 AM4/12/20
to
jacobnavia <ja...@jacob.remcomp.fr> wrote:
> OK, I suppose that any destructors for the old element are called of course.

Whatever the assignment operator for the type in question does is done.
In practice it just works.

Real Troll

unread,
Apr 12, 2020, 2:39:04 PM4/12/20
to
On 12/04/2020 09:15, jacobnavia wrote:
> How do you replace an element at, say, position 4?
>
> You have to erase the element and then insert a new one?
>
>

Something like this would work:

> myvector.erase(myvector.begin() + 3);
>
> myvector.insert(myvector.begin() + 3, 60);

The first line erases the item;  The second line inserts the number 60
in the 4th place.


Manfred

unread,
Apr 12, 2020, 7:44:45 PM4/12/20
to
The requirement is that the assignment operator be correctly defined.
This is obviously true for built-in and std types.
For user defined types the programmer has to take care of it.

James Kuyper

unread,
Apr 12, 2020, 10:35:14 PM4/12/20
to
On Sunday, April 12, 2020 at 5:43:42 AM UTC-4, jacobnavia wrote:
> Le 12/04/2020 à 11:21, Juha Nieminen a écrit :
> > Barry Schwarz <schw...@delq.com> wrote:
> >> On Sun, 12 Apr 2020 10:15:11 +0200, jacobnavia
> >> <ja...@jacob.remcomp.fr> wrote:
> >>
> >>> How do you replace an element at, say, position 4?
> >>
> >> Does
> >> vec[4] = value;
> >> not do what you want?
> >
> > More like vec[3] = value;
> >
> > (Although, admittedly, "position 4" is ambiguous, as it
> > could mean either one.)
> >
>
> OK, I suppose that any destructors for the old element are called of course.

Assuming that vec[3] and 4 have the same type (int), then destructors
play no role in the matter, it simply calls the copy assignment
operator. If a class type T were involved, then other possibilities come
up depending upon which operator overloads are, or are not, defined.

Juha Nieminen

unread,
Apr 13, 2020, 1:57:46 AM4/13/20
to
This is actually an incorrect answer to the question that was
presented.

The question asked if the element has to be removed and then the
new one inserted. And the answer is no, that's not needed. You can
simply assign the new value to the element in question.
0 new messages