Pregunta

Stringstream is very convenient in C++ to convert between strings and other num types. I am trying to use a stringstream multiple times in a program like this.

stringstream ss;
string s1="stack",s2="overflow";
string s3,s4;
ss<<s1;
ss>>s3;
ss.str("");  
cout<<s3<<endl;

ss<<s2;
ss>>s4;
cout<<s4<<endl;

The program will correctly output "stack", but will not output "overflow". Any explanations for that?

¿Fue útil?

Solución

call ss.clear()

stringstream ss;
string s1="stack",s2="overflow";
string s3,s4;
ss<<s1;
ss>>s3;
ss.str("");  
cout<<s3<<endl;
ss.clear()
ss<<s2;
ss>>s4;
cout<<s4<<endl;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top