【发布时间】:2014-01-13 16:46:45
【问题描述】:
以下是我的代码: 我无法在文件算法中使用反向来反转字符串
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
ifstream fp;
string line;
fp.open("list");
if(!fp.is_open())
{
cerr<<"file not open";
}
while(!fp.eof())
{
getline(fp,line);
cout<<line<<end;
std::reverse(line.begin(),line.end());
}
}
我得到的编译错误是:
file.cpp: In function ‘int main()’:
file.cpp:21:15: error: ‘end’ was not declared in this scope
【问题讨论】:
-
是
endl,不是end。 -
问题出在上面那行的
end。投票结束是一个由拼写错误引起的问题。 -
这是一个很好的例子,说明为什么我不喜欢有
using子句,所以我最终改用std::endl。 -
或者,更好的是,
'\n'。每行后刷新可能会减慢速度。 -
Aside:永远不要使用
.eof()或.good()作为循环条件。这样做通常会产生错误代码,就像这里一样。请改用while ( getline(fp, line) ) { ... }。见:stackoverflow.com/questions/5605125/…