为了使用流操作从一个字符串中读取数据,需要创建istringstream对象,下面的例子说明了如何使用istringsstream对象:
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
istringstream strStream("1000 3.14 Hello world");//create an input string stream using a string
int i;
double f;
strStream >> i >> f;//get a int and a float
assert(i = 1000);
double relErr = (fabs(f) - 3.14)/3.14;
assert(relErr <= numeric_limits<double>::epsilon();
string str;
strStream >> str;//get a string with first word
assert(str == "Hello");
cout << strStream.rdbuf();//print " world"
}