Cpp General

Structures

// Declare a structure
struct StructName {
    data_type member1;
    data_type member2;
    // ...
};

// Create an instance
StructName instance_name;

// Access members
instance_name.member1 = value;

// Nested structures
struct NestedStruct {
    int inner_member;
};

struct OuterStruct {
    int outer_member;
    NestedStruct nested;
};

Pointers

// Declare a pointer
data_type* pointer_name;

// Initialize pointer
pointer_name = &variable;

// Dereference pointer
value = *pointer_name;

// Pointer arithmetic
pointer_name++; // Moves to the next element

// Dynamic memory allocation
data_type* dynamic_ptr = new data_type;
delete dynamic_ptr; // Release memory

References

// Declare a reference
data_type& reference_name = original_variable;

// Use reference
reference_name = new_value; // Modifies the original variable

Arrays

// Declare an array
data_type array_name[size];

// Initialize an array
data_type array_name[] = {value1, value2, value3};

// Access elements
element = array_name[index];

// Modify elements
array_name[index] = new_value;

Sorting Algorithms

Bubble Sort: Bubble sort repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It continues to do this until no more swaps are needed, indicating that the list is sorted. Bubble sort has poor performance for large lists and is mainly used for educational purposes.
Selection Sort: Selection sort divides the list into a sorted and an unsorted region. It repeatedly selects the minimum element from the unsorted region and moves it to the end of the sorted region. The process continues until the entire list is sorted.

Search Algorithms

Linear Search:
Linear search is a simple algorithm that scans through an array one element at a time, comparing each element with the target value. It continues this process until it finds the target or reaches the end of the array. Linear search is straightforward but not the most efficient for large datasets.
Binary Search:
Binary search works on a sorted array and follows a divide-and-conquer approach. It starts with the middle element and compares it to the target. If they match, the search is successful. If the target is smaller, it repeats the process on the left half of the array; if the target is larger, it looks in the right half. This process continues until the target is found or the search range becomes empty.

Functions

// Function declaration
return_type function_name(parameters);

// Function definition
return_type function_name(parameters) {
    // Function body
    // Code here
    return result; // Optional
}

HashMap

 
C++
Java
Python3
include
#include <unordered_map>
import java.util.*;
new
std::unordered_map<int, int> t;
Hashtable<Integer, Integer> t = new Hashtable<>();
d = {}
add
t.insert(std::make_pair<int, int>(key, val));
t.put(key, val);
d[key] = val
get
int val = t.at(key);
Integer val = t.get(key);
val = d[key]
delete
t.erase(key);
t.remove(key);
del d[key]
iterate
 
for (Map.Entry<Integer, Integer> set : t.entrySet()) {
key=set.getKey();
val=set.getValue();
}
iterate
 
t.forEach((key, val)->{...})
for key in d:
check in table
std::unordered_map<int, int>::const_iterator it = t.find(key);
if (it != t.end())
if (t.containsKey(key))
if key in d.keys():
size
s.size()
s.size()
len(s)
check empty
if (s.empty())
if (s.isEmpty())
if not s:

HashSet

 
C++
Java
Python3
include
#include <unordered_set>
import java.util.*;
new
std::unordered_set<int> s;
Set<Integer> s = new HashSet<>();
s = set()
add
s.insert(x);
s.add(x);
s.add(x)
delete
s.erase(x);
s.remove(x);
s.discard(x)
check in set
std::unordered_set<int>::const_iterator it = s.find(x);
if (it != s.end())
if (s.contains(x))
if x in s:
size
s.size()
s.size()
len(s)
check empty
if (s.empty())
if (s.isEmpty())
if not s:

Heap

 
C++
Java
Python3
include
#include <queue>
import java.util.*;
from queue import PriorityQueue
new min heap
std::priority_queue<int, std::vector<int>, std::greater<int>> h;
Queue<Integer> h = new PriorityQueue<>();
h = PriorityQueue()
new max heap
std::priority_queue<int> h;
Queue<Integer> h = new PriorityQueue<>(Collections.reverseOrder());
push
h.push(i);
h.add(i);
h.put((1, "Harry"))
pop
h.pop();
Integer popped = h.poll();
popped = h.get()
top
h.top()
h.peek()
popped = h.get()
h.put(popped)
size
h.size()
h.size()
h.qsize()
check empty
if (h.empty())
if (h.isEmpty())
if h.empty():

Queue

 
C++
Java
Python3
include
#include <queue>
import java.util.*;
new
std::queue<int> q;
Queue<Integer> q = new LinkedList<>();
q = []
push
q.push(i);
q.add(i);
q.append(i)
pop
q.pop()
popped = q.remove();
popped = q.pop(0)
front
q.front()
q.peek()
q[0]
size
q.size()
q.size()
len(q)
check empty
if (q.empty())
if (q.isEmpty())
if not q:

Stack

 
C++
Java
Python3
include
#include <stack>
import java.util.*;
new
std::stack<int> s;
Stack<Integer> s = new Stack<Integer>();
s = []
push
s.push(i)
s.push(i);
s.append(i)
pop
s.pop();
popped = s.pop();
popped = s.pop()
top
s.top()
s.peak()
s[-1]
size
s.size()
s.size()
len(s)
check empty
if (s.empty())
if (s.empty())
if not s:

String

 
C++
Java
Python3
new
char* s = "string";
String s = "string";
s = "string"
get
char c = s[0];
char c = s.charAt(0);
c = s[0];
length
strlen(s)
s.length()
len(s)
sub string
char* sub = malloc(10);
strncpy(sub, s+begin, end-begin);
String sub = s.substring(begin, end);
s[begin, end]
check substring
if (strstr(sub, s))
if (s.contains(sub))
if sub in s:

Array

 
C++
Java
Python3
new
int x = [0];
int[] x = new int[]{0};
x = [0]
length
sizeof(x)
x.length
len(x)

Java Wrapper Class

byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
char
Character
Comments