命名空间
变体
操作

标准库头文件 <sstream>

来自 cppreference.com
< cpp‎ | header
 
 
标准库头文件
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)

内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用实用程序
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<debugging> (C++26)
<expected> (C++23)
<bitset>
<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)
<inplace_vector> (C++26)
迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<cmath>
<linalg> (C++26)
<numbers> (C++20)

时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17/26*)
<text_encoding> (C++26)
输入/输出
<sstream>
<spanstream> (C++23)
<fstream>
<syncstream> (C++20)
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/26*)
正则表达式
<regex> (C++11)
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<rcu> (C++26)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)

<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)

<barrier> (C++20)
<future> (C++11)
<hazard_pointer> (C++26)

C 兼容性
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (直到 C++20)

 

此头文件是 输入/输出 库的一部分。

内容

实现原始字符串设备
(类模板) [编辑]
实现高级字符串流输入操作
(类模板) [编辑]
实现高级字符串流输出操作
(类模板) [编辑]
实现高级字符串流输入/输出操作
(类模板) [编辑]
stringbuf std::basic_stringbuf<char>
(typedef)
wstringbuf std::basic_stringbuf<wchar_t>
(typedef)
istringstream std::basic_istringstream<char>
(typedef)
wistringstream std::basic_istringstream<wchar_t>
(typedef)
ostringstream std::basic_ostringstream<char>
(typedef)
wostringstream std::basic_ostringstream<wchar_t>
(typedef)
stringstream std::basic_stringstream<char>
(typedef)
wstringstream std::basic_stringstream<wchar_t>
(typedef)

函数

专门化了 std::swap 算法
(函数模板) [编辑]
专门化了 std::swap 算法
(函数模板) [编辑]
专门化了 std::swap 算法
(函数模板) [编辑]
专门化了 std::swap 算法
(函数模板) [编辑]

[编辑] 概要

namespace std {
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
    class basic_stringbuf;
 
  using stringbuf  = basic_stringbuf<char>;
  using wstringbuf = basic_stringbuf<wchar_t>;
 
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
    class basic_istringstream;
 
  using istringstream  = basic_istringstream<char>;
  using wistringstream = basic_istringstream<wchar_t>;
 
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
    class basic_ostringstream;
  using ostringstream  = basic_ostringstream<char>;
  using wostringstream = basic_ostringstream<wchar_t>;
 
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
    class basic_stringstream;
  using stringstream  = basic_stringstream<char>;
  using wstringstream = basic_stringstream<wchar_t>;
}

[编辑] 类模板 std::basic_stringbuf

namespace std {
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
  class basic_stringbuf : public basic_streambuf<CharT, Traits> {
  public:
    using char_type      = CharT;
    using int_type       = typename Traits::int_type;
    using pos_type       = typename Traits::pos_type;
    using off_type       = typename Traits::off_type;
    using traits_type    = Traits;
    using allocator_type = Allocator;
 
