Show Menu
This is a draft cheat sheet. It is a work in progress and is not finished yet.




strcpy­(de­sti­nation, source) incl. sentinel char
strcat­(de­sti­nation, source)
strcmp­(str1, str2); if = 0 then equal, if >0 then str1 is greater else str1 is smaller
strlen­(char* str) returns int; not include sentinel
strncp­y(d­est­,src, number of charac­ters)
strnca­t(d­,s,n); strncm­p(d­,s,n)
strchr­(char str, char) returns a pointer (char) to the first occurence of char in str else returns null
strstr­(str1, str2) returns pointer to first occurence of str2 in str1 (e.g. ptr = strstr­(str1, "­hel­lo")


abs(int) returns the absolute of the int
pow(base, exponent) returns the power
sqrt(n­umber) returns the square root
ceil(n­umber) rounds up; floor(­number) rounds down


atoi(str) converts str to int; atof(str) converts str to double; use with c strings (char *)
x = rand() % number; srand(­tim­e(NULL) requires <ti­me.h­>
itoa(v­alue, array, base); value = str to be converted; array = c string array; base = 10, 2, 16, etc.


- Pointer is the memory address of a variable (i.e. int * num_ptr)

- Use typedef to declare variable type (i.e. typedef int* num_ptr -> num_ptr X;)

- Create dynamic var: ptr = new type; delete dynamic var: delete ptr

- Deleting dynamic var does not delete the ptr just what is stored inside it

- Make sure to assign null to ptr after delete so it is not dangling

ptr_a = new (nothrow) int;
if (ptr_a == NULL) {
cout << "­sor­ry";
exit(1); } <- ptr set to null if allocation fails

- array identi­fiers are pointers
int hours[5];
int* ptr;
ptr = hours; <- both point to first index

- hours[1] is equivalent to *(hours+1)

- char phrase[] is equivalent to char* phrase

int* number­_ptr;
number_ptr = new int[10] <- dynamic array
delete [] number­_ptr;


- Sentinel character is '\0' and marks the end of string

- char phrase[5] = {'A', 'B'} or char phrase[] = "­Hel­lo"

- Using >> to input strings is limited because it ends at white space

-­tli­ne(­str­ing­_name, 80) is often used instead


swap(var1, var2) swaps the 2 variables; also works arrays­/vector
min(va­l1,­val2), max(va­l1,­val2);
find(b­egin, end, val); if no found return end
sort(b­egin, end)


tolowe­r(char) converts to lowercase; touppe­r(char) converts to upper case
isalph­a(char) returns true if char is alphabetic (0 = false)
isalnu­m(char) returns true if char is decimal, upper/­lower
isblan­k(c­har), isspac­e(char) return true if char is either ' ' or \n
isuppe­r(c­har), islowe­r(c­har), isdigi­t(char) are all boolean
ispunc­t(char) returns true if char is punctu­ation

letter to number / number to letter

int -> char
int number = 40;
char letter = number;

char -> int
char letter = 'c';
int number = letter;

ASCII for 0 = 48
ASCII for space = 32
ASCII for A = 65
ASCII for a = 97


- connect: stream.op­en(­"­nam­e"); <- connects to the beginning of the file

- discon­nect:­ose();

if (strea­m.f­ail()) {
sorry + exit }

- in_str­­t(ch) -> assigns ch the next char in the file and reposi­tions file

- out_st­rea­m.p­ut(ch) -> puts ch in next position in file

- in_str­eam.pu­tba­ck(ch) -> puts back in the file but does not alter file

- char = in_str­eam.peek() assigns char the next char in file but does not move it forward

while (! in_str­eam.eof()) {
cout << ch;
in_str­­t(ch) }

- streams must be reference & arguments in functions only


- 'gdb' to start gdb
- Start with 'run' or 'r'
- Set breakpoint with 'break'
- Print variab­le_­name, &v­ari­abl­e_a­ddress
- 'Watch' variab­le_name
- 'C' for continue
- Execute next line using 'step' or 's'
- 'Next' or 'n'
- Pressing enter repeats last command
- 'q' to quit
- 'finish' to stop execution of current function


- Do { ... } While (bool)

switch (selector) {
case label1:
break; }


array_­nam­e.b­egin() / array_­nam­e.end = first and last elements in the array; also .front­/.back
array_­nam­e.s­ize() = number of elements in the array vs. sizeof­(ex­pre­ssi­on/­type) = bytes
array_­nam­e.m­ax_­size() returns max number of elements allowed in array
array_­nam­e.e­mpty() returns true if array size is 0 meaning no elements
array_­nam­e.a­t(int) = array at position int like array_­nam­e[int]


getlin­e(i­npu­t_s­tream, str, option­al_­limit)
begin(­array); end(ar­ray); .begin­/.end


exe: main.o exe.o
(tab) g++ -g main.o exe.o -o exe

main.o: main.cpp exe.h
(tab) g++ -Wall -c main.cpp

exe.o: exe.cpp exe.h
(tab) g++ -Wall -c exe.cpp

Input/­Output with >> & <<

- out_stream << 34 << ' ' would result in '3', '4', ' ' as characters in output file

- doing input_­stream >> number would take all characters together until reaching a blank space

- >> skips over blank space no matter the data type


- When declaring a function that passes an array use []

- When passing actual array to function no need to use []

- Array parameters are essent­ially reference parameters but not need to use &

- Pass the size of the array in the function as well

- Add const before array in function if you don't want it to change the array e.g. const int array_­name[]

- 2D arrays -> int array_­nam­e[r­ow]­[co­lumn]

- functi­on(­arr­ay[­][c­olumn])

Header file (function declar­ations)


#include directives
Using directives



Another Makefile

OBJ = main.o tube.o helper.o
EXE = tube
CXX = g++
CXXFLAGS = -Wall -g

$(EXE): $(OBJ)
   $(CXX) $(OBJ) -o $@

%.o: %.cpp
   $(CXX) $(CXXFLAGS) -c $<

main.o: tube.h

tube.o: tube.h helper.h

helper.o: helper.h

   rm -f $(OBJ) $(EXE)