Clearing vectors every cycle

Hi,
I have several sets of real time stream of data (float) that I need to manage. (face tracking)
each set counts a variable number of features (e.g. set “Brows” includes 5 features "BrowsUpL, BrowsDownL, BrowsUpR, etc…)

I am using a vector to store the stream of data for each set. By doing this however my vector keeps on filling with floats of the individual feature and I need to cycle it according to the number of feature for each set. in other words, every n inputs the vector should be cleared and start again

(I hope it’s clear so far)

My feeling is that what I am doing is too mechanic and I am wondering if there is a more efficient way to do so.

Here is the code and I hope this makes sense for someone?

Cheers

if(faceShift.getFound()) {
        for (int i =0; i<browsData.size(); i++) {
            if (browsData[i]>=0) {
              ofDrawBitmapString(browsName[i] + " " + ofToString(browsData[i]), 10, i*20+80);
                
            }
        }
    }
    
    //clearing the vectors every cycle //browsFeatCount is the number of features in the set "Brows"
    
    if (browsData.size()%browsFeatCount==0) {
        browsData.clear();
        browsName.clear();
    }

clear() is how you empty out a vector and especially if your vector is all just floats, what you’re doing should work fine. Unless you need it to be size() == 0 right away. By default, std::vector::clear often doesn’t actually zero the amount of storage used by a vector, but just destroys the objects contained inside so the vector has room to store more objects without reallocation. If you need to make sure that size == 0 right away then this does the trick:

 vector<float>().swap(v);

More immediately, I’m wondering if the modulo operator that you’re using (the % operator) is actually getting triggered though.

1 Like

Hi @joshuajnoble,
many thanks for replying.
That code is doing pretty much what I needed. But I was wondering if there might be a more code-efficient way to do the same. Maybe conceptually approaching the same problem from another point of view perhaps using other tools I don’t know yet? I am saying this because I feel the process is a bit too mechanical but it might just be my impression.:slight_smile:
The % seems to be triggered as the cout gives me 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 that is what I was expecting.

Anyway I am now trying to understand what you pointed out.
So, let me try to get it. clear() destroys the objects in the vector but the size of the vector doesn’t go to 0? so is clear() just filling the vector with rubbish data? I am not sure I get the difference between size() == 0 and destroying all the objects in the vector. Are you suggesting to fill the vector with all zeros instead of destroying the data in it?

cheers

It will go to zero eventually but it sometimes leaves memory allocated for more objects and so calls to size() return different values depending on when the memory is actually cleared.

In terms of your approach, it’s fine, vectors are very fast to work with but conceptually if you know that it’s always going to be a certain size then you might want to just use a fixed array, i.e. float v[5];

it makes sense.
I will adjust accordingly then.
Thanks for these hints @joshuajnoble