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 ...
随机推荐
- BaseFragment 基类代码
public abstract class BaseFragment extends Fragment implements IBaseView { private List<BasePrese ...
- codecs模块, decode、encode
使用codecs模块,在Python中完成字符编码 字符的编码是按照某种规则在单字节字符和多字节字符之间进行转换的某种方法.从单字节到多字节叫做decoding,从多字节到单字节叫做encodin ...
- ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
MySQL版本:mysql5.7.21 修改用户权限,刷新权限表,报1146 mysql> flush privileges; ERROR 1146 (42S02): Table 'mysql. ...
- 清北学堂Day 6之STL
电脑突然一炸,什么都没有保存,凉了.(又出现了笔记凉凉事件嘤嘤嘤) 行吧慢慢回忆 就算我们会手写,我们也要学STL.吸了O2的STL可是要上天的. 数据结构 pair 使用方式: pair<类型 ...
- 【CDN+】 一些常用的Linux命令,crontab+VI+Hive(持续更新)
前言 本文主要是记录下工作中可能用到的一些linux指令,当作字典查用 Crontab 基本命令 # 安装 yum -y install vixie-cron crontabs#查看状态 servic ...
- 云计算openstack核心组件——keystone身份认证服务
一.Keystone介绍: keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户 ...
- dubbo入门和springboot集成dubbo小例子
从零开始搭建springboot-dubbo的例子 Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,以及 SOA 服务治理方案 一. Dubbo的简单介绍 1. ...
- Entity Framework 中遇到的一些小问题
一.在多表关联中,一个表中有另一个表的model累,在Include方法里提示lambda表达式不是委托类型,这时候需要using System.Data.Entity; 二.插入数据时,主键并非自增 ...
- java文件上传下载 使用SmartUpload组件实现
使用SmartUpload组件实现(下载jsmartcom_zh_CN.jar) 2017-11-07 1.在WebRoot创建以下文件夹,css存放样式文件(css文件直接拷贝进去),images存 ...
- tmux多终端工具
在Linux服务器上没有办法像在桌面系统一样开多个终端,所以有时后进行一些操作不是太方便,所以可以使用tmux工具,创建多个终端. 这里仅仅是简单的介绍一下如何创建多个终端和进行多个终端之间切换,tm ...