【C++】A trick I learned:put boilerplate code into constructor of a struct

百般思念 提交于 2019-12-03 13:21:35

I learned this trick from hitonanode's submission on AtCoder.

The trick is like

struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

What I used to do is like

#define FAST_READ ios::sync_with_stdio(false); cin.tie(nullptr);
int main() {
    FAST_READ
    cout << fixed << setprecision(10);
    // ...
}

using this trick, the code becomes

struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }; } fast_ios_;
int main() {
    // ...
}

I think macros are better avoided when alternatives are available.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!