|
So how do you use this little puppy? Its hardly rocket science. Here's some example code to show you it in action.
int main()
{
// create a circular buffer of ints with capacity 20
circular_buffer cbuf(20);
// Add 15 elements
for (int n = 0; n < 15; ++n) cbuf.push_back(n*2);
// Copy them to cout
std::copy(cbuf.begin(), cbuf.end(), std::ostream_iterator(std::cout));
// Add 10 more elements
for (int n = 0; n < 10; ++n) cbuf.push_back(n*2);
// Note now that 5 elements will have been consumed from the back of the buffer,
// replaced by new data!
// Print and consume each element
while (!cbuf.empty())
{
std::cout << "element:" << cbuf.front();
pop_front();
}
// done
return 0;
}
|