How can I insert element to vector instead the element already exists in the vector?

When you do v1.insert(v1.begin(), 7), you are just inserting an element at the beginning of v1. You don’t expect that, after inserting an element, the number of elements stays the same.

I think what you want is simply v1[0] = 7.

Leave a Comment