12345678
int main() { std::stringstream sstream; sstream << "foo"; sstream.seekp(0); sstream << "x"; std::cout << sstream.str() << std::endl; }
12
sstream.seekp(0); sstream << sstream.str().insert(0, "bar");
12345678910111213
#include <iostream> #include <sstream> #include <string> int main() { std::ostringstream oss("foo"); const auto s {oss.str()}; oss.seekp(0); oss << "bar" << s; std::cout << oss.str() << '\n'; }
barfoo
1234567891011121314151617181920212223
#include <iostream> #include <sstream> #include <string> std::istringstream& push_front( std::istringstream& sstm, const std::string& str ) { sstm.str( str + sstm.str() ) ; sstm.seekg(0) ; return sstm ; } int main() { std::istringstream sstm ( "five six seven" ); std::string str ; sstm >> str ; // five std::cout << str << '\n' ; for( std::string temp : { "zero ", "one ", "two ", "three " } ) push_front( sstm, temp ) ; while( sstm >> str ) std::cout << str << ' ' ; // three two one zero five six seven std::cout << '\n' ; }