\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{blakecromar} \pdfinfo{ /Title (c.pdf) /Creator (Cheatography) /Author (blakecromar) /Subject (C++ Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{329C91} \definecolor{LightBackground}{HTML}{F2F8F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{C++ Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{blakecromar} via \textcolor{DarkBackground}{\uline{cheatography.com/218070/cs/48101/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}blakecromar \\ \uline{cheatography.com/blakecromar} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 4th June, 2026.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{The -\textgreater{} operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Dog{\emph{ d = new Dog("Rex"); \newline \newline d-\textgreater{}bark(); // shorthand \newline (}}d).bark(); // same thing} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{-\textgreater{} operator — shorthand for dereferencing a pointer and accessing a member. Equivalent to (*pointer).member.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Building a class with a header file}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Foo.h \newline struct Foo \{ \newline \textless{}Type\textgreater{} \textless{}attributeName\textgreater{}; \newline \newline static \textless{}ReturnType\textgreater{} \textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}); \newline static \textless{}ReturnType\textgreater{} \textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}); \newline static \textless{}ReturnType\textgreater{} \textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}); \newline \}; \newline \newline // Foo.cpp \newline \#include "Foo.h" \newline \newline \textless{}ReturnType\textgreater{} Foo::\textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}) \newline \{ \newline // implementation \newline \} \newline \newline \textless{}ReturnType\textgreater{} Foo::\textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}) \newline \{ \newline // implementation \newline \} \newline \newline \textless{}ReturnType\textgreater{} Foo::\textless{}functionName\textgreater{}(\textless{}Type\textgreater{} \textless{}variable\textgreater{}) \newline \{ \newline // implementation \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{enum class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{enum class \textless{}EnumName\textgreater{} \{ \textless{}Val1\textgreater{}, \textless{}Val2\textgreater{}, \textless{}Val3\textgreater{} \}; \newline \newline \textless{}EnumName\textgreater{} \textless{}var\textgreater{} = \textless{}EnumName\textgreater{}::\textless{}Val\_n\textgreater{}; // where n is an Val in the definition.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{You can think of these as a variable, but ahead of time it is constrained to a specific set of values.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Definition of a Dangling Pointer}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int{\emph{ createNumber() \{ \newline int x = 42; // lives on the stack \newline return \&x; // return its address \newline \} // x is destroyed here \newline \newline int main() \{ \newline int}} p = createNumber(); \newline *p; // ❌ dangling — x is gone, memory no longer belongs to you \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Dangling pointer — a pointer that points to memory that no longer belongs to you. The object it pointed to has been destroyed, but the pointer still holds its old address.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Address of Operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int x = 42; \newline int* ptr = \&x; // ptr holds the memory address of x} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Definition of a Caller}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int add(int a, int b) \{ // callee \newline return a + b; \newline \} \newline \newline int main() \{ // caller \newline add(1, 2); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Caller — the function that calls another function.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Passing Functions (Callbacks)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}iostream\textgreater{} \newline \newline void sayHello() \newline \{ \newline std::cout \textless{}\textless{} "Hello!" \textless{}\textless{} std::endl; \newline \} \newline \newline void execute(void (*callback)()) \newline \{ \newline callback(); \newline \} \newline \newline int main() \newline \{ \newline execute(\&sayHello); // pass the function, not call it \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{In C++, functions can be passed as parameters to other functions using function pointers, allowing you to decide what code runs without hardcoding it. The receiving function holds onto the address of the passed function and calls it at the right time. This is the foundation of event-driven programming, where you say "call this function when something happens"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Defnition of a Thread}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}thread\textgreater{} \newline \#include \textless{}iostream\textgreater{} \newline \newline void doWork() \{ \newline std::cout \textless{}\textless{} "thread running\textbackslash{}n"; \newline \} \newline \newline int main() \{ \newline std::thread t(doWork); // thread starts here \newline t.join(); // main waits for it to finish \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{An independent sequence of execution within a program. The OS can run multiple threads concurrently, each doing their own work, while sharing the same memory.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{L values (Left Values)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int x = 10; // x is an lvalue \newline x = 20; // valid: lvalue on the left of = \newline \newline int y = x + 1; // (x + 1) is an rvalue — temporary, no fixed address \newline int z = 42; // 42 is an rvalue literal \newline // \&(x + 1); // ERROR: cannot take address of an rvalue} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{An lvalue (locator value) is an expression that refers to a memory location and can appear on the left-hand side of an assignment. It represents an object that persists beyond a single expression — it has an identifiable address in memory.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Definition of a Static Method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class MathHelper \{ \newline public: \newline static int add(int a, int b) \{ \newline return a + b; \newline \} \newline \}; \newline \newline int result = MathHelper::add(3, 5); // 8} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A static method is a function that belongs to the class itself rather than to any instance of it. It has no access to instance attributes and can be called without ever creating an object.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Definition of a Signature}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}iostream\textgreater{} \newline \newline void greet(std::string name) \{ // signature: greet(std::string) \newline std::cout \textless{}\textless{} "Hello, " \textless{}\textless{} name \textless{}\textless{} "!\textbackslash{}n"; \newline \} \newline \newline int main() \{ \newline greet("Alice"); // → Hello, Alice! \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A function signature in C++ is the part of a function declaration that identifies it uniquely to the compiler. It consists of: \newline \newline Function name \newline Parameter types (number, order, and types)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Reference declaration (in type declarations)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int x = 42; \newline int\& ref = x; // ref IS x — same memory location \newline \newline ref = 100; // x is now 100} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{bitwise OR assignment operator}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{|= \newline \newline // Example \newline \newline bool result = false; // 0 \newline result |= false; // 0 | 0 = 0 \newline result |= false; // 0 | 0 = 0 \newline result |= true; // 0 | 1 = 1 \newline result |= false; // 1 | 0 = 1 — stays true \newline result |= false; // 1 | 0 = 1 — stays true \newline // result is true} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Claude responded: Bitwise OR assignment (|=) — takes the existing bits of the left operand, ORs them column by column with the bits of the right operand, and stores the result b…Bitwise OR assignment (|=) — takes the existing bits of the left operand, ORs them column by column with the bits of the right operand, and stores the result back into the left operand. Can only turn bits on, never off.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Lambda Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{[}\&\textless{}OBJECT\textgreater{}{]} \{ \newline \textless{}CODE\textgreater{}; \newline \textless{}CODE\textgreater{}; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The brackets tell the lambda which variables from the surrounding scope it's allowed to use, and how it remembers them. \newline \newline By default, a lambda body cannot see any local variable from the function it's written inside. The capture list is how you grant access, one variable (or one default rule) at a time.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Constructing an obect in C++}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{TypeName \seqsplit{variableName(constructorArguments);}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{This is how it would look in Python: \newline variable\_name = \seqsplit{ClassName(constructor\_arguments)}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Dereferencing a Pointer}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int x = 42; \newline int{\emph{ p = \&x; // p holds the address of x \newline \newline }}p; // dereference — follow the pointer to get 42} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The * in front of a pointer means "go to the address this pointer holds and give me what's there."} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Range Based For-Loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for (const \textless{}type\textgreater{}\& \textless{}element\textgreater{} : \textless{}collection\textgreater{}) \{ \newline // use \textless{}element\textgreater{} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A range-based for loop is a loop that iterates over every element in a collection, from the first to the last, without needing to manually manage an index or i…A range-based for loop is a loop that iterates over every element in a collection, from the first to the last, without needing to manually manage an index or iterator. \newline \newline {\bf{const}} — omit if you need to modify \textless{}element\textgreater{} \newline {\bf{\&}} — omit if you want a copy of each element rather than a reference} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Template Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{template \textless{}typename T\textgreater{} \newline T add(T a, T b) \{ \newline return a + b; // T must support the + operator \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Try Catch}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{try \{ \newline // code that might throw an exception \newline \} catch (const std::exception\& e) \{ \newline // handle exception \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Template Aliases}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// The template \newline template \textless{}typename T\textgreater{} \newline class Box \{ \newline T contents; \newline \}; \newline \newline // Without an alias — verbose \newline Box\textless{}int\textgreater{} myBox; \newline \newline // The alias \newline using IntBox = Box\textless{}int\textgreater{}; \newline \newline // With the alias — cleaner, same thing \newline IntBox myBox;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Commenting Out Unused Parameters in C++}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// You must accept both parameters to match the signature \newline void process(int /{\emph{unused}}/, int used) \newline \{ \newline std::cout \textless{}\textless{} used; // unused not needed, but must be in signature \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{When a function signature is fixed and you can't change it, but don't need all the parameters, comment out the name to suppress unused warnings while keeping the type.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}