Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) boost::circular_buffer

 

boost::circular_buffer is an 'STL compliant container also known as ring or cyclic buffer' [1].

 

#include <algorithm>
#include <cassert>
#include <iostream>
#include <boost/circular_buffer.hpp>

int main()
{
  const int size = 3;
  boost::circular_buffer<int> v(size);

  v.push_back(1);
  v.push_back(4);
  v.push_back(9);

  assert(v.size() == 3);
  assert(v[0] == 1);
  assert(v[1] == 4);
  assert(v[2] == 9);

  v.push_back(16);

  assert(v.size() == 3);
  assert(v[0] == 4);
  assert(v[1] == 9);
  assert(v[2] == 16);

  v.push_back(25);

  assert(v.size() == 3);
  assert(v[0] == 9);
  assert(v[1] == 16);
  assert(v[2] == 25);

  //Screen output: 9 16 25 9 16 25 9 16 25 9
  for (int i=0; i!=10; ++i)
  {
    std::cout << *v.begin() << ' ';
    v.rotate(v.begin() + 1);
  }
}

 

 

 

 

 

External links

 

 

 

 

 

 

References

 

  1. Boost page listing all its libraries

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict