Share via


basic_stringbuf::str

Sets or gets the text in a string buffer without changing the write position.

basic_string<Elem, Tr, Alloc> str( ) const;
void str(
    const basic_string<Elem, Tr, Alloc>& _Newstr
);

Parameters

  • _Newstr
    The new string.

Return Value

Returns an object of class basic_string<Elem, Tr, Alloc**>,** whose controlled sequence is a copy of the sequence controlled by *this.

Remarks

The first member function returns an object of class basic_string<Elem, Tr, Alloc>, whose controlled sequence is a copy of the sequence controlled by *this. The sequence copied depends on the stored stringbuf mode:

  • If mode & ios_base::out is nonzero and an output buffer exists, the sequence is the entire output buffer (epptr - pbase elements beginning with pbase).

  • If mode & ios_base::in is nonzero and an input buffer exists, the sequence is the entire input buffer (egptr - eback elements beginning with eback).

  • Otherwise, the copied sequence is empty.

The second member function deallocates any sequence currently controlled by *this. It then allocates a copy of the sequence controlled by _Newstr. If mode & ios_base::in is nonzero, it sets the input buffer to start reading at the beginning of the sequence. If mode & ios_base::out is nonzero, it sets the output buffer to start writing at the beginning of the sequence.

Example

// basic_stringbuf_str.cpp
// compile with: /EHsc
#include <iostream>
#include <sstream>

using namespace std;

int main( ) 
{
   basic_string<char> i( "test" );
   stringstream ss;
   
   ss.rdbuf( )->str( i );
   cout << ss.str( ) << endl;

   ss << "z";
   cout << ss.str( ) << endl;
      
   ss.rdbuf( )->str( "be" );
   cout << ss.str( ) << endl;
}
test
zest
be

Requirements

Header: <sstream>

Namespace: std

See Also

Reference

basic_stringbuf Class

iostream Programming

iostreams Conventions