Monday 18 February 2019

Tips and C++ template

C++ template

My C++ template :

#include <iostream>
using namespace std;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << 4 << '\n';
    return 0;
}
  • Line 1 : the basic include of C++
  • Line 2 : to remove all std:: for exemple you don't need to write std::cin >> a; anymore you just need to write cin >> a;
  • Line 4 : the main function, inside of it you put your code.
  • Line 5 : optimisation. You could also use ios::base_with_stdio(0);
  • Line 6 : optimisation for reading input
  • Line 7 : optimisation for printing output
  • Line 8 : prints 4 with a new line. '\n' is faster than endl
  • Line 9 : for safety we return 0 at the end
Extra :

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")

#pragma GCC optimize("O3")

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using indexed_set = tree<int, null_type, less<inr>, rb_tree_tag, tree_order_statistics_node_update>;

// using indexed set
indexed_set s;
s.insert(2);
// insert 2
auto x = s.find_by_order(0);
// returns iterator of the element position 2
cout << *x << '\n';
// prints 2
cout << s.order_of_key(2) << '\n'; // 0
// prints the position of 2
  • The first part includes everything. All the includes. We recommand it but for exemple on a MacBook you need to install some stuff to use this.
  • This can do optimisation if g++ didn't optimise for you.
  • This can also do optimisation if g++ didn't optimise for you.
  • This is the tings to include if you want to use a indexed_set
  • This is operations you can use for a indexed_set .order_of_key(2) and .find_by_order(0); are O(log(n)). The next post will be talking about the big O notation if you don't already know what this means.

No comments:

Post a Comment

std::next_permutation(arr.begin(),arr.end());

What is next_permutation(a.begin(),a.end()) ? Next permutation is like what its name says, finds the next permutation of a and assigns ...