문제

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?

도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top