0 day streak
Code library

Intermediate C++ exercises

Read these Intermediate C++ snippets line by line — each one comes with a written breakdown of what the code does and why.

C++ · Intermediate

Move Semantics with std::move

Move semantics allow resources (like heap memory) to be moved from one object to another instead of being copied. std::move casts an object to an rvalue, enabling the move constructor to take over…

PerformanceMemory Management
Open snippet →
C++ · Intermediate

Smart Pointers: std::unique_ptr

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. It cannot be copied, only moved, ensurin…

Memory ManagementBest Practices
Open snippet →
C++ · Intermediate

Lambda Expressions and Capturing

Lambdas are anonymous functions that can be defined inline. The capture clause '[]' allows the lambda to access variables from the surrounding scope, either by value or by reference.

FunctionsSyntax
Open snippet →
C++ · Intermediate

Virtual Destructors in Inheritance

In C++, if you delete a derived class object through a base class pointer, the behavior is undefined unless the base class has a virtual destructor. Marking it virtual ensures the entire object hie…

OOPMemory Management
Open snippet →
C++ · Intermediate

RAII and Exception Safety

Resource Acquisition Is Initialization (RAII) is a core C++ idiom. Resources are tied to object lifetime; they are acquired in the constructor and released in the destructor, ensuring they are neve…

Memory ManagementBest Practices
Open snippet →
C++ · Intermediate

Template Specialization

Template Specialization allows you to define a custom implementation of a template for a specific data type, overriding the generic version.

SyntaxPerformance
Open snippet →
C++ · Intermediate

Operator Overloading

Operator overloading lets you redefine how standard operators like + or - behave when used with your custom classes.

OOPFunctions
Open snippet →
C++ · Intermediate

The 'mutable' Keyword

The mutable keyword allows a specific member of a class to be modified even if the object is accessed through a const reference or inside a const member function.

OOPSyntax
Open snippet →
C++ · Intermediate

Member Initializer Lists

Member initializer lists are used in constructors to initialize class members before the constructor body executes, which is more efficient than assignment.

PerformanceBest Practices
Open snippet →
C++ · Intermediate

Static Class Members

Static members are shared by all instances of a class. There is only one copy of a static member, regardless of how many objects are created.

OOPMemory Management
Open snippet →
C++ · Intermediate

Custom Range-based Iteration

By implementing begin() and end() methods that return pointers (or iterators), you enable your custom classes to be used with C++ range-based for loops.

Control FlowArrays & ListsSyntax
Open snippet →
C++ · Intermediate

Optimized References in Loops

When iterating over containers of objects like strings, using a 'const reference' avoids expensive copy operations for every iteration.

Control FlowPerformanceBest Practices
Open snippet →
C++ · Intermediate

Precise Fixed-Width Integer Aliases

Standard C++ integer types like 'int' or 'long' can vary in size between architectures. The <cstdint> header provides aliases like int32_t or uint64_t that guarantee a specific bit-width, which is…

Data Types
Open snippet →
C++ · Intermediate

Initializers within Selection Statements

C++17 introduced the ability to initialize a variable directly inside the condition of an 'if' or 'switch' statement. This limits the variable's scope to the block, preventing namespace pollution a…

Control FlowSyntax
Open snippet →
C++ · Intermediate

Fixed-Size Containers with Standard Iterators

Unlike raw C-style arrays, std::array is a first-class container that doesn't decay to a pointer automatically. It knows its own size and supports the standard iterator interface (begin/end), makin…

Arrays & ListsBest Practices
Open snippet →
C++ · Intermediate

Compile-Time Logic Branches

The 'if constexpr' statement allows for conditional compilation. Branches that do not meet the condition are not instantiated by the compiler, which is highly useful for template metaprogramming to…

Control FlowPerformance
Open snippet →
C++ · Intermediate

Safe Boundary Validation in Collections

While the subscript operator [] is fast, it does not perform any bounds checking, leading to undefined behavior on invalid indices. The at() method provided by standard containers validates the ind…

Arrays & ListsMemory Management
Open snippet →
C++ · Intermediate

Evaluating Multi-Condition Loops

This intermediate control flow pattern uses logical AND to prevent buffer overflows while iterating. It ensures that memory access only occurs if the index is valid and the data meets specific crit…

Control FlowSyntax
Open snippet →
C++ · Intermediate

Strongly Typed Enumerated Constants

Enum classes (scoped enums) provide better type safety than traditional enums by preventing implicit conversions to integers. Specifying the underlying type like 'unsigned char' optimizes memory us…

Data TypesBest Practices
Open snippet →
C++ · Intermediate

Direct Memory Access via Array Pointers

In C++, arrays decay into pointers to their first element. Pointer arithmetic allows you to navigate through the array by adding offsets to the base address, which is a fundamental concept in low-l…

Arrays & ListsMemory Management
Open snippet →
C++ · Intermediate

Post-Condition Iteration Logic

The do-while loop guarantees at least one execution of the code block because the condition is evaluated after the body. This is useful for tasks like input validation where you need to run the log…

Control FlowSyntax
Open snippet →
C++ · Intermediate

Grid Storage with Nested Arrays

Multidimensional arrays in C++ are stored in row-major order. This means all elements of a row are contiguous in memory. Nested loops are the standard way to traverse these structures using row and…

Arrays & ListsData Types
Open snippet →
C++ · Intermediate

Modern Containerized Arrays

Unlike raw C-style arrays, std::array is a container that knows its own size and doesn't decay into a pointer automatically. It provides a more robust way to handle fixed-size collections while mai…

Arrays & ListsBest Practices
Open snippet →
C++ · Intermediate

Range-Based Loop with Structured Bindings

Structured bindings (C++17) allow you to unpack complex data structures like pairs or tuples directly into individual variables within a loop header. This significantly improves readability when it…

Control FlowSyntax
Open snippet →
C++ · Intermediate

Explicit Narrowing Conversions

Using static_cast for type conversion is preferred over C-style casts because it is more visible in the code and checked by the compiler. It signals to other developers that the precision loss duri…

Data TypesSyntax
Open snippet →
C++ · Intermediate

Intentional Switch Fallthrough

Switch fallthrough occurs when a case doesn't end with a break. While often a bug, it can be useful for hierarchical logic. The [[fallthrough]] attribute explicitly tells the compiler and other dev…

Control FlowBest Practices
Open snippet →
C++ · Intermediate

Two-Dimensional Data Initialization

Multidimensional arrays are stored in row-major order in memory. Initializing them with nested braces improves code clarity by visually mapping the data to its logical grid structure.

Arrays & ListsData Types
Open snippet →