\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{Jack84} \pdfinfo{ /Title (comp2123.pdf) /Creator (Cheatography) /Author (Jack84) /Subject (COMP2123 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}{FD6A02} \definecolor{LightBackground}{HTML}{FEF5EF} \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{COMP2123 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Jack84} via \textcolor{DarkBackground}{\uline{cheatography.com/72157/cs/18281/}}} \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}Jack84 \\ \uline{cheatography.com/jack84} \\ \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 17th December, 2018.\\ 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{2.38896 cm} x{2.58804 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Bashshell Basic Commands}} \tn % Row 0 \SetRowColor{LightBackground} `gedit example.txt \&` & create a new empty file \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.1 Bashshell Directory Manipulation}} \tn % Row 0 \SetRowColor{LightBackground} `pwd` & Name of the current directory \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `cd dir` & change the directory to `cd` \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `cd \textasciitilde{}` & Change to home directory \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `cd ..` & Change to parent directory \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} `cd .` & Change to current directory \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} `mkdir dir` & Creates a new directory \tn % Row Count 7 (+ 2) % Row 6 \SetRowColor{LightBackground} `rmdir dir` & removes a directory \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} `rm -rf dir` & Removes non-empty directory dir and all of its subdirectories and files \tn % Row Count 12 (+ 3) % Row 8 \SetRowColor{LightBackground} `mv dir1 dir2` & Rename dir1 to dir2 \tn % Row Count 14 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.2 Bashshell File Manipulation}} \tn % Row 0 \SetRowColor{LightBackground} `gedit hello.cpp \&` & Starts the gedit program to edit file hello.cpp \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `g++ hello.cpp -o hello.o` & compile hello.cpp to create executable a.o \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `./a.o` & invoke `a.o` \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} `cp file1 file2` & Copy file1 into file2 \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} `cp -r dir1 dir2` & Copy dir1 (including its subdirectories) to dir2 \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} `mv file dir` & moves `file` to `dir` \tn % Row Count 10 (+ 1) % Row 6 \SetRowColor{LightBackground} `rm file` & remove file \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} `rm dir` & remove all files and directories in `dir` \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} `touch file` & Create an empty file called `file` \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} `cat file` & Display the content of `file` \tn % Row Count 17 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{File Security and Permission}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`ls -lt` print in long form with security level indication} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`chmod {[}who{]} {[}operator{]} {[}permissions{]} filename`} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{[}who{]} `u` = user; `g` = group; `o` = other; `a` = all} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{[}operator{]} `+` = add; `-` = remove; `=` set} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{[}permission{]} `r` = read; `w` = write; `x` = execute} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.3 Bash Shell Searching}} \tn % Row 0 \SetRowColor{LightBackground} `find . -name "hello.txt" -type f` & Search by name for a file \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `find . -name "hello.*" -type f` & Search any file type with the name "hello" \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `find . -name "hello" -type d` & Search for a directory called "hello" \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `find . -name "*.c"` & find .c files \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `find . -name "*.{[}ch{]}" & find .c and .h files \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} `grep '\textasciicircum{}apple' example` & match beginning of a line \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} `grep 'apple\$' example` & match the end of a line \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} `grep '\textasciicircum{}apple\$' example` & match the exact contents of a line \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} `grep 'p?' example` & match zero or one occurrence \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} `grep 'p+' example` & match one or more occurrences \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} `grep 'p*' example` & match zero or more occurrences \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} `grep 'p.' example` & match a single character "p" \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{LightBackground} `'{[}12345{]}'` or `'{[}1-5{]}'` & match any character enclosed by {[}{]} \tn % Row Count 26 (+ 2) % Row 13 \SetRowColor{white} `(ab)\{3\}` & 3 occurrences of "ab" \tn % Row Count 27 (+ 1) % Row 14 \SetRowColor{LightBackground} `(ab)\{1,3\}` & 1 to 3 occurrences of "ab" \tn % Row Count 29 (+ 2) % Row 15 \SetRowColor{white} `(ab)\{3,\}` & 3 or more occurrences of "ab" \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.3 Bash Shell Searching (cont)}} \tn % Row 16 \SetRowColor{LightBackground} `grep -c "UNIX" bar.txt` & no. of occurrence for "UNIX" \tn % Row Count 2 (+ 2) % Row 17 \SetRowColor{white} `sed s/UNIX/Unix/g bar.txt` & replace all "UNIX" to "Unix" \tn % Row Count 4 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Note that (ab) can also be other valid patterns} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.4 Useful Bash Shell Commands}} \tn % Row 0 \SetRowColor{LightBackground} `wc -c file` & return no. of bytes \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `wc -l file` & returns no. of lines \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `wc -w file` & returns no. of words \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `sort file` & sort file in alphabetical order and output the sorted texts \tn % Row Count 6 (+ 3) % Row 4 \SetRowColor{LightBackground} `sort -n file` & numerical sort \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `sort -n -r file` & -r for reverse sort \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} `sort -k3 -n filename` & sort by field no. 3 \tn % Row Count 11 (+ 2) % Row 7 \SetRowColor{white} `sort -t, -k3 -n filename` & sort by field no.3 but the delimiter is comma \tn % Row Count 13 (+ 2) % Row 8 \SetRowColor{LightBackground} `cut -d ' ' -f 1,3 example1` & return 1st and 3rd columns where the delimiter is a space \tn % Row Count 16 (+ 3) % Row 9 \SetRowColor{white} `uniq file` & remove adjacent duplicate lines \tn % Row Count 18 (+ 2) % Row 10 \SetRowColor{LightBackground} `diff fileA fileB` & how to transform fileA to fileB \tn % Row Count 20 (+ 2) % Row 11 \SetRowColor{white} `0a1` & add the line1 of fileB after line0 of fileA \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} `2,3C3` & change line2,3 of fileA to line3 of fileB \tn % Row Count 24 (+ 2) % Row 13 \SetRowColor{white} `spell file` & display all incorrect words in file \tn % Row Count 26 (+ 2) % Row 14 \SetRowColor{LightBackground} `su` & change to super user mode \tn % Row Count 28 (+ 2) % Row 15 \SetRowColor{white} `yum install {[}program{]}` & install program \tn % Row Count 30 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{1.5 Standard I/O and pipe}} \tn % Row 0 \SetRowColor{LightBackground} `wc data.txt 1\textgreater{} result.txt` & send standard output to file \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `wc data.txt \textgreater{} result.txt` & same as above \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `{[}command{]} \textgreater{}\textgreater{} file` & append result of {[}command{]} to the file \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} `{[}command{]} 2\textgreater{} file` & send standard error to file \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `./add.o \textless{} input.txt \textgreater{} output.txt` & add.o takes input from input.txt and output to output.txt \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`ls -l | grep "Jan 25"`} \tn % Row Count 12 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C user input}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int a; float b; \newline scanf("\%d\%f", \&a, \&b); \newline printf("\%g", a*b);} \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}{C++ const keyword}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{BigInteger add(const BigInteger \& a, const BigInteger \& b) \newline \{ \newline // Cannot modify any of the parameters \newline \} \newline \newline string \seqsplit{BigInteger::setNumber(string} number) const \{ \newline // setNumber is a read-only function. \newline sign = "-"; // error because sign is a member variable of BigInteger \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}{C++ Constructor}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{class Point\{ \newline private: \newline int x, y; \newline public: \newline Point() \{ // default \newline a = 10; b=20 \newline \} \newline Point(int x1, int y1)\{ // parameterised \newline x = x1; y = y1; \newline \} \newline \} \newline \newline int main()\{ \newline Point p; // default \newline Point q(10, 20); // parameterised \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Constructor is a member function that shares the same name as the class} \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}{C++ friend}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{// -{}-{}- In .h file -{}-{}- }} \newline class BigInteger\{ \newline public: \newline void setNumber( string ); \newline string getNumber(); \newline private: \newline char sign; \newline int length; \newline int value{[}100{]}; \newline friend BigInteger add(BigInteger a, BigInteger b); \newline \}; \newline \newline BigInteger add(BigInteger a, BigInteger b); \newline // add declaration of friend function AFTER the declaration fo the big integer class \newline \newline {\bf{// -{}-{}- in .cpp file -{}-{}- //}} \newline BigInteger add(BigInteger a, BigInteger \{ \newline // implement the function \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2.1 Shell Script basics}} \tn % Row 0 \SetRowColor{LightBackground} `echo -n "hello world"` & print without \textbackslash{}n \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `read name` & read user input and store it in a var called "name" \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} `a=apple` & no quote \tn % Row Count 6 (+ 1) % Row 3 \SetRowColor{white} `a='apple pie'` & single quote (strings) \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} `a="\$a\textbackslash{}\$"` & can handle special characters \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} \$ & variable substitution \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \textbackslash{} & escape special characters \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} `\textbackslash{}`cat hello.txt\textbackslash{}`` & enclose bash commands \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`a="\textbackslash{}`wc -l file | cut -d\textbackslash{}"\textbackslash{}" -f1\textbackslash{}`"`} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`echo "there are \$a lines in file"`} \tn % Row Count 16 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2.2.1 Shell Script - Using Strings}} \tn % Row 0 \SetRowColor{LightBackground} `\$\{\#a\}` & length of string \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `\$\{a:pos:len\}` & substring (assume index 0) \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `\$\{a/from/to\}` & change part of string \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`a="Apple pie"; from="pie"; to="juice";`} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2.2.4 Shell Scripting - Variable as numbers}} \tn % Row 0 \SetRowColor{LightBackground} `let "a=\$a+1"` & increment a by 1 \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{2.3 Shell Scripting - Control Flow}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{\# -{}-{}- if-else statements -{}-{}- \#}} \newline \newline if {[} condition {]} \newline then \newline echo "Action 1" \newline elif {[} condition2 {]} \newline then \newline echo "Action2" \newline else \newline echo "Action neither" \newline fi \newline {\bf{\#example}} \newline \#!/bin/bash \newline echo "Do you want to remove all .cpp files (Y/N)" \newline read ans \newline if {[} "\$ans" == "Y" {]} \newline then \newline rm -rf *.cpp \newline echo "All .cpp files are removed" \newline fi \newline \newline {\bf{\# -{}-{}- for-in loop -{}-{}- \#}} \newline \newline \#!/bin/bash \newline list="1 2 3 4 5" \newline for i in \$list \newline do \newline echo "This is iteration \$i" \newline done \newline \newline {\bf{\# -{}-{}- for-loop with a range -{}-{}- \#}} \newline \newline for ((i=0; i\textless{}=100; i=i+3)) \newline do \newline echo \$i \newline done} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.34379 cm} x{3.63321 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{2.4 Shell Scripting - Useful techniques}} \tn % Row 0 \SetRowColor{LightBackground} `\$\#` & number of arguments input by user \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `\$0; \$1; \$2` & 1st argument, 2nd argument, etc \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`cp file123 fileabc 1\textgreater{} /dev/null 2\textgreater{} \&1`} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`/dev/null` is system dust bin} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`\&1` is the standard output} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`echo "\$0:error:Copy failed" \textgreater{}\&2`} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`\&2` is error output} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.14011 cm} x{2.83689 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Shell Script Conditions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{String comparison}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `{[} "\$s" {]}` & iff length of s is non-zero \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`{[} "s1" == "\$s2" {]}`} \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`{[} "\$s1" != "\$s2" {]}`} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} `{[} "\$s1" /\textgreater{} "\$s2" {]}` & s1 sorted after s2 \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`{[} "\$s1" /\textless{} "s2" {]}`} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{File Checking}}} \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `{[} -e \$file {]} ` & iff exists \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} `{[} -f \$file {]}` & iff is a file \tn % Row Count 11 (+ 1) % Row 9 \SetRowColor{white} `{[} -d file {]}` & iff is a directory \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Number Comparison}}} \tn % Row Count 13 (+ 1) % Row 11 \SetRowColor{white} `{[} \$a -eq \$b {]}` & iff a = b \tn % Row Count 14 (+ 1) % Row 12 \SetRowColor{LightBackground} `{[} \$a -ne \$b {]}` & iff a!=b \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} `{[} \$a -lt \$b {]}` & iff a\textless{}b \tn % Row Count 16 (+ 1) % Row 14 \SetRowColor{LightBackground} `{[} \$a -le \$b {]}` & iff a\textless{}=b \tn % Row Count 17 (+ 1) % Row 15 \SetRowColor{white} `{[} \$a -gt \$b {]}` & iff a\textgreater{}b \tn % Row Count 18 (+ 1) % Row 16 \SetRowColor{LightBackground} `{[} \$a -ge \$b {]}` & iff a\textgreater{}=b \tn % Row Count 19 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Iterate words in a file}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{list=\textbackslash{}`cat wordlist.txt\textbackslash{}` \newline for line in \$list \newline do \newline echo "\$line" \newline done} \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}{C++ Misc}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`void func(int array{[}{]});` // array as parameter} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C++ Dynamic Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int * a = NULL; int n; \newline cin \textgreater{}\textgreater{} n; a = new int{[}n{]}; \newline ... \newline delete{[}{]} a; // free memory} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Pointers C++}} \tn % Row 0 \SetRowColor{LightBackground} `int *baz` & define a pointer \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `\&foo` & address of foo \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `*baz` & value pointed by baz \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} `void PBV(int *p)` & parameter is a pointer \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} `void PBR(int *\&p)` & parameter is address of pointer \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{modifying the parameter modifies the original variable} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C++ Linked List and Various functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int main()\{ Node {\emph{head = NULL; ... \} \newline void headInsert(Node }}\&head, int k, int v)\{ \newline Node {\emph{newNode = new Node; \newline newNode -\textgreater{} key = k; \newline newNode -\textgreater{} value = v; \newline newNode -\textgreater{} next = head; \newline head = newNode; \newline \} \newline void printList(Node }}head)\{ \newline Node {\emph{current = head; \newline while (current!=NULL)\{ \newline cout \textless{}\textless{} "Key:" \textless{}\textless{} current-\textgreater{}key \textless{}\textless{} ",value:" \textless{}\textless{} current-\textgreater{}value \textless{}\textless{} endl; \newline current = current-\textgreater{}next; \newline \} \newline \} \newline bool isSorted(Node }}head)\{ \newline Node {\emph{current = head; \newline Node }}previous = NULL; \newline while (current!=NULL)\{ \newline if (previous !=NULL)\{ \newline if (previous-\textgreater{}key \textgreater{} current-\textgreater{}key) \newline return false; \newline \} \newline previous = current; \newline current = current-\textgreater{}next; \newline \} \newline return true; \newline \} \newline void insertInOrder(Node {\emph{\&head, int k)\{ \newline Node }}newNode = new Node; \newline newNode-\textgreater{}key = k; \newline \newline if (head == NULL) \newline \{ \newline newNode-\textgreater{}next = NULL; \newline head = newNode; \newline \} \newline else \newline \{ \newline Node {\emph{current= head; \newline Node }}previous = NULL; \newline while(current!=NULL) \newline \{ \newline if (current-\textgreater{}key \textgreater{} k) \newline break; \newline previous = current; \newline current=current-\textgreater{}next; \newline \} \newline newNode-\textgreater{}next = current; \newline if (previous!=NULL) \newline previous-\textgreater{}next = newNode; \newline else \newline head = newNode; \newline \} \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}{4. Separate Compilation and Makefile}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{census.o: census.cpp BigInteger.h Country.h \newline g++ -c census.cpp \newline \newline BigInteger.o:BigInteger.h BigInteger.cpp \newline g++ -c BigInteger.cpp \newline \newline Country.o:BigInteger.h Country.h Country.cpp \newline g++ -c Country.cpp \newline \newline census:census.o BigInteger.o Country.o \newline g++ census.o BigInteger.o Country.o -o census} \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}{C++ Traverse Linked List using for-loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{void TraverseList(Node {\emph{head)\{ \newline for(Node }}n = head; n -\textgreater{} next != NULL; n = n -\textgreater{} next)\{//do something to n\} \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}{C++ Operator overloading}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Using friend functions \newline BigInteger operator+(const BigInteger \&a, const BigInteger \&b); \newline istream \&operator \textgreater{}\textgreater{} (istream \&cin, BigInteger \&b); \newline ostream \&operator \textless{}\textless{} ostream \&cout, BigInteger \&b);} \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}{C AVL Tree - Node and maximum}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{struct treeNode \{ \newline int key; struct treeNode * left; \newline struct treeNode * right; \newline \} \newline typedef struct treeNode treeNode; \newline int maximum(int a, int b)\{ \newline return (a \textgreater{} b ? a : b); \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}{C AVL - Rotation Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{treeNode* R\_rotation(treeNode *parent)\{ \newline treeNode *child = parent -\textgreater{} left; \newline parent -\textgreater{} left = child -\textgreater{} right; \newline child -\textgreater{} right = parent; return child;\} \newline \newline treeNode* L\_rotation(treeNode *parent)\{ \newline treeNode *child = parent -\textgreater{} right; \newline parent -\textgreater{} right = child -\textgreater{} left; \newline child -\textgreater{} left = parent; return child;\} \newline \newline treeNode* LR\_rotation(treeNode *parent)\{ \newline treeNode *child = parent -\textgreater{} left; \newline parent -\textgreater{} left = L\_rotation(child); \newline return R\_rotation(parent);\} \newline \newline treeNode* RL\_rotation(treeNode *parent)\{ \newline treeNode *child = parent -\textgreater{} right; \newline parent -\textgreater{} right = R\_rotation(child); \newline return L\_rotation(parent);\}} \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}{C AVL - Insert()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{treeNode* Insert(treeNode *currentNode, int key)\{ \newline if(currentNode == NULL)\{ \newline currentNode = \seqsplit{(treeNode*)malloc(sizeof(treeNode));} \newline currentNode -\textgreater{} key = key; \newline currentNode -\textgreater{} left = currentNode -\textgreater{} right = NULL; \newline \} \newline else if(key \textgreater{} currentNode -\textgreater{} key)\{ \newline currentNode -\textgreater{} right = Insert(currentNode -\textgreater{} right, key); \newline currentNode = \seqsplit{balance\_tree(currentNode);} \newline \} \newline else if(key \textless{} currentNode -\textgreater{} key) \{ \newline currentNode -\textgreater{} left = Insert(currentNode -\textgreater{} left, key); \newline currentNode = \seqsplit{balance\_tree(currentNode);} \newline \} \newline else \{ \newline printf("fail! - duplicated key \textbackslash{}n"); \newline exit(-1); \newline \} \newline return currentNode; \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}{C AVL - get\_height}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int get\_height(treeNode *currentNode) \newline \{ \newline if(currentNode == NULL) \newline return 0; \newline else\{ \newline int height = 1 + maximum(get\_height(currentNode-\textgreater{}left), get\_height(currentNode-\textgreater{}height)); \newline return height; \newline \} \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}{C AVL - getBalance()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int get\_balance(treeNode * currentNode)\{ \newline if(currentNode == NULL) return 0; \newline else return get\_height(currentNode-\textgreater{}left) - get\_height(currentNode-\textgreater{}height); \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}{C AVL - balance\_tree()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{treeNode* balance\_tree(treeNode * currentNode)\{ \newline int height\_diff = \seqsplit{get\_balance(currentNode);} \newline if(height\_diff \textgreater{} 1) \newline \{ \newline \seqsplit{if(get\_balance(currentNode} -\textgreater{} left) \textgreater{} 0)\{ \newline currentNode = \seqsplit{R\_rotation(currentNode);} \newline \} else \{ \newline currentNode = \seqsplit{LR\_rotation(currentNode);} \newline \} \newline \} else if(height\_diff \textless{} -1)\{ \newline if(get\_balance(currentNode-\textgreater{}right) \textless{} 0)\{ \newline currentnode = \seqsplit{L\_rotation(currentNode);} \newline \} else \{ \newline currentNode = \seqsplit{RL\_rotation(currentNode);} \newline \} \newline \} \newline return currentNode; \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}{C AVL - main()}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int main()\{ \newline treeNode *root = NULL; root = Insert(root, 5);\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.44333 cm} x{3.53367 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{5.1 Containers}} \tn % Row 0 \SetRowColor{LightBackground} `vector\textless{}int\textgreater{} v;` & vector definition \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `v{[}i{]}` & i-th item in the vector \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \seqsplit{`v.pop\_back()`} & remove last item \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} `v.size()` & size of vector \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} `list\textless{}int\textgreater{} l;` & list definition \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{`l.push\_front()`} & insert item at front \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{`l.push\_back()`} & insert item at back \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{`l.pop\_front()`} & remove the first item \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} \seqsplit{`l.pop\_back()`} & remove last item \tn % Row Count 16 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{`l.front()`} & access the first item \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} `l.back()` & access the last item \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} `l.size()` & return num of items \tn % Row Count 19 (+ 1) % Row 12 \SetRowColor{LightBackground} `map\textless{}K, V\textgreater{} m;` & map definition \tn % Row Count 21 (+ 2) % Row 13 \SetRowColor{white} `m{[}i{]}` & i-th item in the list \tn % Row Count 22 (+ 1) % Row 14 \SetRowColor{LightBackground} \seqsplit{`m.count(k)`} & return no. of pairs in the map with key = k \tn % Row Count 24 (+ 2) % Row 15 \SetRowColor{white} `m.size()` & no. of items \tn % Row Count 25 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Using map with user define objects}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{bool operator\textless{}(const Record\& a, const Record\& b)\{ \newline return a.name \textless{} b.name; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Must overload "\textless{}" 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}{Directives for STL}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#include\textless{}vector\textgreater{}; \#include\textless{}list\textgreater{}; \#include\textless{}map\textgreater{}; \#include\textless{}algorithm\textgreater{};} \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}{Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Sorting}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`sort(v.begin(), v.end())` // vector} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`sort(a, a+10);` // array} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`c.sort()` // list and maps} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`sort(v,begin(), v.end(), compare);`// descend} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`bool compare(int a, int b)\{ return a \textgreater{} b; \}`} \tn % Row Count 6 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{overload `operator\textless{}()` for special tricks} \tn % Row Count 7 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Binary Search}}} \tn % Row Count 8 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{`binary\_search(v.begin()}, v.end(), target); //returns bool`} \tn % Row Count 10 (+ 2) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{"target" is what you are looking for in v} \tn % Row Count 11 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Upper \& lower bound}}} \tn % Row Count 12 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`lower\_bound(v.begin(), v.end(), target); // returns ForwardIterator"} \tn % Row Count 14 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`upper\_bound(v.begin(), v.end(), target);`} \tn % Row Count 15 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`lower\_bound()` returns the earliest postion} \tn % Row Count 16 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`upper\_bound()` returns the lastest position} \tn % Row Count 17 (+ 1) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`binary\_search()`, `upper\_bound()` and `lower\_bound()` can be used with `vectors`, `lists`, and `maps`} \tn % Row Count 20 (+ 3) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Random Shuffle}} (see appendix)} \tn % Row Count 21 (+ 1) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{need `\textless{}cstdlib\textgreater{}`, `\textless{}ctime\textgreater{}`, `srand(time(NULL))`} \tn % Row Count 22 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C++ STL Template Class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{template \textless{}class T\textgreater{} \newline class MyCollection\{ \newline vector\textless{}T\textgreater{} data; \newline public: \newline void Add(const T \&); \newline \}; \newline template \textless{}class T\textgreater{} \newline void MyCollection \textless{}T\textgreater{} :: Add(const T \& d)\{ \newline data.push\_back(d); \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}{C++ Template Operator Overloading (One-to-one)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{template \textless{}class T\textgreater{} \newline class MyCollection\{ \newline vector\textless{}T\textgreater{} data; \newline public: \newline void Add(T const \&); \newline T \& Draw(); \newline friend ostream \& operator\textless{}\textless{}(ostream \& cout, const MyCollection\textless{}T\textgreater{} \&q)\{ \newline cout \textless{}\textless{} "Collection" \textless{}\textless{} endl; \newline typename vector\textless{}T\textgreater{}::const\_iterator itr; \newline for(itr =q.data.begin(); itr != q.data.end(); itr++) \newline cout \textless{}\textless{} " " \textless{}\textless{} *itr \textless{}\textless{} endl; \newline return cout; \newline \} \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}{C++ Template Overloading (many-to-many)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{template \textless{}class T\textgreater{} \newline class MyCollection\{ \newline vector\textless{}T\textgreater{} data; \newline public: \newline void Add(T const \&); \newline T \& Draw(); \newline template \textless{}class U\textgreater{} \newline friend ostream \& operator\textless{}\textless{}(ostream \& cout, const MyCollection\textless{}U\textgreater{}\& q); \newline \}; \newline template \textless{}class U\textgreater{} \newline ostream \& operator\textless{}\textless{}(ostream \&cout, const MyCollection\textless{}U\textgreater{} \& q) \newline \{ \newline typename vector\textless{}U\textgreater{}::const\_iterator itr; \newline ... (same) \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{3.33459 cm} p{1.64241 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C Conversion Specifier}} \tn % Row 0 \SetRowColor{LightBackground} int & \%d \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} float & \%f \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} double & \%lf \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} char & \%c \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} string & \%s \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C string}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`char name{[}{]} = "Alan";`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`char name{[}100{]}; scanf("\%s", name);`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `\#include\textless{}string.h\textgreater{}` & more functions \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} `strcopy(char s1{[}{]}, char s2{[}{]})` & copy s2 to s1 \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} `strcat(char s1{[}{]}, char s2{[}{]})` & append s2 to end of s1 \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} `strcmp(char s1{[}{]}, char s2{[}{]})` & return -ive if s1\textless{}s2. return +ive if s1\textgreater{}s2. return 0 if s1==s2 \tn % Row Count 11 (+ 3) % Row 6 \SetRowColor{LightBackground} `strlen(char s1{[}{]})` & return length of string \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{C Functions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Pass by reference} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`void swap(double *a, double *b)\{...\}`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Using this function: pass the address} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`swap(\&a, \&b);`} \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`int array{[}{]} = \{1, 2, 3\};`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`a{[}i{]}` is the same as `*(a+i)`} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C Memory Allocation}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`int size; int *a; scanf("\%d", \&size);`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{`a = \seqsplit{malloc(size*sizeof(int));`}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{`free(a); // a is pointer`} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{C Structure and typedef}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{struct student \{ char name{[}20{]}; int uid; \} \newline typedef struct student student; \newline int main()\{ student a; ... \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Python}} \tn % Row 0 \SetRowColor{LightBackground} `int(a); float(a); str(a)` & type casting \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `a+b` & concatenation \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} `s{[}i{]}` & access i-th \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `s{[}1:5{]}` & substring s{[}1{]} to s{[}4{]} \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} `s{[}1:{]}` & s{[}1{]} to end \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} `s{[}:4{]}` & start to s{[}3{]} \tn % Row Count 8 (+ 1) % Row 6 \SetRowColor{LightBackground} `len(s)` & length of the string \tn % Row Count 9 (+ 1) % Row 7 \SetRowColor{white} `print "howdy!", ` & print without newline \tn % Row Count 11 (+ 2) % Row 8 \SetRowColor{LightBackground} `s = input("prompt")` & take user input as strings \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Python File Input}} \tn % Row 0 \SetRowColor{LightBackground} `with open("filename", "mode") as f:` & open file. define scope \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `f.close()` & end of scope \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} `s = f.read();` & read from file \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} `f.write(str(a));` & write to the file \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{file modes}}} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} r & read only \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} r+ & reading and writing \tn % Row Count 9 (+ 2) % Row 7 \SetRowColor{white} w & write only \tn % Row Count 10 (+ 1) % Row 8 \SetRowColor{LightBackground} w+ & writing and reading (overwrite) \tn % Row Count 12 (+ 2) % Row 9 \SetRowColor{white} a & appending \tn % Row Count 13 (+ 1) % Row 10 \SetRowColor{LightBackground} a+ & appending and reading \tn % Row Count 15 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Python Flow of Control}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{\#if-else statement}} \newline if condition: \newline statement \newline elif condition: \newline statement \newline else: \newline statement} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Conditions are not enclosed by brackets} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{1.9908 cm} p{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Python Logical operators}} \tn % Row 0 \SetRowColor{LightBackground} \&\& & and \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} || & or \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} ! & not \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Python For-loops}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for i in list: \newline statement \newline statement \newline \newline {\bf{\#Example 1}} \newline i = 1 \newline for dir in {[} "n", "e", "w", "s" {]}: \newline print "the" + str(i) + "-th direction is" + dir \newline i+=1 \newline \newline {\bf{\#Example2}} \newline for i in range(0, 71): \newline if i \% 7 == 0: \newline print i,} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Python Array}} \tn % Row 0 \SetRowColor{LightBackground} `arr = {[}0{]} * i` & empty array with size i \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `arr{[}i{]}` & i-th item in array \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}