\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{NexWebSites.com (NexWebSites)} \pdfinfo{ /Title (modern-c-17-standard-library-features.pdf) /Creator (Cheatography) /Author (NexWebSites.com (NexWebSites)) /Subject (Modern C++ 17 Standard Library Features 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}{0010A3} \definecolor{LightBackground}{HTML}{F7F7FC} \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{Modern C++ 17 Standard Library Features Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{NexWebSites.com (NexWebSites)} via \textcolor{DarkBackground}{\uline{cheatography.com/57483/cs/24614/}}} \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}NexWebSites.com (NexWebSites) \\ \uline{cheatography.com/nexwebsites} \\ \uline{\seqsplit{nexwebsites}.com} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 3rd October, 2020.\\ Updated 13th January, 2022.\\ 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}{std::variant}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{std::variant\textless{}int, double\textgreater{} v \{12\}; \newline std::get\textless{}int\textgreater{}(v); // == 12 \newline std::get\textless{}0\textgreater{}(v); // == 12 \newline v = 12.0; \newline std::get\textless{}double\textgreater{}(v); // == 12.0 \newline std::get\textless{}1\textgreater{}(v); // == 12.0} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The class template std::variant represents a type-safe union. An instance of std::variant at any given time holds a value of one of its alternative types (it's also possible for it to be valueless).} \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::optional}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{std::optional\textless{}std::string\textgreater{} create(bool b) \{ \newline if (b) \{ \newline return "Wonder Woman"; \newline \} else \{ \newline return \{\}; \newline \} \newline \} \newline \newline create(false).value\_or("empty"); // == "empty" \newline create(true).value(); // == "Wonder Woman" \newline // optional-returning factory functions are usable as conditions of while and if \newline if (auto str = create(true)) \{ \newline // ... \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The class template std::optional manages an optional contained value, i.e. a value that may or may not be present. A common use case for optional is the return value of a function that may fail.} \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::any}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{std::any x \{5\}; \newline x.has\_value() // == true \newline std::any\_cast\textless{}int\textgreater{}(x) // == 5 \newline std::any\_cast\textless{}int\&\textgreater{}(x) = 10; \newline std::any\_cast\textless{}int\textgreater{}(x) // == 10} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A type-safe container for single values of any type.} \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::string\_view}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Regular strings. \newline std::string\_view cppstr \{"foo"\}; \newline // Wide strings. \newline std::wstring\_view wcstr\_v \{L"baz"\}; \newline // Character arrays. \newline char array{[}3{]} = \{'b', 'a', 'r'\}; \newline std::string\_view array\_v(array, std::size(array)); \newline \newline std::string str \{" trim me"\}; \newline std::string\_view v \{str\}; \newline v.remove\_prefix(std::min(v.find\_first\_not\_of(" "), v.size())); \newline str; // == " trim me" \newline v; // == "trim me"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{A non-owning reference to a string. Useful for providing an abstraction on top of strings (e.g. for parsing).} \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}{Parallel algorithms}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{std::vector\textless{}int\textgreater{} longVector; \newline // Find element using parallel execution policy \newline auto result1 = \seqsplit{std::find(std::execution::par}, std::begin(longVector), std::end(longVector), 2); \newline // Sort elements using sequential execution policy \newline auto result2 = \seqsplit{std::sort(std::execution::seq}, std::begin(longVector), std::end(longVector));} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Many of the STL algorithms, such as the copy, find and sort methods, started to support the parallel execution policies: seq, par and par\_unseq which translate to "sequentially", "parallel" and "parallel unsequenced".} \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::invoke}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{template \textless{}typename Callable\textgreater{} \newline class Proxy \{ \newline Callable c; \newline public: \newline Proxy(Callable c): c(c) \{\} \newline template \textless{}class... Args\textgreater{} \newline decltype(auto) operator()(Args\&\&... args) \{ \newline // ... \newline return std::invoke(c, std::forward\textless{}Args\textgreater{}(args)...); \newline \} \newline \}; \newline auto add = {[}{]}(int x, int y) \{ \newline return x + y; \newline \}; \newline Proxy\textless{}decltype(add)\textgreater{} p \{add\}; \newline p(1, 2); // == 3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Invoke a Callable object with parameters. Examples of Callable objects are std::function or std::bind where an object can be called similarly to a regular 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}{std::apply}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{auto add = {[}{]}(int x, int y) \{ \newline return x + y; \newline \}; \newline std::apply(add, std::make\_tuple(1, 2)); // == 3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Invoke a Callable object with a tuple of 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}{std::filesystem}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{const auto bigFilePath \{"bigFileToCopy"\}; \newline \newline if \seqsplit{(std::filesystem::exists(bigFilePath))} \{ \newline const auto bigFileSize \{std::filesystem::file\_size(bigFilePath)\}; \newline std::filesystem::path tmpPath \{"/tmp"\}; \newline \newline if \seqsplit{(std::filesystem::space(tmpPath)}.available \textgreater{} bigFileSize) \{ \newline \seqsplit{std::filesystem::create\_directory(tmpPath}.append("example")); \newline \seqsplit{std::filesystem::copy\_file(bigFilePath}, \seqsplit{tmpPath.append("newFile"));} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The new std::filesystem library provides a standard way to manipulate files, directories, and paths in a filesystem. \newline \newline Here, a big file is copied to a temporary path if there is available space.} \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::byte}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{std::byte a \{0\}; \newline std::byte b \{0xFF\}; \newline int i = std::to\_integer\textless{}int\textgreater{}(b); // 0xFF \newline std::byte c = a \& b; \newline int j = std::to\_integer\textless{}int\textgreater{}(c); // 0} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The new std::byte type provides a standard way of representing data as a byte. Benefits of using std::byte over char or unsigned char is that it is not a character type, and is also not an arithmetic type; while the only operator overloads available are bitwise operations. \newline \newline Note that std::byte is simply an enum, and braced initialization of enums become possible thanks to \seqsplit{direct-list-initialization} of enums.} \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}{Splicing for maps and sets}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Moving elements from one map to another: \newline std::map\textless{}int, string\textgreater{} src \{\{1, "one"\}, \{2, "two"\}, \{3, "Three"\}\}; \newline std::map\textless{}int, string\textgreater{} dst \{\{3, "three"\}\}; \newline dst.insert(src.extract(src.find(1))); \newline dst.insert(src.extract(2)); \newline // dst == \{ \{ 1, "one" \}, \{ 2, "two" \}, \{ 3, "three" \} \}; \newline \newline // Inserting an entire set: \newline std::set\textless{}int\textgreater{} src \{1, 3, 5\}; \newline std::set\textless{}int\textgreater{} dst \{2, 4, 5\}; \newline dst.merge(src); \newline // src == \{ 5 \} \newline // dst == \{ 1, 2, 3, 4, 5 \} \newline \newline // Inserting elements which outlive the container: \newline auto elementFactory() \{ \newline std::set\textless{}...\textgreater{} s; \newline s.emplace(...); \newline return s.extract(s.begin()); \newline \} \newline s2.insert(elementFactory()); \newline \newline // Changing the key of a map element: \newline std::map\textless{}int, string\textgreater{} m \{\{1, "one"\}, \{2, "two"\}, \{3, "three"\}\}; \newline auto e = m.extract(2); \newline e.key() = 5; \newline m.insert(std::move(e)); \newline // m == \{ \{ 1, "one" \}, \{ 3, "three" \}, \{ 5, "two" \} \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Moving nodes and merging containers without the overhead of expensive copies, moves, or heap \seqsplit{allocations/deallocations}.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}