网络上经常可以看到各种文件输入输出方式速度的比较,但却很少看到 fstream 的方式(即 ifstream 和 ofstream),不过我感觉这种方式更快啊。
用 fstream 写的文件输入输出环境下的 A+B Problem 示例:
#include <fstream>
using namespace std;
ifstream fin("a+b.in");
ofstream fout("a+b.out");
int a,b;
int main(){
fin>>a>>b;
fout<<a+b<<endl;
return 0;
}
虽然看起来像 cin、cout,但似乎比快读快写都要快很多。