Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_stringbuf<CharT,Traits,Allocator>::str

      From cppreference.com
      <cpp‎ |io‎ |basic stringbuf
       
       
       
       
      (1)
      std::basic_string<CharT, Traits, Allocator> str()const;
      (until C++20)
      std::basic_string<CharT, Traits, Allocator> str()const&;
      (since C++20)
      template<class SAlloc>
      std::basic_string<CharT, Traits, SAlloc> str(const SAlloc& a)const;
      (2)(since C++20)
      std::basic_string<CharT, Traits, Allocator> str()&&;
      (3)(since C++20)
      void str(conststd::basic_string<CharT, Traits, Allocator>& s);
      (4)
      template<class SAlloc>
      void str(conststd::basic_string<CharT, Traits, SAlloc>& s);
      (5)(since C++20)
      void str(std::basic_string<CharT, Traits, Allocator>&& s);
      (6)(since C++20)
      template<class StringViewLike>
      void str(const StringViewLike& t);
      (7)(since C++26)

      Gets and sets the underlying string.

      In the descriptions below,buf andmode areexposition-only data members of*this.

      1) Creates and returns astd::basic_string object containing a copy of thisstd::basic_stringbuf's underlying character sequence. For input-only streams, the returned string contains the characters from the range[eback()egptr()). For input/output or output-only streams, contains the characters frompbase() to the last character in the sequence regardless ofegptr() andepptr().
      The member character sequence in a buffer open for writing can be over-allocated for efficiency purposes. In that case, only theinitialized characters are returned: these characters are the ones that were obtained from the string argument of the constructor, the string argument of the most recent call to a setter overload ofstr(), or from a write operation. A typical implementation that uses over-allocation maintains a high-watermark pointer to track the end of the initialized part of the buffer and this overload returns the characters frompbase() to the high-watermark pointer.
      Equivalent toreturnstd::basic_string<CharT, Traits, Allocator>(view(), get_allocator());.
      (since C++20)
      2) Same as(1), except thata is used to construct the returnedstd::basic_string. Equivalent toreturnstd::basic_string<CharT, Traits, SAlloc>(view(), a);.
      This overload participates in overload resolution only ifSAlloc meets the requirements ofAllocator.
      3) Creates astd::basic_string object as if by move constructing it from*this's underlying character sequence inbuf.buf may need to be adjusted to contain the same content as in(1) at first. After that, setsbuf to empty and callsinit_buf_ptrs(), then returns thestd::basic_string object.
      4) Replaces the underlying character sequence as if bybuf= s, then callsinit_buf_ptrs().
      5) Same as(4), except the type ofs's allocator is notAllocator.
      This overload participates in overload resolution only ifstd::is_same_v<SAlloc, Allocator> isfalse.
      6) Replaces the underlying character sequence as if bybuf= std::move(s), then callsinit_buf_ptrs().
      7) Implicitly convertst to a string viewsv as if bystd::basic_string_view<CharT, Traits> sv= t;, then replaces the underlying character sequence as if bybuf= sv, then callsinit_buf_ptrs().
      This overload participates in overload resolution only ifstd::is_convertible_v<const StringViewLike&,
                           std::basic_string_view<CharT, Traits>>
      istrue.

      Contents

      [edit]Parameters

      s - astd::basic_string object holding the replacement character sequence
      t - an object (convertible tostd::basic_string_view) holding the replacement character sequence
      a - allocator to use for all memory allocations of the returnedstd::basic_string

      [edit]Return value

      1-3) Astd::basic_string object holding this buffer's underlying character sequence.
      4-7) (none)

      [edit]Notes

      This function is typically accessed throughstd::basic_istringstream::str(),std::basic_ostringstream::str(), orstd::basic_stringstream::str().

      Feature-test macroValueStdFeature
      __cpp_lib_sstream_from_string_view202306L(C++26)Interfacing string streams withstd::string_view

      [edit]Example

      Run this code
      #include <iostream>#include <sstream> int main(){int n; std::istringstream in;// could also use in("1 2")    in.rdbuf()->str("1 2");// set the get area    in>> n;std::cout<<"after reading the first int from\"1 2\", the int is "<< n<<", str() =\""<< in.rdbuf()->str()<<"\"\n";// or in.str() std::ostringstream out("1 2");    out<<3;std::cout<<"after writing the int '3' to output stream\"1 2\""<<", str() =\""<< out.str()<<"\"\n"; std::ostringstream ate("1 2",std::ios_base::ate);// C++11    ate<<3;std::cout<<"after writing the int '3' to append stream\"1 2\""<<", str() =\""<< ate.str()<<"\"\n";}

      Output:

      after reading the first int from "1 2", the int is 1, str() = "1 2"after writing the int '3' to output stream "1 2", str() = "3 2"after writing the int '3' to append stream "1 2", str() = "1 23"

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 432C++981. overload(1) did not specify the content
      of the underlying character sequence
      2. overload(4) did not specify how the
      input and output sequences are initialized
      both specified
      LWG 562C++98overload(4) setepptr() to point one past the last underlying
      character ifbool(mode&std::ios_base::out)==true
      epptr() can be set
      beyond that position

      [edit]See also

      gets or sets the contents of underlying string device object
      (public member function ofstd::basic_stringstream<CharT,Traits,Allocator>)[edit]
      (C++20)
      obtains a view over the underlying character sequence
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_stringbuf/str&oldid=158933"

      [8]ページ先頭

      ©2009-2025 Movatter.jp