Why this boilerplate code?

I have been preparing for competitive programming for a while now, and I have been looking at a lot of solutions that people have submitted to problems. One thing I have noticed is that most submissions which take low time to execute have weird looking boilerplate code at the start. This is a good example of what I am talking about.

If anyone feels too lazy to click on that link, then here you go:

#include 
#include 
#include 
using namespace std;

template
using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
#define ll long long int
#define el "\n"
#define sqr(x) ((x) * (x))
#define all(vec) (vec).begin(),(vec).end()
template  inline void ckmax(T &x, T y) {if (y > x) x = y; }
template  inline void ckmin(T &x, T y) {if (y < x) x = y; }
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator _it(_ss); err(_it, args); }
void err(istream_iterator it) {}
template
void err(istream_iterator it, T a, Args... args) {
	cerr << *it << " =: " << a << endl;
	err(++it, args...);
}
template 
inline std::ostream& operator << (std::ostream& os, const std::pair& p) {
    return os << "(" << p.first << ": " << p.second << ")";
}
template
inline std::ostream &operator << (std::ostream & os,const std::vector& v) {
    bool first = true;
    os << "[";
    for(unsigned int i = 0; i < v.size(); i++) {
        if(!first) os << ", ";
        os << v[i];
        first = false;
    }
    return os << "]";
}
template
inline std::ostream &operator << (std::ostream & os,const std::set& v) {
    bool first = true;
    os << "{";
    for (typename std::set::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
        if(!first) os << ", ";
        os << *ii;
        first = false;
    }
    return os << "}";
}
template
inline std::ostream &operator << (std::ostream & os,const std::map& v) {
    bool first = true;
    os << "{";
    for (typename std::map::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
        if(!first) os << ", ";
        os << *ii ;
        first = false;
    }
    return os << "}";
}
template
inline std::ostream &operator << (std::ostream & os,const std::unordered_set& v) {
    return os << std::set(v.begin(), v.end());
}
template
inline std::ostream &operator << (std::ostream & os,const ordered_set& v) {
    return os << std::set(v.begin(), v.end());
}
template
inline std::ostream &operator << (std::ostream & os,const std::unordered_map& v) {
    return os << std::map(v.begin(), v.end());
}

const int MOD = 1e9 + 7;
const long long INF = 1e18;
const double EPS = 1e-6;

To me, it looks like a whole lot of mumbo-jumbo, probably because I am not too familiar with C++. Could anybody please explain, if not line-by-line, then at least on an overall how each statement or preprocessor directive benefits the execution of the program.

Those #include’s are for importing policy based data structures you can read about here.
Then, they are a bunch of #define’s to save some extra keystrokes.
The rest part is the boilerplate code, which helps (a lot!) in debugging.
You can print vectors, sets, maps, etc, using cout with these codes.

In short, if you just want to increase I/O speed of your C++ program, these 2 lines are enough for that.

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // you code
}

Thanks a lot for your answer! What do these lines do?
Also, do you have any resources which explain how the lines that help in debugging work?

Also, I just realised that the solution I linked to was yours :stuck_out_tongue:
Great job on the program man!
What resources did you use to learn competetive programming?
Sorry if I am going off-topic :slight_smile:

also use
cout.tie(NULL); // if cout are bottle neck in your code

No. cin.tie(NULL) is enough. If cin is not tied to cout then cout is not tied to cin, and so cout.tie(NULL) does nothing.

1 Like

You don’t need cout.tie(NULL).