Namespaces
Variants

Standard library header <sstream>

From cppreference.net
Standard library headers

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

目录

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

函数

特化 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>;
}
说明:根据要求,所有HTML标签、属性及 标签内的C++代码均保持原样,仅对代码外的说明文字进行了翻译。由于原文中除代码外无其他需要翻译的文本内容,故输出结果与输入完全一致。

类模板 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;
    // 构造函数
    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);
    // 赋值与交换
    basic_stringbuf& operator=(const basic_stringbuf&) = delete;
    basic_stringbuf& operator=(basic_stringbuf&& rhs);
    void swap(basic_stringbuf& rhs) noexcept(see below);
    // 访问器与设置器
    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:
    // 重写的虚函数
    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;                        // 仅用于说明
    basic_string<CharT, Traits, Allocator> buf;     // 仅用于说明
    void init_buf_ptrs<span

类模板 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;
    // 构造函数
    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);
    // 赋值与交换
    basic_istringstream& operator=(const basic_istringstream&) = delete;
    basic_istringstream& operator=(basic_istringstream&& rhs);
    void swap(basic_istringstream& rhs);
    // 成员函数
    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;   // 仅用于说明
  };
  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;
    // 构造函数
    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);
    // 赋值与交换
    basic_ostringstream& operator=(const basic_ostringstream&) = delete;
    basic_ostringstream& operator=(basic_ostringstream&& rhs);
    void swap(basic_ostringstream& rhs);
    // 成员函数
    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;   // 仅用于说明
  };
  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;
    // 构造函数
    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);
    // 赋值与交换
    basic_stringstream& operator=(const basic_stringstream&) = delete;
    basic_stringstream& operator=(basic_stringstream&& rhs);
    void swap(basic_stringstream& rhs);
    // 成员函数
    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;  // 仅用于说明
  };
  template<class CharT, class Traits, class Allocator>
    void swap(basic_stringstream<CharT, Traits, Allocator>& x,
              basic_stringstream<CharT, Traits, Allocator>& y);
}

缺陷报告

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

缺陷报告 适用标准 发布时行为 正确行为
LWG 170 C++98 std::basic_ostringstream std::basic_stringstream
的概要说明中缺失 traits_type 定义
已补充
LWG 251 C++98 std::basic_stringbuf std::basic_istringstream
std::basic_ostringstream std::basic_stringstream
的概要说明中缺失 allocator_type 定义
已补充