Moeb Moeb - 6 months ago 33
C++ Question

How to change a particular element of a C++ STL vector

vector<int> l;
for(int i=1;i<=10;i++){

Now, for example, how do I change the
5th element
of the vector to

I tried
l.assign(4, -1);

It is not behaving as expected. None of the other vector methods seem to fit.

I have used vector as I need random access functionality in my code (using


at and operator[] both return a reference to the indexed element, so you can simply use: = -1;


l[4] = -1;