boost 循环缓冲区

时间:2023-03-08 22:51:19
boost 循环缓冲区

boost 循环缓冲区

  1. #include <boost/circular_buffer.hpp>
  2. int _tmain(int argc, _TCHAR* argv[])
  3. {
  4. boost::circular_buffer<int> cb(3);
  5. // Insert some elements into the buffer.
  6. cb.push_back(1);
  7. cb.push_back(2);
  8. cb.push_back(3);
  9. int a = cb[0];  // a == 1
  10. int b = cb[1];  // b == 2
  11. int c = cb[2];  // c == 3
  12. // The buffer is full now, pushing subsequent
  13. // elements will overwrite the front-most elements.
  14. cb.push_back(4);  // Overwrite 1 with 4.
  15. cb.push_back(5);  // Overwrite 2 with 5.
  16. // The buffer now contains 3, 4 and 5.
  17. a = cb[0];  // a == 3
  18. b = cb[1];  // b == 4
  19. c = cb[2];  // c == 5
  20. // Elements can be popped from either the front or the back.
  21. cb.pop_back();  // 5 is removed.
  22. cb.pop_front(); // 3 is removed.
  23. int d = cb[0];  // d == 4
  24. return 0;
  25. }