Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strstream::strstream

      From cppreference.com
      <cpp‎ |io‎ |strstream
       
       
       
       
      strstream();
      (1)(deprecated in C++98)
      (removed in C++26)
      strstream(char* s,int n,std::ios_base::openmode mode=
                                     std::ios_base::in|std::ios_base::out);
      (2)(deprecated in C++98)
      (removed in C++26)

      Constructs new input/output strstream and its underlyingstd::strstreambuf.

      1) Default-constructs the underlyingstd::strstreambuf, which creates a dynamically growing buffer, and initializes the base class with the address of the strstreambuf member.
      2) Initialized the base class with the address of the underlyingstd::strstreambuf member, which is initialized in one of the two possible ways, both of which use a user-provided fixed-size array:
      a) if(mode& app)==0 (theapp bit is not set inmode), constructs the buffer by callingstrstreambuf(s, n, s). The behavior is undefined if there are less thann elements in the array whose first element is pointed to bys.
      b) if(mode& app)!=0 (theapp bit is set inmode), constructs the buffer by callingstrstreambuf(s, n, s+std::strlen(s)). The behavior is undefined if there are less thann elements in the array whose first element is pointed to bys or if the array does not contain a valid null-terminated character sequence.

      Contents

      [edit]Parameters

      s -char array to use as the output buffer
      n - size of the array to be used for output
      mode - specifies stream open mode. It is a bitmask type, the following constants are defined (although onlyapp is used):
      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

      [edit]Example

      Run this code
      #include <iostream>#include <string>#include <strstream> int main(){// dynamic bufferstd::strstream s1;    s1<<1<<' '<<3.14<<" example"<<std::ends;std::cout<<"Buffer holds: '"<< s1.str()<<"'\n";    s1.freeze(false); int n;double d;std::string w;    s1>> n>> d>> w;std::cout<<"Read back: n = "<< n<<", d = "<< d<<", w = '"<< w<<"'\n"; // static bufferchar arr[20]="-1 -3.14 ";std::strstream s2(arr, sizeof arr,std::ios_base::app);    s2<<"another"<<std::ends;std::cout<<"Buffer holds: '"<< s2.str()<<"'\n";    s2>> n>> d>> w;std::cout<<"Read back: n = "<< n<<", d = "<< d<<", w = '"<< w<<"'\n";}

      Output:

      Buffer holds: '1 3.14 example'Read back: n = 1, d = 3.14, w = 'example'Buffer holds: '-1 -3.14 another'Read back: n = -1, d = -3.14, w = 'another'

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 115C++98overload (2) only considered the case
      mode& app==0 (== has higher precedence than&)
      considers cases(mode& app)==0
      and(mode& app)!=0

      [edit]See also

      constructs astrstreambuf object
      (public member function ofstd::strstreambuf)[edit]
      constructs anistrstream object, optionally allocating the buffer
      (public member function ofstd::istrstream)[edit]
      constructs anostrstream object, optionally allocating the buffer
      (public member function ofstd::ostrstream)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/strstream/strstream&oldid=170629"

      [8]ページ先頭

      ©2009-2025 Movatter.jp