boost IOStreams
Boost.IOStreams provides numerous implementations of the two concepts. Devices which describes data sources and sinks, and stream which describes an interface for formatted input/output based on the interface from the standard library.
Devices
Devices are classes that provide read and write access to objects that are usually outside of a process.
1. using an array as a device with boost::iostreams::array_sink, boost::iostreams::array_source.
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <string>
#include <iostream> using namespace boost::iostreams; int main()
{
char buffer[];
array_sink sink{buffer};
stream<array_sink> os{sink};
os << "Boost" << std::endl; array_source source{buffer};
stream<array_source> is{source};
std::string s;
is >> s;
std::cout << s << std::endl;
}
Above example uses the device boost::iostreams::array_sink to write data to an array. The array is passed as a parameter to the constructor. Afterwards, the device is connected with a stream of type boost::iostreams::stream. A reference to the device is passed to the constructor of boost::iostreams::stream, and the type of the device is passed as a template parameter to boost::iostreams::stream.
uses the operator<< to write "Boost" to the stream. The stream forwards the data to the device. Because the device is connected to the array, "Boost" is stored in the first five elements of the array.
boost::iostreams::array_source is used like boost::iostreams::array_link. While boost::iostreams::array_sink supports only write operations, boost::iostreams::array_source supports only read. boost::iostreams::array supports both write and read operations.
Please note that boost::iostreams::array_source and boost::iostreams::array_sink receive a reference to an array. The array must not be destroyed while the devices are still in use.
2. using a vector as device with boost::iostreams::back_insert_device
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream.hpp>
#include <vector>
#include <string>
#include <iostream> using namespace boost::iostreams; int main()
{
std::vector<char> v;
back_insert_device<std::vector<char>> sink{v};
stream<back_insert_device<std::vector<char>>> os{sink};
os << "Boost" << std::endl; array_source source{v.data(), v.size()};
stream<array_source> is{source};
std::string s;
is >> s;
std::cout << s << std::endl;
return 0;
}
boost::iostreams::back_insert_device can be used to write data to any container that provides the member function insert(). The device calls this member function to forward data to the container. The example above uses boost::iostreams::back_insert_device to write "Boost" to a vector. Afterwards, "Boost" is read from boost::iostreams::array_source.
3. using a file as a device with boost::iostreams::file_source
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream> using namespace boost::iostreams; int main()
{
file_source f{"main.cpp"};
if (f.is_open())
{
stream<file_source> is{f};
std::cout << is.rdbuf() << std::endl;
f.close();
}
return ;
}
boost::iostreams::file_source provides is_open() to test whether a file was opened successfully. It also provides the member function close() to explicitly close a file.
Filters
Boost.IOStreams also provides filters, which operate in front of devices to filter data read from or written to devices.
1. using boost::iostreams::regex_filter
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/regex.hpp>
#include <boost/regex.hpp>
#include <iostream> using namespace boost::iostreams; int main()
{
char buffer[];
array_sink sink{buffer};
filtering_ostream os;
os.push(regex_filter{boost::regex{"Bo+st"}, "C++"});
os.push(sink);
os << "Boost" << std::flush;
os.pop();
std::cout.write(buffer, );
return ;
}
The data is sent through a filter of type boost::iostreams::regex_filter, which replaces characters. The filter expects a regular expression and a format string. The format string specifies what the characters should be replaced with.
The filter and the device are connected with the stream boost::iostreams::filtering_ostream. This class provides a member function push(), which the filter and the device are passed to. The filter(s) must be passed before the device; the order is important. You can pass one or more filters, but once a device has been passed, the stream is complete, and you must not call push() again.
The filter boost::iostreams::regex_filter can’t process data character by character because regular expressions need to look at character groups. That’s why boost::iostreams::regex_filter starts filtering only after a write operation is complete and all data is available. This happens when the device is removed from the stream with the member function pop(). Example above calls pop() after “Boost” has been written to the stream. Without the call to pop(), boost::iostreams::regex_filter won’t process any data and won’t forward data to the device.
2. accessing filters in boost::iostreams::filtering_ostream
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/counter.hpp>
#include <iostream> using namespace boost::iostreams; int main()
{
char buffer[];
array_sink sink{buffer};
filtering_ostream os;
os.push(counter{});
os.push(sink);
os << "Boost" << std::flush;
os.pop();
counter *c = os.component<counter>();
std::cout << c->characters() << std::endl;
std::cout << c->lines() << std::endl;
return ;
}
The filter boost::iostreams::counter counts characters and lines. This class provides the member function characters() and lines().
boost::iostreams::filtering_stream provides the member function component() to access a filter. Because component() is a template, the type of the filter must be passed as a template parameter. component() returns a pointer to the filter.
3. writing and reading data compressed with ZLIB
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/zlib.hpp>
#include <vector>
#include <string>
#include <iostream> using namespace boost::iostreams; int main()
{
std::vector<char> v;
back_insert_device<std::vector<char>> snk{v};
filtering_ostream os;
os.push(zlib_compressor{});
os.push(snk);
os << "Boost" << std::flush;
os.pop(); array_source src{v.data(), v.size()};
filtering_istream is;
is.push(zlib_decompressor{});
is.push(src);
std::string s;
is >> s;
std::cout << s << std::endl;
return ;
}
Example above uses the stream boost::iostreams::filtering_istream in addition to boost::iostreams::filtering_ostream. This stream is used when you want to read data with filters. In the example, compressed data is written and read again.
Boost.IOStreams provides several data compression filters. The class boost::iostreams::zlib_compressor compresses data in the ZLIB format. To uncompress data in the ZLIB format, use the class boost::iostreams::zlib_decompressor. These filters are added to the streams using push().
boost IOStreams的更多相关文章
- Boost C++: 网络编程1
#include <iostream> #include <boost/asio.hpp> #include <boost/config/compiler/visualc ...
- Linux上安装使用boost入门指导
Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...
- Win7下Boost库的安装
Boost库是C++领域公认的经过千锤百炼的知名C++类库,涉及编程中的方方面面,简单记录一下使用时的安装过程 1.boost库的下载 boost库官网主页:www.boost.org 2.安装 将下 ...
- Boost 1.61.0 Library Documentation
http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...
- VS2008下直接安装使用Boost库1.46.1版本号
Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C ...
- VS2008下直接安装使用Boost库1.46.1版本
Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C++ ...
- VS2008下直接安装Boost库1.46.1版本号
Boost图书馆是一个移植.提供源代码C++库.作为一个备份标准库,这是C++发动机之间的一种标准化的过程. Boost图书馆由C++图书馆标准委员会工作组成员发起,一些内容有望成为下一代C++标准库 ...
- boost 学习笔记 0: 安装环境
boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...
- Visual Studio 2013 boost
E:\Visual Studio 2013\install\VC\bin\amd64>E:\IFC\boost_1_56_0_vs2013'E:\IFC\boost_1_56_0_vs2013' ...
随机推荐
- LintCode之主元素
题目描述: 分析:由题目可知这个数组不为空且该主元素一定存在,我选用HashMap来存储,HashMap的存储结构是”键—值对“,”键“用来存储数组元素,”值“用来存储这个元素出现的次数,然后循环遍历 ...
- 最长上升子序列(LIS)题目合集
有关最长上升子序列的详细算法解释在http://www.cnblogs.com/denghaiquan/p/6679952.html 1)51nod 1134 一题裸的最长上升子序列,由于N<= ...
- ES6中set的用法回顾
ES6中的set类似一个数组,但是其中的值都是唯一的,Set本身是一个构造函数,用来生成 Set 数据结构. set函数可以接受一个数组作为参数,用来初始化: const set = new Set( ...
- 网络设备MIB浏览器ifType、ifDescr、ifMtu、ifInOctets等的含义(Zabbix SNMP)
1.ifType 接口的类型 取值117表示接口为GigabitEthernet (取值62表示接口为 FastEnthernet) 2.ifDescr 接口类型的描述 有GigabitEtherne ...
- ECG 项目预研
1. 数据的采集 智能安全帽,流数据,鉴于数据量大,应该是采集到云平台上,然后在云平台上对数据处理,是一种典型的物联网+大数据应用场景,考虑使用AWS或者阿里云,然后搭建Hadoop/Spark 环境 ...
- java注解编程@since 1.8
一.基本元注解: @Retention: 说明这个注解的生命周期 RetentionPolicy.SOURCE -> 保留在原码阶段,编译时忽略 RetentionPolicy.CLASS -& ...
- Struts2之校验
##1.输入校验 错误提示页面 <%@ page language="java" contentType="text/html; charset=UTF-8&quo ...
- Spring cloud 注册服务小结
服务注册中心:Eureka.Zookeeper.Cousul.Nacos 使用RestTemplate.openFeign做服务调用,底层使用的是Ribbon. Ribbon做了负载均衡,也可以做一个 ...
- php实现字符串翻转,使字符串的单词正序,单词的字符倒序
如字符串'I love you'变成'I evol uoy',只能使用strlen(),不能使用其他内置函数. function strturn($str){ $pstr=''; $sstr=''; ...
- python 注释有哪些和作用
python 单行注释 #作为代表 python 多行注释 ‘’‘ 这是三个单引号注释 ’‘’ “”“ 这是三个双引号注释 ”“”