[c++] Replace an element into a specific position of a vector

I want to replace an element into a specific position of a vector, can I just use an assignment:

// vec1 and 2 have the same length & filled in somehow

vec1[i] = vec2[i] // insert vec2[i] at position i of vec1

or I have to use insert():

vector<sometype>::iterator iterator = vec1.begin();

vec1.insert(iterator+(i+1), vec2[i]);

This question is related to c++ visual-c++ vector stl insert

The answer is

vec1[i] = vec2[i]

will set the value of vec1[i] to the value of vec2[i]. Nothing is inserted. Your second approach is almost correct. Instead of +i+1 you need just +i

v1.insert(v1.begin()+i, v2[i])

Similar questions with c++ tag:

Similar questions with visual-c++ tag:

Similar questions with vector tag:

Similar questions with stl tag:

Similar questions with insert tag: