std::basic_ostringstream<CharT,Traits,Allocator>::basic_ostringstream

来自cppreference.com

(1)
explicit basic_ostringstream( std::ios_base::openmode mode =
                                  std::ios_base::out );
(C++11 前)
explicit basic_ostringstream( std::ios_base::openmode mode );
(C++11 起)
basic_ostringstream()
    : basic_ostringstream(std::ios_base::out) {}
(2) (C++11 起)
explicit basic_ostringstream

    ( const std::basic_string<CharT, Traits, Allocator>& str,
      std::ios_base::openmode mode =

          std::ios_base::out );
(3)
explicit basic_ostringstream

    ( std::basic_string<CharT, Traits, Allocator>&& str,
      std::ios_base::openmode mode =

          std::ios_base::out );
(4) (C++20 起)
basic_ostringstream( std::ios_base::openmode mode, const Allocator& a );
(5) (C++20 起)
template< class SAlloc >

basic_ostringstream( const std::basic_string<CharT, Traits, SAlloc>& str,

                     std::ios_base::openmode mode, const Allocator& a );
(6) (C++20 起)
template< class SAlloc >

basic_ostringstream( const std::basic_string<CharT, Traits, SAlloc>& str,
                     const Allocator& a )

    : basic_ostringstream(str, std::ios_base::out, a) {}
(7) (C++20 起)
template< class SAlloc >

explicit basic_ostringstream
    ( const std::basic_string<CharT, Traits, SAlloc>& str,
      std::ios_base::openmode mode =

          std::ios_base::out );
(8) (C++20 起)
template< class StringViewLike >

explicit basic_ostringstream
    ( const StringViewLike& t,
      std::ios_base::openmode mode =

          std::ios_base::out );
(9) (C++26 起)
template< class StringViewLike >

basic_ostringstream( const StringViewLike& t,

                     std::ios_base::openmode mode, const Allocator& a );
(10) (C++26 起)
template< class StringViewLike >
basic_ostringstream( const StringViewLike& t, const Allocator& a );
(11) (C++26 起)
basic_ostringstream( basic_ostringstream&& other );
(12) (C++11 起)

构造新的字符串流。

给定

std::basic_ostream 基以及仅用于阐述的数据成员 sb 会按以下方式初始化。

 重载  std::basic_ostream sb
(1) base_type(std::addressof(sb))[1] buf_type(mode | std::ios_base::out)
(2) buf_type(std::ios_base::out)
(3) buf_type(str, mode | std::ios_base::out)
(4) buf_type(std::move(str), mode | std::ios_base::out)
(5) buf_type(mode | std::ios_base::out, a)
(6) buf_type(str, mode | std::ios_base::out, a)
(7) buf_type(str, std::ios_base::out, a)
(8) buf_type(str, mode | std::ios_base::out)
(9) std::addressof(sb) {t, mode | std::ios_base::out, Allocator()}
(10) {t, mode | std::ios_base::out, a}
(11) {t, std::ios_base::out, a}
(12) otherstd::basic_ostream
移动构造
other.sb 移动构造
  1. 在 C++11 前 std::basic_iostream 基(对于重载 (1,3))会以 base_type(&sb) 初始化。
8) 此重载只有在std::is_same_v<SAlloc, Allocator>false时才会参与重载决议。
9-11) 这些重载只有在std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>true时才会参与重载决议。

参数

str - 用于初始化字符串流内容的字符串
t - 用于初始化字符串流内容的对象(可转换到 std::basic_string_view
mode - 指定流打开模式。它是位掩码类型 (BitmaskType) ,定义下列常量:
常量 解释
app 每次写入前寻位到流结尾
binary 二进制模式打开
in 为读打开
out 为写打开
trunc 在打开时舍弃流的内容
ate 打开后立即寻位到流结尾
noreplace (C++23) 以独占模式打开
other - 用作源的另一字符串流

注解

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

功能特性测试 标准 注释
__cpp_lib_sstream_from_string_view 202306L (C++26) 字符串流的 std::string_view 接口,(9-11)

示例

#include <iostream>
#include <sstream>
 
int main()
{
    // 默认构造函数(输入/输出流)
    std::stringstream buf1;
    buf1 << 7;
    int n = 0;
    buf1 >> n;
    std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n';
 
    // 输入流
    std::istringstream inbuf("-10");
    inbuf >> n;
    std::cout << "n = " << n << '\n';
 
    // 输出流在追加模式(C++11)
    std::ostringstream buf2("test", std::ios_base::ate);
    buf2 << '1';
    std::cout << buf2.str() << '\n';
}

输出:

buf1 = 7 n = 7
n = -10
test1

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
P0935R0 C++11 默认构造函数是显式的 改成隐式的

参阅

获取或设置底层字符串设备对象的内容
(公开成员函数)
构造一个 basic_stringbuf 对象
(std::basic_stringbuf<CharT,Traits,Allocator> 的公开成员函数)