The C++ std::stringstream::operator=() function is a assignment operator that allows to assign the contents one stringstream object to another. This operator copies the state of one stringstream to another, including its buffer content, error state and formatting settings.
This function has 2 polymorphic variants: with using the copy assignment operator and move assignment operator (you can find the syntaxes of all the variants below).
SyntaxFollowing is the syntax for std::stringstream::operator=() function.
stringstream& operator= (const stringstream&) = delete; or stringstream& operator= (stringstream&& rhs);Parameters
This function returns the *this.
ExceptionsThis function never throws exceptions.
Data racesModifies both stream objects (*this and rhs).
ExampleIn the following example, we are going to use the basic usage of the copy assignment operator.
#include <iostream> #include <sstream> int main() { std::stringstream a; a << "Tutorialspoint"; std::stringstream b; b.str(a.str()); std::cout << b.str() << std::endl; return 0; }Output
Output of the above code is as follows −
TutorialspointExample
Consider the following example, where we are going to reuse the stream with new assignment.
#include <iostream> #include <sstream> int main() { std::stringstream a; a << "Hello"; std::stringstream b; b.str(a.str()); a.str(""); a << "Namaste"; std::cout << "b: " << b.str() << std::endl; std::cout << "a: " << a.str() << std::endl; return 0; }Output
Following is the output of the above code −
b: Hello a: Namaste
istream.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4