Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |io‎ |basic stringstream

      [edit template]
       
       
       
       
      (1)
      explicit basic_stringstream(std::ios_base::openmode mode=
                                       std::ios_base::in|std::ios_base::out);
      (until C++11)
      explicit basic_stringstream(std::ios_base::openmode mode);
      (since C++11)
      basic_stringstream()
         : basic_stringstream(std::ios_base::in|std::ios_base::out){}
      (2)(since C++11)
      explicit basic_stringstream

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

               std::ios_base::in|std::ios_base::out);
      (3)
      explicit basic_stringstream

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

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

      basic_stringstream(conststd::basic_string<CharT, Traits, SAlloc>& str,

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

      basic_stringstream(conststd::basic_string<CharT, Traits, SAlloc>& str,
                         const Allocator& a)

         : basic_stringstream(str,std::ios_base::in|std::ios_base::out, a){}
      (7)(since C++20)
      template<class SAlloc>

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

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

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

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

      basic_stringstream(const StringViewLike& t,

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

      Constructs new string stream.

      Given

      thestd::basic_iostream base and theexposition-only data membersb are initialized as follows.

      Over
       load 
      std::basic_iostream basesb
      (1)base_type(std::addressof(sb))[1]buf_type(mode)
      (2)buf_type(std::ios_base::in|std::ios_base::out)
      (3)buf_type(str, mode)
      (4)buf_type(std::move(str), mode)
      (5)buf_type(mode, a)
      (6)buf_type(str, mode, a)
      (7)buf_type(str,std::ios_base::in|std::ios_base::out, a)
      (8)buf_type(str, mode)
      (9)std::addressof(sb){t, mode, Allocator()}
      (10){t, mode, a}
      (11){t,std::ios_base::in|std::ios_base::out, a}
      (12)move constructed fromother'sstd::basic_iostream basemove constructed fromother.sb
      1. Thestd::basic_iostream base was intialized withbase_type(&sb) (for overloads(1,3)) until C++11.
      8) This overload participates in overload resolution only ifstd::is_same_v<SAlloc, Allocator> isfalse.
      9-11) These overloads participate in overload resolution only ifstd::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>> istrue.

      Contents

      [edit]Parameters

      str - string to use as initial contents of the string stream
      t - an object (convertible tostd::basic_string_view) to use as initial contents of the string stream
      a - allocator used for allocating the contents of the string stream
      mode - specifies stream open mode. It is aBitmaskType, the following constants are defined:
      Constant Explanation
      app seek to the end of stream before each write
      binary open inbinary mode
      in open for reading
      out open for writing
      trunc discard the contents of the stream when opening
      ate seek to the end of stream immediately after open
      noreplace(C++23) open in exclusive mode
      other - another string stream to use as source

      [edit]Notes

      Construction of one-offbasic_stringstream objects in a tight loop, such as when used for string conversion, may be significantly more costly than callingstr() to reuse the same object.

      Feature-test macroValueStdFeature
      __cpp_lib_sstream_from_string_view202306L(C++26)Interfacingstd::stringstreams withstd::string_view,(9-11)

      [edit]Example

      Run this code
      #include <iostream>#include <sstream> int main(){// default constructor (input/output stream)std::stringstream buf1;    buf1<<7;int n=0;    buf1>> n;std::cout<<"buf1 = "<< buf1.str()<<" n = "<< n<<'\n'; // input streamstd::istringstream inbuf("-10");    inbuf>> n;std::cout<<"n = "<< n<<'\n'; // output stream in append mode (C++11)std::ostringstream buf2("test",std::ios_base::ate);    buf2<<'1';std::cout<< buf2.str()<<'\n';}

      Output:

      buf1 = 7 n = 7n = -10test1

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      P0935R0C++11the default constructor was explicitmade implicit

      [edit]See also

      gets or sets the contents of underlying string device object
      (public member function)[edit]
      constructs abasic_stringbuf object
      (public member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_stringstream/basic_stringstream&oldid=116183"

      [8]ページ先頭

      ©2009-2025 Movatter.jp