Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::basic_...gstream::str(3) C++ Standard Libarystd::basic_...gstream::str(3)

NAME
       std::basic_ostringstream::str - std::basic_ostringstream::str

Synopsis
	  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  )
       (2) (since C++20)
	  const;
	  std::basic_string<CharT,Traits,Allocator> str()  &&;		   (1)
       (3) (since C++20)
	  void	 str(	const	std::basic_string<CharT,Traits,Allocator>&   s
       (4)
	  );
	  template<class					       SAlloc>
       (5) (since C++20)
	  void str( const std::basic_string<CharT,Traits, SAlloc>& s );
	  void	  str(	  std::basic_string<CharT,Traits,Allocator>&&	s   );
       (6) (since C++20)

	  Manages the contents of the underlying string	object.

	  1) Returns a copy of the underlying string. Equivalent to return rd-
       buf()->str();.
	  2) Returns a copy of the underlying string, using  a	as  allocator.
       Equivalent to
	  return rdbuf()->str(a);.
	  3)  Returns  a  string  move-constructed from	the underlying string.
       Equivalent to
	  return std::move(*rdbuf()).str();.
	  4-5) Replaces	the contents of	the underlying string.	Equivalent  to
       rdbuf()->str(s);.
	  6) Replaces the contents of the underlying string. Equivalent	to
	  rdbuf()->str(std::move(s));.

Parameters
	  s - new contents of the underlying string
	  a - allocator	used to	construct the returned string

Return value
	  1-2) a copy of the underlying	string object.
	  3) a string move-constructed from the	underlying string object.
	  4-6) (none)

Notes
	  The copy of the underlying string returned by	str is a temporary ob-
       ject that will be
	  destructed at	the end	of the expression, so directly calling c_str()
       on the result
	  of  str() (for example in auto *ptr =	out.str().c_str();) results in
       a dangling
	  pointer.

Example
       // Run this code

	#include <sstream>
	#include <iostream>
	int main()
	{
	    int	n;

	    std::istringstream in;  // could also use in("1 2")
	    in.str("1 2");
	    in >> n;
	    std::cout << "after	reading	the first int from \"1 2\", the	int is
       "
		      << n << ", str() = \"" <<	in.str() << "\"\n";

	    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);
	    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"

See also
	  rdbuf	returns	the underlying raw string device object
		(public	member function)
	  str	replaces or obtains a copy of the associated character string
		(public	    member     function	    of	    std::basic_string-
       buf<CharT,Traits,Allocator>)

http://cppreference.com		  2022.07.31	 std::basic_...gstream::str(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::wostringstream::str&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help