online-judge-tools / template-generator

Analyze problems of competitive programming and automatically generate boilerplate
MIT License
86 stars 11 forks source link

Replace endl (!= std::endl) with '\n' in the generated C++ code #67

Closed kmyk closed 3 years ago

kmyk commented 3 years ago

Description

std::endl を使うと TLE になりがち (e.g. Codeforces) です。これは std::endl が改行文字 ('\n') を出力するだけでなく出力の flush をしてしまうことに起因します。 https://en.cppreference.com/w/cpp/io/manip/endl これを防ぐため、現状の実装では std::endl は使わず、endl という名前のローカル変数を利用しています。 https://github.com/online-judge-tools/template-generator/blob/dd4d54356445ee0e01d1557e9715848eab5d553b/onlinejudge_template_resources/template/main.cpp#L65 しかしこのトリックは分かりにくいものである (例: https://tsutaj.hatenablog.com/entry/2020/10/24/112331) ため、単純に '\n' を使うようにした方がよい気がする (たぶん)。

Possible implementations and the analysis

選択肢は

ということで「char endl = '\n' を書いた上で '\n' を使う」 が最良だろうか。

このあたりをカスタマイズする方法がないのも問題なのだけど、面倒なのでカスタマイズについてはとりあえず後回しでいきたい。