CPP (for CP)
Table of Contents
CPP (for CP)
- Basic syntax and prior C knowledge is assumed
Tips
include bits/stdc++.h
can save time including specific header files.
Basic data types
- data types (int, char, float, double, bool)
- long int and long long int
- Size of data types:
#include <iostream> int main() { int a; long int b; long long int c; std::cout << sizeof(a) << std::endl; std::cout << sizeof(b) << std::endl; std::cout << sizeof(c) << std::endl; }
std::string
for working with strings in cpp gives trivial features like concat (+), comparison (==) and,size()
etc.std::getline(istream, str)
can be used to get a single line input
- Use arrays for storing collection of items of same data type.
- Array size is limited.
- For CP, local array size shouldn't be more than 105 ~int arr[1e5].
Ranges (rough estimation)
- -109 < int < 109
- -1012 < long int < 1012
- -1018 < long long int < 1018
- header file
climits
has accurate declared limits - sizeof double is higher than long int but it's not used where precise calculation is required.
#include <iostream> #include <climits> int main() { std::cout << INT_MIN << " < int < " << INT_MAX << std::endl; }
Calculation precedence (increasing -> decreasing)
Types lower in this list will be converted to higher data types in expressions and outputs.
- double
- float
- long long int
- long int
- int
- char
#include <iostream> int main() { std::cout << 7 / 2 << std::endl; // outputs int std::cout << 7 / 2.0 << std::endl; // outputs float std::cout << 'a' + 1 << std::endl; // outputs int (ascii values) }
Operator precedence and associativity
Category | Operator | Associativity |
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | = ! |
Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
CPP References
References can be used to ignore variable copying when calling functions. This does not require any use of pointers.
Example:
#include <iostream> // swap takes in references a and b void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int a = 4, b = 3; std::cout << a << " " << b << std::endl; // this utility function is already declared in <algorithm> header file swap(a,b); std::cout << a << " " << b << std::endl; }
Short long long double
#include <iostream> #include <climits> using namespace std; int main () { cout << sizeof(int) << endl; cout << sizeof(char) << endl; cout << sizeof(float) << endl; cout << sizeof(double) << endl; short a = 3; long int b = 69; long long int c = 69; long double d = 2.0; cout << a << endl; cout << sizeof(a) << endl; cout << sizeof(b) << endl; cout << sizeof(c) << endl; cout << sizeof(d) << endl; }