Big O notation
O(log n) Binary Search
O(n) linear search
O(c) constant time
On^c c bested for loops
On! All permutations
[Link]
C++ Basics:
Code Purpose
int x; Input
std::cin >> x; // Reads input into x
std::cout << "Hello, World!"; // Prints to the Output
console
int a, b;
string x; input:123 456 hehe
cin >> a >> b >> x;
cout<<a<<” “<<b<<” ”<<x<<”\n”; Output 123 456 hehe
Also note: \n is faster than endl Note: Make input and output more efficient
put this at beginning of code
int a, b; Faster but harder to use.
scanf("%d %d", &a, &b);
● Int ("%d"): 32 Bit integer
int a = 123, b = 456;
printf("%d %d\n", a, b); ● Long ("%ld"): 64 bit integer
Read whole line from input: ● Char ("%c"): Character type
string s;
getline(cin, s); ● Float ("%f"): 32 bit real value
Amount of data unknown: read all inputs ● Double ("%lf"): 64 bit real value
until no more
While (cin>>x){
//code
}
Sometimes input and output are files, put at
beginning:
freopen("[Link]", "r", stdin);
freopen("[Link]", "w", stdout);
int age = 25; Data Types: int (integer), float
float height = 5.9; (floating-point number), double
char grade = 'A'; (double-precision float), char (character),
string (text), bool (boolean).
int sum = 5 + 3; // sum = 8 Arithmetic Operators: +, -, *, /, %
(modulus)
Comparison Operators: ==, !=, <, >, <=,
>=.
Logical Operators: && (and), || (or), !
(not).
if (age > 18) { Conditional Flow:
std::cout << "Adult"; If/else if/else.
} else {
std::cout << "Minor";
}
switch (grade) { switch
case 'A': std::cout << "Excellent"; break;
case 'B': std::cout << "Good"; break;
default: std::cout << "Grade not
recognized";
}
for (int i = 0; i < 10; i++) { For-loop: known iteration count
std::cout << i << " ";
}
int i = 0; While-loop: repeats if true
while (i < 10) {
std::cout << i << " ";
i++;
}
int i = 0; Do-while-loop: repeats at least once
do {
std::cout << i << " ";
i++;
} while (i < 10);
Functions:
int add(int a, int b) { Defined using a return type, function name,
return a + b; and parameters
}
int result = add(5, 3); // result = 8 Calling a function
int factorial(int n) { Recursion:
if (n <= 1) return 1; A function calling itself
return n * factorial(n - 1);
}
1st 2 lines of code:
#include <bits/stdc++.h>
Using namespace std; (only for noi stuff, other projects no need lol)
Integer data types
● short
● int
● long long
● unsigned short
● unsigned int
● unsigned long long
Float data types: Float, double, long double
Declare variables: datatype variablename = value
Input: cin>> variable;
Fast io for input: [Link](0); ios_base::sync_with_stdio(0);
Output: cout<< “variable”;
cout<<”variable\n”
Built-in Functions
Std::max, std:min ([Link]
Std::sort (default: ascending order) [Link]
Extra knowledge: what are iterators? Lol ien
For use with std:: binary search, std::lowerbound/upperbound
WA
RTE
MTL
AC