You want to keep the buffer size by overwriting the old elements. Just rewrite the old ones over time. If you want to deal with the case when nItems <limit, then you will need to deal with this, this is just a simple example of using modulo to insert into a fixed-size buffer.
std::vector<int> data(10);
for (int i = 0 ; i < 100 ; ++i)
{
data[i%10] = i;
}
for (std::vector<int>::const_iterator it = data.begin() ; it !=data.end(); ++it)
{
std::cout << *it << std::endl;
}
This paste method will save the last 10 items in the buffer.
source
share