\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{Benjy} \pdfinfo{ /Title (c.pdf) /Creator (Cheatography) /Author (Benjy) /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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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}{Benjy} via \textcolor{DarkBackground}{\uline{cheatography.com/56918/cs/16149/}}} \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}Benjy \\ \uline{cheatography.com/benjy} \\ \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 8th October, 2019.\\ 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}{enum}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{enum Color \{ \newline COLOR\_RED, // assigned 0 \newline COLOR\_BLUE // assigned 1 \newline \}; \newline Color color=COLOR\_RED; \newline \newline // input/output \newline int input\{\}; \newline std::cin \textgreater{}\textgreater{} input; \newline Color color=static\_cast\textless{}Color\textgreater{}(input)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Prefer enum class if possible (C++11)} \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 (C++11)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{enum class Color \{ \newline RED, \newline BLUE \newline \}; \newline Color color=Color::RED; \newline std::cout \textless{}\textless{} static\_cast\textless{}int\textgreater{}(color);} \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}{typedef}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{typedef double distance\_t; \newline using distance\_t = double; // C++11} \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}{struct}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{struct Employee \{ \newline short id; \newline int age; \newline \}; \newline Employee joe = \{1, 42\}; // initializer list \newline joe.age = 43; // member selection operator} \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}{std::vector}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}vector\textgreater{} \newline std::vector\textless{}int\textgreater{} tab \{1, 2, 3, 4\}; \newline tab{[}0{]} // or tab.at(0) with bounds checking \newline tab.size() \newline tab.resize(3); // expensive \newline \newline // capacity \& stack-like behavior \newline std::vector\textless{}int\textgreater{} stack; \newline stack.reserve(5); // set capacity \newline stack.push\_back(3); \newline int x = stack.back(); // top of stack \newline stack.pop\_back();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{capacity = allocated memory \newline length = active values \newline Setting capacity avoid resizing the array} \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}{std::tuple - C++11}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}tuple\textgreater{} \newline // using namespace std; \newline tuple\textless{}int, double\textgreater{} t = make\_tuple(3, 6.7); \newline int a = get\textless{}0\textgreater{}(t); \newline \newline // std::tuple\textless{}int, double\textgreater{} foo(); \newline int a; \newline double b; \newline std::tie(a,b) = foo() \newline auto {[}a, b{]} = foo(); // C++17} \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}{std::array - C++11}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}array\textgreater{} \newline std::array\textless{}int, 3\textgreater{} tab \{1, 2, 3\}; \newline tab{[}0{]} // or tab.at(0) with bounds checking \newline tab.size() // length not sizeof()} \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}{References}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int value=5; \newline int \&ref=value; // non-const ref \newline const int value=5; \newline const int \&ref=value; // treats value as const} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{References are similar to const pointers (cannot change 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}{Pointers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int value=5; \newline int {\emph{const p=\&value; //const pointer \newline const int value=5; \newline const int }}p; // non-const pointer to const int \newline \newline // const pointer to const value \newline const int {\emph{const p=\&value; \newline \newline // int foo(int x); \newline int (}}ptr)(int); // function pointer \newline int ({\emph{const ptr)(int) = foo; // const pointer \newline \#include \textless{}functional\textgreater{} \newline std::function\textless{}int(int)\textgreater{} ptr = foo; // C++11 \newline ptr(5); // implicit dereference or (}}ptr)(5)} \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}{new \& delete (dynamic allocation)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// scalar version \newline int {\emph{p=new int(3); \newline delete p; \newline \newline // array version \newline int }}p = new int{[}3{]} \{1, 2, 3\}; // C++11 syntax \newline delete{[}{]} p;} \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}{Classes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Foo \{ \newline private: \newline // non-static member initialization \newline int m\_value = 10; // C++11 \newline // static member variable // \newline static int m\_id; \newline public: \newline // constructor and destructor \newline Foo(int value):m\_value(value); \newline \textasciitilde{}Foo(); \newline // copy constructor \newline Foo(const Foo\& foo) \newline :m\_value(foo.m\_value); \newline \newline // static member function \newline static int get\_id(); // see definition \newline // const member function \newline void print() const; \newline // friend function \newline friend void reset(Foo \&foo); \newline \}; \newline \newline Foo(6); // anonymous object \newline \newline // static \newline int Foo::m\_id=0; \newline int Foo::get\_id() \{ return m\_id; \} // no static \newline int id = Foo::get\_id(); \newline \newline // const member function \newline void Foo::print() const \{ {\emph{whatever}} \} \newline const Foo foo; \newline foo.print(); // no constness violation \newline \newline // friend function \newline friend void reset(Foo \&foo) \{ foo.m\_value=0; \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1) no implicit default constructor if at least one constructor provided \newline 2) implicit copy constructor and overloaded operator= use memberwise initialization (shallow copy)} \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}{std::initializer\_list (C++11)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}initalizer\_list\textgreater{} \newline int m\_size; \newline int *m\_array; \newline \newline // prevent shallow copy (dynamic mem alloc) \newline Foo(const Foo\& foo) = delete; \newline Foo\& operator=(const Foo\& foo) = delete; \newline \newline Foo(const std::initializer\_list\textless{}int\textgreater{}\& list) \newline :m\_size(static\_cast\textless{}int\textgreater{}(list.size)) \newline \{ \newline // deep copy with for-each loop \newline int count=0; \newline for (auto\& value : list) \newline m\_array{[}count++{]} = value; \newline \} \newline Foo\& operator=(const std::initializer\_list\textless{}int\textgreater{} \&list) \newline \{ \newline // same but delete{[}{]} m\_array \newline // if same size (no need for new or delete) \newline // deep copy \newline \} \newline \newline Foo foo \{ 1, 2, 3, 4 \}; // call constructor \newline Foo foo = \{ 1, 2, 3, 4 \}; // call operator=} \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}{For-each loops - C++11}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for (int num : tab) \newline for (auto num : tab) // avoid type conversion \newline for (auto \&num : tab) // avoid copy \newline for (const auto \&num : tab) // read-only} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{For-each loops don't work with array decayed to pointer (fixed or dynamic)} \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}{std::rand()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}cstdlib\textgreater{} \newline \#include \textless{}ctime\textgreater{} \newline std::stand(static\_cast\textless{}unsigned int\textgreater{}(std::time(nullptr))); // seed with time \newline std::rand(); // {[}0, RAND\_MAX{]} \newline \newline // {[}min, max{]} \newline return min + \seqsplit{(std::rand()\%(max-min+1))} \newline //better \newline static constexpr double fraction \{1.0/(RAND\_MAX+1.0)\}; \newline return min + static\_cast\textless{}int\textgreater{}((max-min+1.0)*(std::rand()*fraction))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\% is biased towards low numbers} \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}{Input validation with std::cin}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// extraneous input \newline std::cin.ignore(32767, '\textbackslash{}n'); // clears buffer \newline \newline // extraction failure \newline if (std::cin.fail()) \{ // type or overflow \newline std::cin.clear(); // back to normal mode \newline std::cin.ignore(32767, '\textbackslash{}n'); \newline \} \newline \newline // switch-like validation \newline int x \{\}; \newline do \{ \newline std::cin \textgreater{}\textgreater{} x; \newline \} while (x\textless{}0);} \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}{std::cerr and assert}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include \textless{}cstdlib\textgreater{} // for exit \newline std::cerr \textless{}\textless{} "Error message"; \newline exit(1); \newline \newline \#include \textless{}cassert\textgreater{} \newline assert(x==0); // combines cerr and exit \newline assert(x==0 \&\& "Error message"); \newline // check at compile-time \newline static\_assert(x==0, "Error message"); // C++11} \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}{Operator overloading}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int m\_value; \newline int *m\_array=nullptr; \newline \newline // ret by value (ref to out of scope var) \newline friend Foo operator+(const Foo \&foo, const Foo\& bar); \newline \newline // subscript {[}{]} \newline // ret by ref to assign (l-value) \newline int\& operator{[}{]}(const int idx) \newline \{ return m\_array{[}idx{]}; \} \newline \newline // overload int typecast \newline operator int() const \{ return m\_value; \} \newline \newline // std::cout \newline std::ostream\& operator\textless{}\textless{}(std::ostream\& out, const Foo \&foo) \newline \{ \newline out \textless{}\textless{} m\_value; \newline return out; \newline \} \newline \newline // std::cin \newline std::istream\& operator\textgreater{}\textgreater{}(std::istream\& in, Foo \&foo) \newline \{ \newline in \textgreater{}\textgreater{} m\_value; \newline return in; \newline \} \newline \newline // increment operator as member function \newline Foo\& operator++(); // prefix, ++x \newline Foo operator++(int) // postfix, x++ \newline \{ \newline Foo tmp(foo); \newline // call implicit copy constructor \newline // or assignment with tmp = foo \newline // but beware of shallow copy \newline \newline // increment this \newline return tmp; // return by value \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1) unary op or if modif to left operand \newline -\textgreater{} member function \newline no modif, binary as friend or regular one \newline 2) {[}{]}, =, (), -\textgreater{} as member} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}