All Projects → rachitiitr → modern-cpp-tricks

rachitiitr / modern-cpp-tricks

Licence: other
Modern CPP Tricks often useful in Coding Interviews and Competitive Programming

Projects that are alternatives of or similar to modern-cpp-tricks

Technical Interview Guide
My learning material for technical interviews!
Stars: ✭ 76 (-85.93%)
Mutual labels:  competitive-programming, coding-interviews
Leetcode
Leetcode solutions
Stars: ✭ 2,894 (+435.93%)
Mutual labels:  competitive-programming, coding-interviews
ds-algo-placement-resources
A complete roadmap and resources for competitive programming for placement purpose.
Stars: ✭ 58 (-89.26%)
Mutual labels:  competitive-programming, coding-interviews
Interview Techdev Guide
This repository contains curated technical interview questions by fn+geeks community
Stars: ✭ 252 (-53.33%)
Mutual labels:  competitive-programming, coding-interviews
Dreamjob
DreamJob
Stars: ✭ 152 (-71.85%)
Mutual labels:  competitive-programming, coding-interviews
Competitiveprogrammingquestionbank
This repository contains all the popular competitive programming and DSA questions with solutions.
Stars: ✭ 122 (-77.41%)
Mutual labels:  competitive-programming, coding-interviews
from-software-developer-to-engineer
My journey from a software developer to a software engineer.
Stars: ✭ 17 (-96.85%)
Mutual labels:  competitive-programming, coding-interviews
Data structure and algorithms library
A collection of classical algorithms and data-structures implementation in C++ for coding interview and competitive programming
Stars: ✭ 133 (-75.37%)
Mutual labels:  competitive-programming, coding-interviews
Tech Interview Preparation
A one stop solution to ace your next coding interview 👨‍💻
Stars: ✭ 188 (-65.19%)
Mutual labels:  competitive-programming, coding-interviews
PrepBytes-questions
One can follow to excel in coding if you are a noob!!
Stars: ✭ 46 (-91.48%)
Mutual labels:  competitive-programming, coding-interviews
SeptoCode-21
This September, get ready to beat your brains out for the GDSC Club. Starting from 20th September, all the contributors will be provided with simple programming questions,one per day, which can be written using any programming language of preference. The submissions will be accepted through Google Forms after thorough checking.
Stars: ✭ 20 (-96.3%)
Mutual labels:  competitive-programming
Software-Engineering-Course
List of available free/paid courses,bootcamps available for Coding Interview Prep, SDE Roles
Stars: ✭ 53 (-90.19%)
Mutual labels:  coding-interviews
Grokking-the-Coding-Interview-Patterns-for-Coding-Questions
Grokking the Coding Interview: Patterns for Coding Questions Alternative
Stars: ✭ 374 (-30.74%)
Mutual labels:  coding-interviews
codeforces-timer
A Google Chrome extension which adds a timer to practice timed problem solving on codeforces
Stars: ✭ 20 (-96.3%)
Mutual labels:  competitive-programming
USACO
Algorithms, data structures, and problems in competitive programming up to USACO Platinum
Stars: ✭ 34 (-93.7%)
Mutual labels:  competitive-programming
CodePal
A Visual Studio Code Extension for competitive programming.
Stars: ✭ 45 (-91.67%)
Mutual labels:  competitive-programming
test-case-generator
Test Case generator for competitive coding. Test case generator for competitive programming and potentially for software testing.
Stars: ✭ 28 (-94.81%)
Mutual labels:  competitive-programming
fridge
Fridge for Codechef: An android app for downloading and browsing questions and contests from codechef.com offline
Stars: ✭ 17 (-96.85%)
Mutual labels:  competitive-programming
CS-97SI
Solutions to "CS 97SI: Introduction to Competitive Programming Contests" by Stanford University
Stars: ✭ 24 (-95.56%)
Mutual labels:  competitive-programming
algorithms
🎈My notebook and solutions for 300+ problems that I solved during practice for ACM-ICPC
Stars: ✭ 26 (-95.19%)
Mutual labels:  competitive-programming

Introduction

This is the list of modern CPP tricks often used in Coding Interviews and Competitive Programming.
If you like Rachit's work, you can follow at -

