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 ...
随机推荐
- (转)pd.read_csv之OSError: Initializing from file failed的解决方案
转:https://blog.csdn.net/funnyPython/article/details/78532102 rides = pd.read_csv(data_path)1 # OSErr ...
- Centos7.6 安装
序:在“Intel 英特尔 NUC6I7KYK 迷你主机” 上 安装“Centos7.6” 一.制作U盘启动盘 参考<制作CentOS的U盘启动盘>.<制作 CentOS7的U盘系统 ...
- spring-cloud zuul网关
API Gateway 是随着微服务(Microservice)这个概念一起兴起的一种架构模式,它用于解决微服务过于分散,没有一个统一的出入口进行流量管理的问题. 使用 Zuul 实现 API Gat ...
- IPv6测试环境搭建
IPv6的简介 IPv4 和 IPv6的区别就是 IP 地址前者是 .(dot)分割,后者是以 :(冒号)分割的(更多详细信息自行搜索). PS:在使用 IPv6 的热点时候,记得手机开 飞行模式 哦 ...
- WEB服务端安全---文件上传漏洞
1.简述 文件上传漏洞是指用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务端命令的能力.这种攻击方式是最直接和有效的,而且互联网中我们经常会用到文件上传功能,它本身是没有问题的,正常的业 ...
- jQuery基础--选择器
2. 选择器 2.1. 什么是jQuery选择器 jQuery选择器是jQuery为我们提供的一组方法,让我们更加方便的获取到页面中的元素.注意:jQuery选择器返回的是jQuery对象. jQue ...
- 网页导出excel
package site.action.ecom.backend.wechat.exportExcel; import java.lang.annotation.Documented;import j ...
- python3标准库总结
Python3标准库 操作系统接口 os模块提供了不少与操作系统相关联的函数. ? 1 2 3 4 5 6 >>> import os >>> os.getcwd( ...
- MYSQL 的七种join
建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...
- CSS的置换和非置换元素
一个来自面试的坑. 面试的时候考官先问了行内元素和块级元素的区别,这个不难理解.然后一脚就踩进了,置换元素的坑.例如img就是行内置换元素,这种行内元素是可以设置宽高的. 什么是置换元素 一个内容不受 ...