boost circularBuffer
1. circular buffer has two fundamental properties:
(1): The capacity of the circular buffer is constant and set by you. The capacity doesn't change automatically when you call a member function such as push_back(). Only you can change the capacity of the circular buffer. The size of the circular buffer can not exceed the capacity you set.
(2): Despite constant capacity, you call push_back() as often as you like to insert elements into the circular buffer. If the maximum size has been reached and the circular buffer is full, elements are overwritten.
2. using boost::circular_buffer
#include <boost/circular_buffer.hpp>
#include <iostream> int main() {
boost::circular_buffer<int> cb(); std::cout << cb.capacity() << std::endl;
std::cout << cb.size() << std::endl; cb.push_back();
cb.push_back();
cb.push_back();
std::cout << cb.size() << std::endl; cb.push_back();
cb.push_back();
cb.push_back();
std::cout << cb.size() << std::endl; for (int i : cb) {
std::cout << i << std::endl;
} return ;
}
boost::circular_buffer is a template and must be instantiated with a type. The capacity of the circular buffer is specified when instantiating the class, not through a template paramter. The default constructor of boost::circular_buffer creates a buffer with a capacity of zero elements. Another constructor is available to set the capacity. The capacity of a circular buffer can be queried by calling capacity(). The capacity is not equivalent to the number of stored elements. While the return value of capacity() is constant, size() returns the number of elements in the buffer, which may be different. The return value of size() will always be between 0 and the capacity of teh circular buffer.
2. various member functions of boost::circular_buffer
#include <boost/circular_buffer.hpp>
#include <iostream> int main() {
boost::circular_buffer<int> cb();
cb.push_back();
cb.push_back();
cb.push_back();
cb.push_back(); std::cout << std::boolalpha << cb.is_linearized() << std::endl; boost::circular_buffer<int>::array_range ar1, ar2; ar1 = cb.array_one();
ar2 = cb.array_two();
std::cout << ar1.second << ";" << ar2.second << std::endl; for (int i : cb) {
std::cout << i << std::endl;
} cb.linearize(); ar1 = cb.array_one();
ar2 = cb.array_two();
std::cout << ar1.second << ";" << ar2.second << std::endl; return ;
}
A circular buffer is essentially comparable to std::vector. Because the beginning and end are well defined, a vector can be treated as conventional C array. That is, memory is contiguous, and the first and last elements are always at the lowest and highest memory address. However, a circular buffer does not offer such a guarantee.
The member function is_linearized() returns true if the beginning of the circular buffer is at the lowest memory address. In this case, all the elements in the buffer are stored consecutively from beginning to the end at increasing memory addresses, and elements can be accessed like a conventional C array.
If is_linearized() return false, the beginning of the circular buffer is not at the lowest memory address, which is the case as above. While the first three elements 0, 1 and 2 are stored in exactly this order, calling push_back() for the fourth time will overwrite the number 0 with the number 3. Becasue 3 is the last element added by a call to push_back(), it is now the new end of the circular buffer. The beginning is now the element with the number 1, which is stored at the next higher memory address. This means elements are no longer stored consecutively increasing memory addresses.
Both array_one() and array_two() return a std::pair whose first element si a pointer to the corrsponding array and whose second element is the size. array_onw() accesses the array at the beginning of the circular buffer, and array_two() accesses the array at the end of the buffer.
To simplify matters and treat the circular buffer as a conventional C array, you can force a rearrangement to the elements by calling linearize(). Once complete, you can access all stored elements using array_one(), and you don't need to use array_two().
boost circularBuffer的更多相关文章
- C/C++ 开源库及示例代码
C/C++ 开源库及示例代码 Table of Contents 说明 1 综合性的库 2 数据结构 & 算法 2.1 容器 2.1.1 标准容器 2.1.2 Lockfree 的容器 2.1 ...
- boost强分类器的实现
boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...
- Boost信号/槽signals2
信号槽是Qt框架中一个重要的部分,主要用来解耦一组互相协作的类,使用起来非常方便.项目中有同事引入了第三方的信号槽机制,其实Boost本身就有信号/槽,而且Boost的模块相对来说更稳定. signa ...
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- boost::function的用法
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1. 介绍 Boost.Func ...
- Boost条件变量condition_variable_any
Boost条件变量可以用来实现线程同步,它必须与互斥量配合使用.使用条件变量实现生产者消费者的简单例子如下,需要注意的是cond_put.wait(lock)是在等待条件满足.如果条件不满足,则释放锁 ...
- 新手,Visual Studio 2015 配置Boost库,如何编译和选择,遇到无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib“的解决办法
1,到官网下载最新的boost,www.boost.org 这里我下载的1-63版本. 2,安装,解压后运行bootstrap.bat文件.稍等一小会就OK. 3,编译boost库.注意一定要使用VS ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- vs2013给项目统一配置boost库
1.打开项目,然后点击菜单中的 视图->其他窗口->属性管理器 2. 打开属性管理器,点击项目前的箭头,展开项目,找到debug或者release下面的Microsoft.Cpp.Win3 ...
随机推荐
- CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)
题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数 做法一:对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线 ...
- 2017华南理工华为杯H bx值(容斥问题)
题目描述 对于一个nnn个数的序列 a1,a2,⋯,ana_1,a_2,\cdots,a_na1,a2,⋯,an,从小到大排序之后为ap1,ap2,⋯,apna_{p_1},a_{p ...
- springboot启动报错start bean 'eurekaAutoServiceRegistration' NullPointerException
解决方案参考:https://blog.csdn.net/hhj13978064496/article/details/82825365 我将eureka的依赖包放到了依赖包的最下面,启动报错, 如下 ...
- STM32 ADC基础与多通道采样
12位ADC是一种逐次逼近型模拟数字数字转换器.它有多达18个通道,可测量16个外部和2个内部信号源.ADC的输入时钟不得超过14MHZ,它是由PCLK2经分频产生.如果被ADC转换的模拟电压低于低阀 ...
- Oracle系列:触发器、作业、序列、连接
.Net程序员学用Oracle系列(8):触发器.作业.序列.连接 1.触发器 2.作业 2.1.作业调度功能和应用 2.2.通过 DBMS_JOB 来调度作业 3.序列 3.1.创建序列 3.2 ...
- vue双花括号的使用
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python程序打包为可执行文件exe
Python程序打包为可执行文件exe,pyinstaller应用 山重水复疑无路,柳暗花明又一村. 本来是向老师提交一个python程序,因为第一次所以就很尴尬只把源码给老师了,应该是打包成一个可执 ...
- MAC-安装套件管理工具Homebrew
前言 Homebrew是一款Mac OS下的套件管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能. Homebrew安装 1,Homebrew官网获取安装指令,官网地址:https://br ...
- c语言自带的排序与查找
qsort与bsearch qsort(元素起始地址,元素总数,单个元素的大小,比较函数) bsearch(key元素地址,元素起始地址,元素总数,单个元素的大小,比较函数) 比较函数: 原型为int ...
- Visual Studio中把文件夹导入工程中
VS用到的功能还是太少,记录备忘. 有的时候需要把其他库的源码导入当前工程直接使用,而这个库是源码形式,又带很多目录的. 之前从没遇到过这种情况,自己的库目录自己新建,添加. 第三方库一般有单独的Pr ...