istringstream用法,见如下代码
#include <iostream> #include"sstream" using namespace std; int main() { istringstream is("I am love C++ and I love C"); string str; //默认是空格或者回车符或者tab,会输出如下内容:I am love C++ and I love C while (is>>str){ cout<<str<<" "; } cout<<endl; istringstream istringstream1("30+2*2+3-"); long ans; char op; istringstream1>>ans; //会得到如下内容 30 + 2 * 2 + 3 - while (istringstream1 >> op) { cout<<ans<<" "<<op<<" "; istringstream1>>ans; } cout<<endl; istringstream istringstream2("20q230r4"); istringstream2>>ans; //会得到如下内容 20 q 230 r while (istringstream2>>op){ cout<<ans<<" "<<op<<" "; istringstream2>>ans; } return 0; }