zoukankan      html  css  js  c++  java
  • 【C++】A trick I learned:put boilerplate code into constructor of a struct

    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.

    Update 2020/5/21
    There is another way to achieve the same function:

    int fast_io = []() {
                cin.tie(0); 
                ios::sync_with_stdio(false); 
                cout << fixed << setprecision(20); 
                return 0;
    }();
    
  • 相关阅读:
    [BZOJ1303][CQOI2009]中位数图
    [BZOJ1192][HNOI2006]鬼谷子的钱袋
    9.5题解
    9.3题解
    9.2题解
    9.1题解
    8.29题解
    8.28题解
    8.23<2>题解
    8.23<1>题解
  • 原文地址:https://www.cnblogs.com/Patt/p/11796583.html
Copyright © 2011-2022 走看看