The operations affect the status of the stringstream. After the first input ss>>n; the eof flag is set. Subsequent operations on the same stream will fail.
#include <iostream>
#include <sstream>
usingnamespace std;
int main()
{
stringstream ss;
int n;
ss << "12";
ss >> n;
cout << n << endl;
ss.clear(); // reset the status flags
ss.str(""); // set empty string
ss << "45";
ss >> n;
cout << n << endl;
}
#include <iostream>
#include <sstream>
int main()
{
{
std::stringstream stm ;
stm << "12 " ; // note: space after the last character
int n ;
stm >> n ; // the stream is not at eof after this
std::cout << n << '\n' ; // 12
stm << "45" ;
stm >> n ;
std::cout << n << '\n' ; // 45
}
{
// initialise with a stringbuf of size 4 characters
std::stringstream stm( std::string( 4, ' ' ) ) ;
stm << "12" ; // note: no space after the last character
int n ;
stm >> n ; // the stream is not at eof after this
std::cout << n << '\n' ; // 12
stm << "45" ;
stm >> n ;
std::cout << n << '\n' ; // 45
}
}