Moeb Moeb - 1 year ago 87
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

Answer Source

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


l[4] = -1;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download