Used for output and input in C++, respectively
Found in the <iostream> header</iostream>
std::cout and std::cin
Mostly the same (e.g., if, else, for, while)
Exception: return is not always required in C++, leading to potential Undefined Behavior
Control Flow in C++ vs. Java
Behavior that is unpredictable and can cause crashes or unexpected results
Should be avoided at all costs
Often caused by breaking C++ rules
Undefined Behavior (UB)
Used for type inference (compiler determines the type)
Advantages:
Safety (prevents uninitialized variables)
Conciseness (shorter code)
Uniformity
Performance
auto keyword
Instructions for the preprocessor (runs before compilation)
#include: Inserts the contents of a file
#define: Creates macros (text replacements)
Preprocessor Directives
Java uses “collections” (e.g., List, Set)
C++ uses “containers” (e.g., vector, set)
Both refer to types that store multiple elements
Collections vs. Containers
Used to get the number of elements in a container
Consistent across arrays, strings, and other containers
size() in C++
Function: Named block of code that takes parameters and returns a value
Method: Function that is part of a class (member function)
Function vs. Method
Argument: Value passed to a function
Parameter: Variable declared in the function signature that receives the argument
Argument vs. Parameter
.clear(): Removes all elements
.empty(): Returns true if the container is empty
Additional Container Methods
Most algorithms modify the original container in place (e.g., std::ranges::sort())
Some algorithms return a result (e.g., std::ranges::equal())
Values Returned by Standard Algorithms
New algorithms use std::ranges namespace (e.g., std::ranges::sort())
Old algorithms don’t use std::ranges (e.g., std::sort())
Prefer new algorithms when available
Old vs. New Algorithms
No built-in easy way in standard C++ before C++23
External libraries can help (will be covered later)
Displaying a Range
Shortens code by avoiding repeated std::
Can lead to errors if overused
Use cautiously, preferably within functions
using Directive
Alternative to using directive
Creates a shorter name for a namespace (e.g., namespace rng = std::ranges;)
Can be used globally in .cpp files
Namespace Alias
Used for dynamic memory allocation (creating objects on the heap)
Avoid until you understand pointers
new Keyword
Use them whenever possible for better code
Understand how they work and why to choose one over another
Standard Algorithms
Makes a variable or object immutable (cannot be changed)
Use by default for safety and clarity
Its absence should raise a “yellow flag”
const Keyword