    // constructors
    basic_stringbuf() : basic_stringbuf(ios_base::in | ios_base::out) {}
    explicit basic_stringbuf(ios_base::openmode which);
    explicit basic_stringbuf(
      const basic_string<CharT, Traits, Allocator>& s,
      ios_base::openmode which = ios_base::in | ios_base::out);
    explicit basic_stringbuf(const Allocator& a)
      : basic_stringbuf(ios_base::in | ios_base::out, a) {}
    basic_stringbuf(ios_base::openmode which, const Allocator& a);
    explicit basic_stringbuf(
      basic_string<CharT, Traits, Allocator>&& s,
      ios_base::openmode which = ios_base::in | ios_base::out);
    template<class SAlloc>
      basic_stringbuf(
        const basic_string<CharT, Traits, SAlloc>& s, const Allocator& a)
        : basic_stringbuf(s, ios_base::in | ios_base::out, a) {}
    template<class SAlloc>
      basic_stringbuf(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_stringbuf(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which = ios_base::in | ios_base::out);
    basic_stringbuf(const basic_stringbuf&) = delete;
    basic_stringbuf(basic_stringbuf&& rhs);
    basic_stringbuf(basic_stringbuf&& rhs, const Allocator& a);
 
    // assign and swap
    basic_stringbuf& operator=(const basic_stringbuf&) = delete;
    basic_stringbuf& operator=(basic_stringbuf&& rhs);
    void swap(basic_stringbuf& rhs) noexcept(see below);
 
    // getters and setters
    allocator_type get_allocator() const noexcept;
 
    basic_string<CharT, Traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<CharT,Traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<CharT, Traits, Allocator> str() &&;
    basic_string_view<CharT, Traits> view() const noexcept;
 
    void str(const basic_string<CharT, Traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<CharT, Traits, SAlloc>& s);
    void str(basic_string<CharT, Traits, Allocator>&& s);
 
  protected:
    // overridden virtual functions
    int_type underflow() override;
    int_type pbackfail(int_type c = Traits::eof()) override;
    int_type overflow (int_type c = Traits::eof()) override;
    basic_streambuf<CharT, Traits>* setbuf(CharT*, streamsize) override;
 
    pos_type seekoff(off_type off, ios_base::seekdir way,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;
    pos_type seekpos(pos_type sp,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;
 
  private:
    ios_base::openmode mode;                        // exposition only
    basic_string<CharT, Traits, Allocator> buf;     // exposition only
    void init_buf_ptrs();                           // exposition only
  };
 
  template<class CharT, class Traits, class Allocator>
    void swap(basic_stringbuf<CharT, Traits, Allocator>& x,
              basic_stringbuf<CharT, Traits, Allocator>& y) noexcept(noexcept(x.swap(y)));
}

[编辑] 类模板 std::basic_istringstream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
  class basic_istringstream : public basic_istream<CharT, Traits> {
  public:
    using char_type      = CharT;
    using int_type       = typename Traits::int_type;
    using pos_type       = typename Traits::pos_type;
    using off_type       = typename Traits::off_type;
    using traits_type    = Traits;
    using allocator_type = Allocator;
 
    // constructors
    basic_istringstream() : basic_istringstream(ios_base::in) {}
    explicit basic_istringstream(ios_base::openmode which);
    explicit basic_istringstream(
      const basic_string<CharT, Traits, Allocator>& s,
      ios_base::openmode which = ios_base::in);
    basic_istringstream(ios_base::openmode which, const Allocator& a);
    explicit basic_istringstream(
      basic_string<CharT, Traits, Allocator>&& s,
      ios_base::openmode which = ios_base::in);
    template<class SAlloc>
      basic_istringstream(
        const basic_string<CharT, Traits, SAlloc>& s, const Allocator& a)
        : basic_istringstream(s, ios_base::in, a) {}
    template<class SAlloc>
      basic_istringstream(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_istringstream(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which = ios_base::in);
    basic_istringstream(const basic_istringstream&) = delete;
    basic_istringstream(basic_istringstream&& rhs);
 
    // assign and swap
    basic_istringstream& operator=(const basic_istringstream&) = delete;
    basic_istringstream& operator=(basic_istringstream&& rhs);
    void swap(basic_istringstream& rhs);
 
    // members
    basic_stringbuf<CharT, Traits, Allocator>* rdbuf() const;
    basic_string<CharT, Traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<CharT,Traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<CharT, Traits, Allocator> str() &&;
    basic_string_view<CharT, Traits> view() const noexcept;
 
    void str(const basic_string<CharT, Traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<CharT, Traits, SAlloc>& s);
    void str(basic_string<CharT, Traits, Allocator>&& s);
 
  private:
    basic_stringbuf<CharT, Traits, Allocator> sb;   // exposition only
  };
 
  template<class CharT, class Traits, class Allocator>
    void swap(basic_istringstream<CharT, Traits, Allocator>& x,
              basic_istringstream<CharT, Traits, Allocator>& y);
}

[编辑] 类模板 std::basic_ostringstream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
  class basic_ostringstream : public basic_ostream<CharT, Traits> {
  public:
    using char_type      = CharT;
    using int_type       = typename Traits::int_type;
    using pos_type       = typename Traits::pos_type;
    using off_type       = typename Traits::off_type;
    using traits_type    = Traits;
    using allocator_type = Allocator;
 
    // constructors
    basic_ostringstream() : basic_ostringstream(ios_base::out) {}
    explicit basic_ostringstream(ios_base::openmode which);
    explicit basic_ostringstream(
      const basic_string<CharT, Traits, Allocator>& s,
      ios_base::openmode which = ios_base::out);
    basic_ostringstream(ios_base::openmode which, const Allocator& a);
    explicit basic_ostringstream(
      basic_string<CharT, Traits, Allocator>&& s,
      ios_base::openmode which = ios_base::out);
    template<class SAlloc>
      basic_ostringstream(
        const basic_string<CharT, Traits, SAlloc>& s, const Allocator& a)
        : basic_ostringstream(s, ios_base::out, a) {}
    template<class SAlloc>
      basic_ostringstream(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_ostringstream(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which = ios_base::out);
    basic_ostringstream(const basic_ostringstream&) = delete;
    basic_ostringstream(basic_ostringstream&& rhs);
 
    // assign and swap
    basic_ostringstream& operator=(const basic_ostringstream&) = delete;
    basic_ostringstream& operator=(basic_ostringstream&& rhs);
    void swap(basic_ostringstream& rhs);
 
    // members
    basic_stringbuf<CharT, Traits, Allocator>* rdbuf() const;
 
    basic_string<CharT, Traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<CharT,Traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<CharT, Traits, Allocator> str() &&;
    basic_string_view<CharT, Traits> view() const noexcept;
 
    void str(const basic_string<CharT, Traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<CharT, Traits, SAlloc>& s);
    void str(basic_string<CharT, Traits, Allocator>&& s);
 
   private:
    basic_stringbuf<CharT, Traits, Allocator> sb;   // exposition only
  };
 
  template<class CharT, class Traits, class Allocator>
    void swap(basic_ostringstream<CharT, Traits, Allocator>& x,
              basic_ostringstream<CharT, Traits, Allocator>& y);
}

[编辑] 类模板 std::basic_stringstream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>,
           class Allocator = allocator<CharT>>
  class basic_stringstream : public basic_iostream<CharT, Traits> {
  public:
    using char_type      = CharT;
    using int_type       = typename Traits::int_type;
    using pos_type       = typename Traits::pos_type;
    using off_type       = typename Traits::off_type;
    using traits_type    = Traits;
    using allocator_type = Allocator;
 
    // constructors
    basic_stringstream() : basic_stringstream(ios_base::out | ios_base::in) {}
    explicit basic_stringstream(ios_base::openmode which);
    explicit basic_stringstream(
      const basic_string<CharT, Traits, Allocator>& s,
      ios_base::openmode which = ios_base::out | ios_base::in);
    basic_stringstream(ios_base::openmode which, const Allocator& a);
    explicit basic_stringstream(
      basic_string<CharT, Traits, Allocator>&& s,
      ios_base::openmode which = ios_base::out | ios_base::in);
    template<class SAlloc>
      basic_stringstream(
        const basic_string<CharT, Traits, SAlloc>& s, const Allocator& a)
        : basic_stringstream(s, ios_base::out | ios_base::in, a) {}
    template<class SAlloc>
      basic_stringstream(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_stringstream(
        const basic_string<CharT, Traits, SAlloc>& s,
        ios_base::openmode which = ios_base::out | ios_base::in);
    basic_stringstream(const basic_stringstream&) = delete;
    basic_stringstream(basic_stringstream&& rhs);
 
    // assign and swap
    basic_stringstream& operator=(const basic_stringstream&) = delete;
    basic_stringstream& operator=(basic_stringstream&& rhs);
    void swap(basic_stringstream& rhs);
 
    // members
    basic_stringbuf<CharT, Traits, Allocator>* rdbuf() const;
 
    basic_string<CharT, Traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<CharT,Traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<CharT, Traits, Allocator> str() &&;
    basic_string_view<CharT, Traits> view() const noexcept;
 
    void str(const basic_string<CharT, Traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<CharT, Traits, SAlloc>& s);
    void str(basic_string<CharT, Traits, Allocator>&& s);
 
  private:
    basic_stringbuf<CharT, Traits> sb;  // exposition only
  };
 
  template<class CharT, class Traits, class Allocator>
    void swap(basic_stringstream<CharT, Traits, Allocator>& x,
              basic_stringstream<CharT, Traits, Allocator>& y);
}

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布的行为 正确行为
LWG 170 C++98 在以下的概要中,traits_type 的定义缺失:
std::basic_ostringstreamstd::basic_stringstream
已添加
LWG 251 C++98 在以下的概要中,allocator_type 的定义缺失:
std::basic_stringbuf, std::basic_istringstream
std::basic_ostringstreamstd::basic_stringstream
已添加