轉眼到了畢業季,大家都在忙著找暑期實習;我也投了一個,是阿里巴巴的暑期實習;實習,少不了機試,又想起了大一時曾經湊過acm的熱鬧;當時學到一個技巧,是使用重定向向輸入輸出函式,這樣在進行測驗的時候就比較方便了;
這樣除錯的時候,不用從控制臺進行手動輸入,直接從檔案中進行輸入就行;這樣也方便debug;
引入頭檔案:
#include <cstdio>
經典使用樣例:(我一般只重定向輸入,還是在標準輸入中輸出)
freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
函式解釋:(可以使用 man freopen 進行查詢)
#include <stdio.h> FILE * freopen(const char *path, const char *mode, FILE *stream);
The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen() function. If the path argument is NULL, freopen() attempts to re-open the file associated with stream with a new mode. The new mode must be compatible with the mode that the stream was originally opened with: Streams open for reading can only be re-opened for reading, streams open for writing can only be re-opened for writing, and streams open for reading and writing can be re-opened in any mode. The ``x'' mode option is not meaningful in this context. The primary use of the freopen() function is to change the file associated with a standard text stream (stderr, stdin, or stdout).
另外一個技巧就是,c++的萬能頭:
#include<bits/stdc++.h>
部分平臺,不支持此頭檔案;經過我的測驗,macos 的clang++ 11.0.0 好像不支持這個檔案;但是g++ 9.3.0通過我的測驗,好像支持這個檔案;
保持更新,轉載請注明出處;更多內容請關注cnblogs.com/xuyaowen;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/52941.html
標籤:C++
