How to insert a line at the beginning of a line

For example, only, not the actual code:

stringstream ss; ss << " world!"; string hello("Hello"); // insert hello to beginning of ss ?? 

Thanks for all the answers, I also found this code that works:

 ostringstream& insert( ostringstream& oss, const string& s ) { streamsize pos = oss.tellp(); oss.str( s + oss.str() ); oss.seekp( pos + s.length() ); return oss; } 
+4
source share
3 answers

You cannot do this without making a copy. One of the methods:

 stringstream ss; ss << " world!"; const string &temp = ss.str(); ss.seekp(0); ss << "Hello"; ss << temp; 
+3
source

the only way i see is to create a string from a stream and prefix your other string

 string result = hello + ss.str(); 

it is called a stream for some reason.

0
source

Assuming ss1 contains hi

 ss1 << ss.rdbuf(); 

or

 ss1 << "hello" << ss; 

Refer to this URL for more information: -

stringstream

0
source

All Articles