I know this is simple, so I apologize in advance.
I am segfaulting when trying to access a vector by index. For example…
vector<float> some_vec;
int i = 0;
for (some iterator loop here)
{
//snip
some_vec[i] = some_float;
i++;
}
What am I doing wrong?
After
your vector is empty. You must not access any element in it then, because there isn’t any.
If you want to put values into it, you need to append them to the vector using
push_back()Alternatively, if you know the size in advance, and if the construction of dummy values in the vector is cheap (as it is for
floatand other built-ins), you canresize()the vector in advanceor create it with the right amount of elements
Given either of the two above, you can then access elements
0..41in the vector.