rajan published an article 1 month, 3 weeks ago |
There are lot of C++ Hacks you can use to save time in competitive programming. Here are few of them.
#include <bits/stdc++.h>
__gcd(a, b):
Returns the GCD of a and b__builtin_popcount(x)
: Returns the number of set bits in xvector<int> odd_nos = {1, 3, 5, 7, 9};
pair<int, string> p = {1, “Hi”}; //Equiv. to p=make_pair(1, "Hi")
map<int, string> m = { {1, “This”}, {2, “is”}, {3, “awesome”} };
//Long Way:
int max_of_3 = max(a, max(b, c));
int max_of_4 = max(max(a, b), max(c, d));
//Easier Way - Can be extended to any number of variables:
int max_of_3 = max({a, b, c});
int max_of_4 = max({a, b, c, d});
//Old Way:
for(auto it=container.begin(), it!=container.end(); it++)
cout<<*it<<" ";
//Alternatively:
for(int i=0;i<container.size();i++) //If the container is a vector
cout<<container[i]<<" ";
//Easier Way:
for(auto &it:container) //Using & also allows us to modify the elements
cout<<it<<" ";
//Initializes a with -1, b with 1, etc
tie(a, b, c, d) = make_tuple(-1, 1, -2, 2);
//x, y can be two integers, or two vectors, or any two containers
swap(x, y);
push_back
again and again, you can just use #define pb push_back
, and type pb
in your code.#define trace(x) cerr<<#x<<": "<<x<<" "<<endl;
int main()
{
int a=5;
trace(a); //prints: "a: 5" to stderr stream
}
arrow_upward Upvote |
Comment |
arrow_downward downvote |
HackersFriend Updates
Events
Python from zero to hero
place Bangalore ( HackersFriend office BTM Layout)