Replace an element into a specific position of a vector

后端 未结 3 1615
臣服心动
臣服心动 2020-12-07 13:00

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
v         


        
3条回答
  •  清歌不尽
    2020-12-07 13:44

    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])
    

提交回复
热议问题