• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_sum_example_1
9 
10 /*`This example shows several functions, including summing all valid values.
11 */
12 
13    #include <boost/circular_buffer.hpp>
14    #include <numeric>
15    #include <assert.h>
16 
main(int,char * [])17    int main(int /*argc*/, char* /*argv*/[])
18    {
19       // Create a circular buffer of capacity 3.
20       boost::circular_buffer<int> cb(3);
21       assert(cb.capacity() == 3);
22       // Check is empty.
23       assert(cb.size() == 0);
24       assert(cb.empty());
25 
26       // Insert some elements into the circular buffer.
27       cb.push_back(1);
28       cb.push_back(2);
29 
30       // Assertions to check push_backs have expected effect.
31       assert(cb[0] == 1);
32       assert(cb[1] == 2);
33       assert(!cb.full());
34       assert(cb.size() == 2);
35       assert(cb.capacity() == 3);
36 
37       // Insert some other elements.
38       cb.push_back(3);
39       cb.push_back(4);
40 
41       // Evaluate the sum of all elements.
42       int sum = std::accumulate(cb.begin(), cb.end(), 0);
43 
44       // Assertions to check state.
45       assert(sum == 9);
46       assert(cb[0] == 2);
47       assert(cb[1] == 3);
48       assert(cb[2] == 4);
49       assert(*cb.begin() == 2);
50       assert(cb.front() == 2);
51       assert(cb.back() == 4);
52       assert(cb.full());
53       assert(cb.size() == 3);
54       assert(cb.capacity() == 3);
55 
56       return 0;
57    }
58 
59    //] [/circular_buffer_sum_example_1]
60 
61 
62    /*
63    There is no output from this example.
64    */
65