Contents:

No more nested min(x, min(y, ...))

Use initializer list and std::min and std::max to make life easy

small = min(x, min(y, min(z, k))); // the old way
small = min({x, y, z, k}); // life is easy

JavaScript like Destructuring using Structured Binding in C++

pair<int, int> cur = {1, 2};
auto [x, y] = cur;
// x is now 1, y is now 2
// no need of cur.first and cur.second

array<int, 3> arr = {1, 0, -1};
auto [a, b, c] = arr;
// a is now 1, b is now 0, c is now -1

Powerful Logging and Debugging

How debug macros work?

Straight to the point, I have often used the debug macro which stringifies the variable names and their values.

#define deb(x) cout << #x << " " << x 
int ten = 10;
deb(ten); // prints "ten = 10"

This is often useful in debugging.

The Problem with this macro - its not scalable

However, when you have multiple variables to log, you end up with more deb2 and deb3 macros.

#define deb(x) cout << #x << " " << x 
#define deb2(x) cout << #x << " " << x << " "  << #y << " " << y 
#define deb3(x, y, z) cout << #x << " " << x << " "  << #y << " " << y << " "  << #z << " " << z 

This is not scalable.

Solution using a powerful macro

Here is the solution using variadic macros and fold expressions,

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
    cout << vars << " = ";
    string delim = "";
    (..., (cout << delim << values, delim = ", "));
}

int xx = 3, yy = 10, xxyy = 103;
deb(xx); // prints "xx = 3"
deb(xx, yy, xxyy); // prints "xx, yy, xxyy = 3, 10, 103"

Generic Reader and Writer for multiple variables and containers

template <typename... T>
void read(T &...args) {
    ((cin >> args), ...);
}

template <typename... T>
void write(string delimiter, T &&...args) {
    ((cout << args << delimiter), ...);
}

template <typename T>
void readContainer(T &t) {
    for (auto &e : t) {
        read(e);
    }
}

template <typename T>
void writeContainer(string delimiter, T &t) {
    for (const auto &e : t) {
        write(delimiter, e);
    }
    write("\n");
}

Usage

// Question: read three space seprated integers and print them in different lines.
	int x, y, z;
	read(x, y, z);
	write("\n", x, y, z);
	
// even works with variable data types :)
	int n;
	string s;
	read(n, s);
	write(" ", s, "has length", n, "\n");
	
// Question: read an array of `N` integers and print it to the output console.
	int N;
	read(N);
	vector<int> arr(N);
	readContainer(arr);
	writeContainer(" ", arr); // output: arr[0] arr[1] arr[2] ... arr[N - 1]
	writeContainer("\n", arr);
	/**
	* output:
	* arr[0]
	* arr[1]
	* arr[2]
	* ...
	* ...
	* ...
	* arr[N - 1]
	*/

Decorators in C++ and Multiple Parameters

Live Demo on YouTube

Printing as many variables in one line

template<typename ...T>
void printer(T&&... args) {
    ((cout << args << " "), ...);
}

int age = 25;
string name = "Rachit";
printer("I am", name, ',', age, "years old"); 
// ^ This prints the following
// I am Rachit, 25 years old

Powerful decorator functions in C++

template<typename F>
auto debug_func(const F& func) {
    return [func](auto &&...args) { // forward reference
        cout << "input = ";
        printer(args...);
        auto res = func(forward<decltype(args)>(args)...);
        cout << "res = " << res << endl;
        return res;
    };
}

debug_func(pow)(2, 3);
// ^ this automatically prints
// input = 2 3 res = 8

Exploiting decorators by nesting them

Lets define another decorator beautify as follows.

template<typename F>
auto beautify(const F& func) {
    return [func](auto &&...args) { // forward reference
        cout << "========" << endl;
        func(forward<decltype(args)>(args)...);
        cout << "========" << endl;
    };
}

beautify(debug_func(pow(2, 3)));
// ^ this now prints
// ========
// input = 2 3 res = 8
// ========

Its amazing how much you can do by writing such generic decorators and nest them.
Think about decorators like log_time that calculates the time taken for a given function.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].