当前位置: 首页 > news >正文

c++11 标准模板(STL)(std::basic_stringbuf)(三)

定义于头文件 <sstream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>

> class basic_stringbuf : public std::basic_streambuf<CharT, Traits>

 std::basic_stringbuf 是关联字符序列为内存常驻的任意字符序列的 std::basic_streambuf 。能从 std::basic_string 的实例初始化它,或将它做成该类的实例。

std::basic_stringbuf 的典型实现保有一个 std::basic_string 类型对象,或等价的可伸缩序列容器作为数据成员,并将它同时用作受控制字符序列(为 std::basic_streambuf 的六个指针所指向的数组)和关联字符序列(所有输入操作的字符源和输出操作的目标)。

另外,典型的实现保有一个 std::ios_base::openmode 类型的数据成员,以指示流的状态(只读、只写、读写、尾端写等)。

若 overflow() 使用过分配策略,则可存储另外的高水位指针,以跟踪最后初始化的字符。(C++11 起)

亦提供二个对常用字符类型的特化:

类型定义
stringbufbasic_stringbuf<char>
wstringbufbasic_stringbuf<wchar_t>

公开成员函数

赋值 basic_stringbuf 对象

std::basic_stringbuf<CharT,Traits,Allocator>::operator=

std::basic_stringbuf& operator=( std::basic_stringbuf&& rhs );

(1)(C++11 起)

std::basic_stringbuf& operator=( const std::basic_stringbuf& rhs ) = delete;

(2)

1) 移动赋值运算符:移动 rhs 的内容到 *this 中。移动后 *this 拥有 rhs 之前保有的关联 string 、打开模式、本地环境和所有其他状态。保证 *this 中 std::basic_streambuf 的六个指针有别于被移动的 rhs 的对应指针,除非它们为空。

2) 复制赋值运算符被删除; basic_stringbuf可复制赋值 (CopyAssignable) 。

参数

rhs-将被移动的另一 basic_stringbuf

返回值

*this

 调用示例

#include <sstream>
#include <string>
#include <iostream>int main()
{std::basic_stringbuf<char> one("one", std::ios_base::in| std::ios_base::out| std::ios_base::ate);std::basic_stringbuf<char> two("two", std::ios_base::in| std::ios_base::out| std::ios_base::ate);std::cout << "Before move, one = \"" << one.str() << '"'<< " two = \"" << two.str() << "\"" << std::endl;//1) 移动赋值运算符:移动 rhs 的内容到 *this 中。//移动后 *this 拥有 rhs 之前保有的关联 string 、打开模式、本地环境和所有其他状态。//保证 *this 中 std::basic_streambuf 的六个指针有别于被移动的 rhs 的对应指针,除非它们为空。one = std::move(two);std::cout << "After move, one = \"" << one.str() << '"'<< " two = \"" << two.str() << "\"" << std::endl;return 0;
}

输出

交换二个 basic_stringbuf 对象

std::basic_stringbuf<CharT,Traits,Allocator>::swap

void swap( std::basic_stringbuf& rhs )

(C++11 起)

交换 *this 和 rhs 的状态与内容。

参数

rhs-另一 basic_stringbuf

返回值

(无)

注意

交换 std::stringstream 对象时自动调用此函数,很少需要直接调用它。

调用示例

#include <sstream>
#include <string>
#include <iostream>int main()
{std::basic_stringbuf<char> one("one", std::ios_base::in| std::ios_base::out| std::ios_base::ate);std::basic_stringbuf<char> two("two", std::ios_base::in| std::ios_base::out| std::ios_base::ate);std::cout << "Before move, one = \"" << one.str() << '"'<< " two = \"" << two.str() << "\"" << std::endl;//交换 *this 和 rhs 的状态与内容。one.swap(two);std::cout << "After swap, one = \"" << one.str() << '"'<< " two = \"" << two.str() << "\"" << std::endl;return 0;
}

输出

 

替换或获得关联字符串的副本

std::basic_stringbuf<CharT,Traits,Allocator>::str

std::basic_string<CharT, Traits, Allocator> str() const;

(1)

void str( const std::basic_string<CharT, Traits, Allocator>& s);

(2)

获取和设置底层字符串。

