[Solved] How to access an index in a vector without segfaulting


After

std::vector<float> some_vec;

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

for (some iterator loop here)
{
    //snip
    some_vec.push_back(some_float);
    i++;
}

Alternatively, if you know the size in advance, and if the construction of dummy values in the vector is cheap (as it is for float and other built-ins), you can resize() the vector in advance

some_vec.resize(42);

or create it with the right amount of elements

std::vector<float> some_vec(42);

Given either of the two above, you can then access elements 0..41 in the vector.

solved How to access an index in a vector without segfaulting