string 流演示

来源:互联网 发布:平湖市行知小学严文婕 编辑:程序博客网 时间:2024/06/17 21:53

/*string 流演示*/

#include <iostream>#include <string>#include <fstream>#include <cassert>#include <cfloat>#include <iomanip>  //包含控制符#include <sstream>using namespace std;int main(){string date="U.S. independence: July 4, 1776";//用string date 创建一个istringstreamistringstream istr(date);    string word1,word2,month;int day,year;char comma;//用输入运算符>>将它分解为单独的符号,其中一些为单词,而其他的为整数istr>>word1>>word2>>month>>day>>comma>>year;  cout<<"Contents of string stream istr, one word per line:\n"<<word1<<'\n'<<word2<<'\n'<<month<<'\n'<<'\n'<<day<<comma<<'\n'<<year<<'\n'<<endl;ofstream outfile("data3.txt");assert(outfile.is_open());ostringstream ostr;    //产生一个格式化的输出到 ostrostr<<word1<<"bicentennial: "<<month<<setw(2)<<day<<", "<<year+200<<endl;//str()创建一个ostream中字符串的拷贝以及一个ofstream中字符串的拷贝cout<<"Contents of string steam ostr:\n"<<ostr.str();outfile<<ostr.str();outfile.close();return 0;}


输出文件:data3.txt

U.S.bicentennial: July 4, 1976

0 0
原创粉丝点击