I’m not sure I need a vector for this but I don’t think I can use an array because I don’t have a fixed size.
I have a Singleton class Song which has a member vector<float> samples; and a method getSamples(int numberOfSamples) When this method is called I want to make the vector the correct size (based on numberOfSamples) and than update some of it’s values possibly multiple times.
vector<float> &Song::getSamples(int numberOfSamples){
for(int i = 0; i < numberOfFrames; i++)
this->samples.push_back(0);
}
So for example how do I change the 3rd value to 3 instead of 0? This would be a lot easier with an array, doing something like samples[2] = 3; but the problem is that I don’t know the length of the vector/array before the getSamples method is called. And I don’t think constantly iterating is a good idea because the numberOfSamples can be quite big and it’s possible I need to update each value up to five times.
You can simply resize the vector, which will value initialise (the default is
T()which will be0.0forfloat) all new values:To set a value for a given index, you can use the subscript operator just like for a plain C-array: