从句子中读出以空格分隔的单词
时间:2010-08-16 来源:idx001
记一下:
C++ Primer P318
ifstream input;
string line;
getline(input,line)
istringstream stream(line);//这里即一次读入一个单词
使用istringstream记得头文件sstream否则有以下错误:
`std::istringstream stream' has initializer but incomplete type
string first,last=string("hello word");
istringstream stream(last);
stream>>first;
cout<<first<<endl;//输出hello
从文件中读出亦是一样,可用ifstream流:
const string file="ftest.txt";
string word;
ifstream in;
in.close();
in.clear();
in.open(file.c_str());
while(in>>word){
cout<<word<<endl;
}//while 亦是遇到空格读一次,直至文件结束
C++ Primer P318
ifstream input;
string line;
getline(input,line)
istringstream stream(line);//这里即一次读入一个单词
使用istringstream记得头文件sstream否则有以下错误:
`std::istringstream stream' has initializer but incomplete type
string first,last=string("hello word");
istringstream stream(last);
stream>>first;
cout<<first<<endl;//输出hello
从文件中读出亦是一样,可用ifstream流:
const string file="ftest.txt";
string word;
ifstream in;
in.close();
in.clear();
in.open(file.c_str());
while(in>>word){
cout<<word<<endl;
}//while 亦是遇到空格读一次,直至文件结束
相关阅读 更多 +