第二十一章流 15sstream字符串输入输出流类

//第二十一章流 15sstream字符串输入输出流类
/*#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	ostringstream out;
	//ostringstream 类从ostream类派生而来,因此ostringstream类对像有cout对像的特征
	char *str = "hello world";
	float num=314.57f;
	out.precision(2);
	out<<fixed;
	cout<<num<<str<<endl;
	string ch=out.str();
	cout<<ch;
    return 0;
}
*/
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	//当然也可以将while(in>>ch)替换为while(in.get(ch)),这样,输出的结果就变为:
	//Next morning I will stay at home
	string str="Next morning I will stay at home.";
	istringstream in(str);
	string ch;
	while(in>>ch)
	{
	    cout<<ch<<endl;
	}
	//总之 istringstream 对像和ostringsstrem对像可以使用ifstream类和ofstream类的方法来管理字符串对像的数据
    return 0;
}

  

posted @ 2012-10-01 14:03  简单--生活  阅读(168)  评论(0编辑  收藏  举报
简单--生活(CSDN)