1 // Copyright 2003-2008 Jan Gaspar. 2 // Copyright 2013 Paul A. Bristow. Added some Quickbook snippet markers. 3 4 // Distributed under the Boost Software License, Version 1.0. 5 // (See the accompanying file LICENSE_1_0.txt 6 // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.) 7 8 //[circular_buffer_example_1 9 /*`For all examples, we need this include: 10 */ 11 12 #include <boost/circular_buffer.hpp> 13 14 //] [/circular_buffer_example_1] 15 main()16 int main() 17 { 18 19 //[circular_buffer_example_2 20 // Create a circular buffer with a capacity for 3 integers. 21 boost::circular_buffer<int> cb(3); 22 23 // Insert three elements into the buffer. 24 cb.push_back(1); 25 cb.push_back(2); 26 cb.push_back(3); 27 28 int a = cb[0]; // a == 1 29 int b = cb[1]; // b == 2 30 int c = cb[2]; // c == 3 31 32 // The buffer is full now, so pushing subsequent 33 // elements will overwrite the front-most elements. 34 35 cb.push_back(4); // Overwrite 1 with 4. 36 cb.push_back(5); // Overwrite 2 with 5. 37 38 // The buffer now contains 3, 4 and 5. 39 a = cb[0]; // a == 3 40 b = cb[1]; // b == 4 41 c = cb[2]; // c == 5 42 43 // Elements can be popped from either the front or the back. 44 cb.pop_back(); // 5 is removed. 45 cb.pop_front(); // 3 is removed. 46 47 // Leaving only one element with value = 4. 48 int d = cb[0]; // d == 4 49 50 //] [/circular_buffer_example_2] 51 return 0; 52 } 53 54 /* 55 56 //[circular_buffer_example_output 57 58 There is no output from this example. 59 60 //] [/circular_buffer_example_output] 61 62 */ 63 64