Reputation: 21748
When adding elements to boost/circular_buffer.hpp with push_back, is it possible to assume that the larger index means the later inserted element?
More precisely, in my case, to make the recent insertions more significant:
double weighted_running_average(const boost::circular_buffer<double> &x)
{
return (x[0] + 2*x[1] + 3*x[2]) / 6;
}
or would this result a non-predictable order of importance?
Upvotes: 0
Views: 2833
Reputation: 37549
Yes, circular_buffer
is a sequence container so when you use push_back
new element will be always accessible at highest index and the order of previously inserted elements will not change. If the buffer is full then first element will be erased and index of all existing elements reduced by 1.
Example (online compiler)
#include <boost/circular_buffer.hpp>
#include <iostream>
int main()
{
::boost::circular_buffer<int> x;
x.set_capacity(2);
x.push_back(0);
x.push_back(1);
std::cout << x[0]; // 0
std::cout << x[1]; // 1
x.push_back(2);
std::cout << x[0]; // 1
std::cout << x[1]; // 2
return 0;
}
Upvotes: 4