• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Circular_buffer example</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../circular_buffer.html" title="Chapter 8. Boost.Circular Buffer">
10<link rel="prev" href="../circular_buffer.html" title="Chapter 8. Boost.Circular Buffer">
11<link rel="next" href="rationale.html" title="Rationale">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="../circular_buffer.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../circular_buffer.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="rationale.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="circular_buffer.example"></a><a class="link" href="example.html" title="Circular_buffer example">Circular_buffer example</a>
29</h2></div></div></div>
30<p>
31      Here is a simple example to introduce the class <code class="computeroutput"><a class="link" href="../boost/circular_buffer.html" title="Class template circular_buffer">circular_buffer</a></code>.
32    </p>
33<p>
34      For all examples, we need this include:
35    </p>
36<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">circular_buffer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
37</pre>
38<p>
39      This example shows construction, inserting elements, overwriting and popping.
40    </p>
41<pre class="programlisting"><span class="comment">// Create a circular buffer with a capacity for 3 integers.</span>
42<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">cb</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
43
44<span class="comment">// Insert three elements into the buffer.</span>
45<span class="identifier">cb</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>
46<span class="identifier">cb</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
47<span class="identifier">cb</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
48
49<span class="keyword">int</span> <span class="identifier">a</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">0</span><span class="special">];</span>  <span class="comment">// a == 1</span>
50<span class="keyword">int</span> <span class="identifier">b</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">1</span><span class="special">];</span>  <span class="comment">// b == 2</span>
51<span class="keyword">int</span> <span class="identifier">c</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">2</span><span class="special">];</span>  <span class="comment">// c == 3</span>
52
53<span class="comment">// The buffer is full now, so pushing subsequent</span>
54<span class="comment">// elements will overwrite the front-most elements.</span>
55
56<span class="identifier">cb</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">4</span><span class="special">);</span>  <span class="comment">// Overwrite 1 with 4.</span>
57<span class="identifier">cb</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>  <span class="comment">// Overwrite 2 with 5.</span>
58
59<span class="comment">// The buffer now contains 3, 4 and 5.</span>
60<span class="identifier">a</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">0</span><span class="special">];</span>  <span class="comment">// a == 3</span>
61<span class="identifier">b</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">1</span><span class="special">];</span>  <span class="comment">// b == 4</span>
62<span class="identifier">c</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">2</span><span class="special">];</span>  <span class="comment">// c == 5</span>
63
64<span class="comment">// Elements can be popped from either the front or the back.</span>
65<span class="identifier">cb</span><span class="special">.</span><span class="identifier">pop_back</span><span class="special">();</span>  <span class="comment">// 5 is removed.</span>
66<span class="identifier">cb</span><span class="special">.</span><span class="identifier">pop_front</span><span class="special">();</span> <span class="comment">// 3 is removed.</span>
67
68<span class="comment">// Leaving only one element with value = 4.</span>
69<span class="keyword">int</span> <span class="identifier">d</span> <span class="special">=</span> <span class="identifier">cb</span><span class="special">[</span><span class="number">0</span><span class="special">];</span>  <span class="comment">// d == 4</span>
70</pre>
71<p>
72      You can see the full example code at <a href="../../../libs/circular_buffer/example/circular_buffer_example.cpp" target="_top">circular_buffer_example.cpp</a>.
73    </p>
74<p>
75      The full annotated description is in the C++ Reference section.
76    </p>
77</div>
78<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
79<td align="left"></td>
80<td align="right"><div class="copyright-footer">Copyright © 2003-2013 Jan Gaspar<p>
81        Distributed under the Boost Software License, Version 1.0. (See accompanying
82        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
83      </p>
84</div></td>
85</tr></table>
86<hr>
87<div class="spirit-nav">
88<a accesskey="p" href="../circular_buffer.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../circular_buffer.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="rationale.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
89</div>
90</body>
91</html>
92