Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
Next article icon

Some useful C++ tricks for beginners in Competitive Programming

Last Updated : 06 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are some of the basic C++ tricks that every beginner in Competitive Programming should follow for increased speed. However, competitive programming can only be mastered with time and lots of practice. These tricks will just help you to save a little time during the contests which sometimes matters a lot. 

 

  • Using the auto keyword to declare data types can save a lot of time during programming contests. 

    When a variable is defined as auto, the compiler can be determining the datatype on its own. 
    Example: 

auto a = 100; // a will become 'int' auto b = 1LL; // b will become 'long long' auto c = 1.0; // c will become 'double' auto d = "variable"; // d will become 'const char*'
  • The watch macro is one of the most useful tricks ever. 
#define watch(x) cout << (#x) << " is " << (x) << endl
  • If you’re debugging your code, watch(variable); will print the name of the variable and its value. (It’s possible because it’s build in preprocessing time.)
  • Using typedef’s can save a lot of time of yours which you might spend re-writing the same snippet again and again. 
    Example:
typedef long long ll; typedef pair w; typedef vector va; typedef vector vb; typedef vector vc;

Use of C++ STL

The C++ STL is a very rich library consisting of useful functions, which are super useful and time-saving during contests. For using these functions you need to include a header file. 

#include <algorithm>

We are going to discuss the most commonly used STL algorithmic functions below:-

// for binary search in containers like vector (let target element=6) binary_search(v.begin(), v.end(), 6);  // return 1 or 0 as present or not  // max/min of two numbers ans = max(a,b); ans = min(a,b);  // max/min of three numbers ans = max(a,max(b,c)); ans = min(a, min(b,c));  // swap two numbers swap(a,b);  // reverse containers like vectors, strings reverse(v.begin(), v.end());  // rotate containers like vector, strings by n position rotate(v.begin(), v.begin()+n, v.end());  // sort arrays of size n sort(arr, arr+n);  // sort containers like vector, strings(based on intro sort) sort(v.begin(), v.end());
  • There’s an inbuilt function to evaluate the greatest common divisor of two numbers. It’s called __gcd() and it’s present in the algorithm header file. To read more about it, refer: https://www.geeksforgeeks.org/stdgcd-c-inbuilt-function-finding-gcd/
  • Using “\n” for adding new line breaks is much faster than using “endl”.
  • If you use ios::sync_with_stdio(false) at the beginning of your code, you’ll make cin and cout as fast as printf and scanf, but you’ll no longer be able to use neither printf nor scanf.


Next Article
Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
author
priyamkakati
Improve
Article Tags :
  • Competitive Programming
  • DSA

Similar Reads

  • Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
    This article is a collection of various tips that would help beginners of Competitive programming to get an insight of things that should or shouldn’t be done. Competitive programming can only be improved by “PRACTICE, PRACTICE AND PRACTICE”. Try to solve as many questions you can solve on sites lik
    6 min read
  • C++ tricks for competitive programming (for C++ 11)
    We have discussed some tricks in the below post. In this post, some more tricks are discussed. Writing C/C++ code efficiently in Competitive programming Although, practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an uppe
    5 min read
  • Tips for testing code in Competitive programming
    Testing Coding problems can sometimes become hectic. Here are some tips to use while testing Algorithmic Programming Problems. There are generally four major categories of defects in program: Syntactical ErrorsSemantic ErrorsRun-time Errors / ExceptionLogical ErrorsSyntactical ErrorsSyntactical erro
    15+ min read
  • How to begin with Competitive Programming?
    At the very beginning to competitive programming, barely anyone knows the coding style to be followed. Below is an example to help you understand how problems are crafted in competitive programming. Let us consider below problem statement as an example. Problem Statement: Linear Search: Given an int
    9 min read
  • Which C++ libraries are useful for competitive programming?
    C++ is one of the most recommended languages in competitive programming (please refer our previous article for the reason) C++ STL contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming
    3 min read
  • Some important shortcuts in Competitive Programming
    One of the best ways to save time in competitive programming is by creating snippets. Snippets are reusable pieces of code that can be used easily for program creation or repeated tasks. It not only eases the efforts put into writing any code but also saves time for debugging as you know that is sto
    2 min read
  • Stack for Competitive Programming
    For competitive programming to be successful, efficient data structures and algorithms are essential. The stack is one such tool. In this article, we will examine how stack data structures play an important role in solving problems efficiently during competitive programming challenges. We will explo
    7 min read
  • String Guide for Competitive Programming
    Strings are a sequence of characters, and are one of the most fundamental data structures in Competitive Programming. String problems are very common in competitive programming contests, and can range from simple to very challenging. In this article we are going to discuss about most frequent string
    15 min read
  • What Are The Best Resources For Competitive Programming?
    Gennady Korotkevich, Petr Mitrichev, Adam D'Angelo.... Have you heard the above name ever...?? Let me tell you who they are... The first two people (Gennady Korotkevich, Petr Mitrichev) are popular for being the top competitive programmers in the world and the last one (Adam D'Angelo) is also one of
    9 min read
  • Queue for Competitive Programming
    In competitive programming, a queue is a data structure that is often used to solve problems that involve tasks that need to be completed in a specific order. This article explores the queue data structure and identifies its role as a critical tool for overcoming coding challenges in competitive pro
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences