查看“︁03-进阶基础/07-文件读写与高级IO”︁的源代码
←
03-进阶基础/07-文件读写与高级IO
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 文件输入输出(<code>freopen</code>) == NOI 系列赛事通常要求使用文件输入输出: <syntaxhighlight lang="cpp"> #include <bits/stdc++.h> using namespace std; int main() { freopen("problem.in", "r", stdin); freopen("problem.out", "w", stdout); // 正常使用 cin / cout 即可 int x; cin >> x; cout << x * 2 << "\n"; return 0; } </syntaxhighlight> - <code>"r"</code>:read,读模式 - <code>"w"</code>:write,写模式 - 文件名在正式比赛的试卷首页会给出 == 输入输出加速 == <syntaxhighlight lang="cpp"> ios::sync_with_stdio(false); cin.tie(0); </syntaxhighlight> 加上这两行后,<code>cin</code>/<code>cout</code> 的速度接近 <code>scanf</code>/<code>printf</code>。 '''注意''':加速后不要混用 <code>cin</code>/<code>cout</code> 和 <code>scanf</code>/<code>printf</code>,否则会导致输出顺序混乱。 == 读入到文件末尾 == 当题目没有告诉具体有多少输入时,读入到 EOF: <syntaxhighlight lang="cpp"> // 方法一 int x; while (cin >> x) { // 处理 x } // 方法二(读取一行) string s; while (getline(cin, s)) { // 处理 s } </syntaxhighlight> == 保留小数位数 == <syntaxhighlight lang="cpp"> #include <iomanip> cout << fixed << setprecision(3) << 1.0 / 3; // 输出 0.333 </syntaxhighlight> == 完整加速框架 == <syntaxhighlight lang="cpp"> #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); // 你的代码 return 0; } </syntaxhighlight> == 带文件 IO 的完整框架 == <syntaxhighlight lang="cpp"> #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); freopen("problem.in", "r", stdin); freopen("problem.out", "w", stdout); // 你的代码 return 0; } </syntaxhighlight> [[Category:进阶基础]] [[Category:三三文档]]
返回
03-进阶基础/07-文件读写与高级IO
。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
特殊页面
工具
链入页面
相关更改
页面信息