1) 创建并返回保有此 std::basic_stringbuf 底层字符序列副本的 std::basic_string 。对于仅输入流,返回的字符串含来自范围 [eback(), egptr()) 的字符。对于输入/输出或仅输出流,含有 pbase() 到序列中末字符的字符,不考虑 egptr() 和 epptr() 。

为写入打开的缓冲区中的成员字符序列能为效率目的过分配。该情况下,仅返回初始化的字符:从构造函数 string 参数获得的字符、最近对 str() 的第二重载的 string 参数的字符或来自写入操作的字符。典型的使用过分配的实现维护一个高水位指针,以跟踪缓冲区已初始化部分的结尾,而此重载返回从 pbase() 到高水位指针的字符。

(C++11 起)

2) 删除此 std::basic_stringbuf 的整个底层字符序列,然后配置新的含有 s 内容副本的底层字符序列。以下列方式初始化 std::basic_streambuf 的指针:

  • 对于输入流( mode & ios_base::in == true ), eback() 指向首字符, gptr() == eback() ,而 egptr() == eback() + s.size() :后继输入将读取首个复制自 s 的字符。
  • 对于输出流( mode & ios_base::out == true ), pbase() 指向首字符而 epptr() >= pbase() + s.size() (允许 epptr 指向更远以令后随的 sputc() 不会立即调用 overflow()
    • 对于后附流( mode & ios_base::ate == true ), pptr() == pbase() + s.size() ,从而后继输出将被后附到复制自 s 的最后字符。(C++11 起)
    • 对于非后附输出流, pptr() == pbase() ,从而后继输出将重写复制自 s 的字符。

参数

s-保有替换字符序列的 string 对象

返回值

1) 保有此缓冲的底层字符序列副本的 string 对象。

2) (无)

注意

此函数典型地通过 std::basic_stringstream::str() 访问。

 调用示例

#include <sstream>
#include <string>
#include <iostream>int main()
{char n;std::basic_stringbuf<char> in;  // 亦能用 in("1 2")in.str("1 2"); // 设置获取区n = in.sgetc();std::cout << "after reading the first char from \"1 2\", the char is "<< n << ", str() = \"" << in.str() << "\"" << std::endl; // 或 in.str()std::basic_stringbuf<char> out("1 2");out.sputc('3');std::cout << "after writing the char '3' to output stream \"1 2\""<< ", str() = \"" << out.str() << "\"" << std::endl;return 0;
}

输出

 

http://www.lryc.cn/news/124002.html

相关文章:

  • Nodejs 第九章(模块化)
  • shell之正则表达式及三剑客grep命令
  • LeetCode 热题 100 JavaScript--33. 搜索旋转排序数组
  • 并发编程 - 线程池中的常见面试题
  • 将多个单独的 Excel 文件合并成一个,并添加标题行
  • VPN pptp和l2tp协议破解
  • 4.3、Flink任务怎样读取Kafka中的数据
  • C语言实例_和校验算法
  • 安全加密框架图——Oracle安全开发者
  • Android databinding 被多次定义
  • 云原生周刊:Kubernetes v1.28 新特性一览 | 2023.8.14
  • 机器学习之分类模型
  • 学习Vue:创建第一个Vue实例
  • JavaFx基础学习【二】:Stage
  • C语言——动态内存函数(malloc、calloc、realloc、free)
  • Redis数据结构——Redis简单动态字符串SDS
  • 【计算机网络】TCP协议超详细讲解
  • Salesforce特别元数据部署技巧
  • [前端系列第2弹]CSS入门教程:从零开始学习Web页面的样式和布局
  • 非计算机科班如何丝滑转码?
  • 亿发创新中医药信息化解决方案,自动化煎煮+调剂,打造智能中药房
  • Vulnhub: MoneyBox: 1靶机
  • [国产MCU]-BL602开发实例-LCD1602 I2C驱动
  • AI 绘画Stable Diffusion 研究(七) 一文读懂 Stable Diffusion 工作原理
  • URLSearchParams:JavaScript中的URL查询参数处理工具
  • 1.4 数据库管理与优化
  • T113-S3 Tina-Linux -- 2.开发板使用
  • Django-配置邮箱功能(一):使用django自带的发送邮件功能
  • JS实现树形结构、一维数组以及map之间的转换
  • Vue中自定义.js变量