c++11 标准模板(STL)(std::basic_istringstream)(二)

复制代码
定义于头文件 <sstream>

|-------------------------------------------------------------------------------------------------------------------------------------------|---|-----------|
| template< class CharT, class Traits = std::char_traits<CharT> > class basic_ostringstream; | | (C++11 前) |
| template< class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> > class basic_ostringstream; | | (C++11 起) |

类模板 std::basic_ostringstream 实现基于字符串的流上的输入与输出操作。它等效地存储一个 std::basic_string 的实例,并在其上进行输出操作。

在低层,该类实际上包装 std::basic_stringbuf 的未处理字符串设备到 std::basic_ostream 的高层接口中。提供到独有 std::basic_stringbuf 成员的完整接口。

亦定义对常用字符类型的二个特化:

|------------------|--------------------------------|
| 类型 | 定义 |
| ostringstream | basic_ostringstream<char> |
| wostringstream | basic_ostringstream<wchar_t> |

成员函数

构造字符串流

复制代码
std::basic_ostringstream<CharT,Traits,Allocator>::basic_ostringstream

|-----------------------------------------------------------------------------------------------------------------------------------|-----|-----------|-----------|
| basic_ostringstream() : basic_ostringstream(ios_base::out) { } | (1) | (C++11 起) |
| explicit basic_ostringstream( ios_base::openmode mode = ios_base::out ); | (2) | (C++11 前) |
| explicit basic_ostringstream( ios_base::openmode mode ); | (2) | (C++11 起) |
| explicit basic_ostringstream( const std::basic_string<CharT,Traits,Allocator>& str, ios_base::openmode mode = ios_base::out ); | (2) | (3) | |
| basic_ostringstream( basic_ostringstream&& other ); | (2) | (4) | (C++11 起) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |
| (2) |

构造新的字符串流。

  1. 默认构造函数。以默认打开模式构造新的底层字符串设备。

  2. 构造新的底层字符串设备。以 basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::out) 构造底层 basic_stringbuf 对象。

  3. str 的副本为底层字符串设备的初始内容。以 basic_stringbuf<Char,Traits,Allocator>(str, mode | ios_base::out) 构造底层 basic_stringbuf 对象。

  4. 移动构造函数。用移动语义,构造拥有 other 的状态的字符串流。

参数

|-------|---|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| str | - | 用于初始化字符串流内容的字符串 |
| mode | - | 指定流打开模式。它是位掩码类型,定义下列常量: |--------|-------------| | 常量 | 解释 | | app | 每次写入前寻位到流结尾 | | binary | 以二进制模式打开 | | in | 为读打开 | | out | 为写打开 | | trunc | 在打开时舍弃流的内容 | | ate | 打开后立即寻位到流结尾 | |
| other | - | 用作源的另一字符串流 |

注意

在短的循环中,例如在用于字符串转换时构造单次使用的 basic_ostringstream 对象,开销可能显著高于调用 str 并复用同一对象。

调用示例

复制代码
#include <sstream>
#include <string>
#include <iostream>

int main()
{
    //1) 默认构造函数。以默认打开模式构造新的底层字符串设备。
    std::basic_ostringstream<int> basic_ostringstream1;

    //2) 构造新的底层字符串设备。
    //以 basic_stringbuf<Char,Traits,Allocator>(mode | ios_base::out)
    //构造底层 basic_stringbuf 对象。
    std::basic_ostringstream<char> basic_ostringstream2(std::ios_base::out);

    //3) 以 str 的副本为底层字符串设备的初始内容。
    std::string string1 = "I am a handsome programmer";
    std::basic_ostringstream<char>
    basic_ostringstream3(string1, std::ios_base::out | std::ios_base::app);
    std::cout << "basic_ostringstream3: " << basic_ostringstream3.str() << std::endl;
    basic_ostringstream3 << "GGX";
    std::cout << "basic_ostringstream3: " << basic_ostringstream3.str() << std::endl;
    std::cout << std::endl;

    //4) 移动构造函数。用移动语义,构造拥有 other 的状态的字符串流。
    std::basic_ostringstream<char> basic_ostringstream4(std::move(basic_ostringstream3));
    std::cout << "basic_ostringstream4: " << basic_ostringstream4.str() << std::endl;

    return 0;
}

输出

相关推荐
虾球xz32 分钟前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz33 分钟前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
虾球xz6 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc6 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威7 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
Dream it possible!8 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
Dddle19 小时前
C++:this指针
java·c语言·开发语言·c++
不見星空10 小时前
2025年第十六届蓝桥杯软件赛省赛C/C++大学A组个人解题
c语言·c++·蓝桥杯
jiunian_cn10 小时前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
梁下轻语的秋缘10 小时前
每日c/c++题 备战蓝桥杯(洛谷P1387 最大正方形)
c语言·c++·蓝桥杯