#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
ifstream infile1;
infile1.open("d:\\test.txt");
string sLine, sWord;
while (getline(infile1, sLine))
{
istringstream iss(sLine);
while (iss >> sWord)
{
//1.读取每一行的每个单词,并输出
cout<<sWord.c_str()<<endl;
}
}
infile1.close();
//2.格式化输入输出
int val1 = 512, val2 = 1024;
int val3, val4;
ostringstream oss;
oss<<"val1: "<<val1<<",val2: "<<val2;//要有空格符
string str;
istringstream iss(oss.str());
iss>>str>>val3>>str>>val4;
cout<<"val3:"<<val3<<",val4:"<<val4<<endl;
//3.类型转换(再进行多次转换的时候,必须调用stringstream的成员函数clear())
string s1 = "1000";
int n1, n2;
stringstream ss;
ss<<s1;
ss>>n1;
ss.clear();
ss<<true;
ss>>n2;
cout<<n1<<", "<<n2<<endl;
return 0;
}
stringstream的用法
最新推荐文章于 2022-02-09 17:30:29 发布