Show Menu
Cheatography

c++ Cheat Sheet (DRAFT) by

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

<fs­tre­am>

ifstream
ofstream

<cs­tri­ng>

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")

<cm­ath>

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

<cs­tdl­ib>

atoi(str) converts str to int; atof(str) converts str to double; use with c strings (char *)
exit(1)
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.

Pointers

- 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;

Strings

- 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

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

<al­gor­ith­m>

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
replac­e(b­egi­n,e­nd,­old­val­,ne­wval)
sort(b­egin, end)

<cc­typ­e>

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;

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

Streams

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

- discon­nect: stream.cl­ose();

stream.op­en(­"­nam­e")
if (strea­m.f­ail()) {
sorry + exit }

- in_str­eam.ge­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

in_str­eam.ge­t(ch)
while (! in_str­eam.eof()) {
cout << ch;
out_st­rea­m.p­ut(ch);
in_str­eam.ge­t(ch) }

- streams must be reference & arguments in functions only

GDB

- '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

Loops

- Do { ... } While (bool)

switch (selector) {
case label1:
statement;
break;
default:
statment;
break; }
 

<ar­ray>

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]

<st­rin­g>

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

Makefiles

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

Arrays

- 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)

#ifndef HEADERNAME_H
#define HEADERNAME_H

#include directives
Using directives

statements

#endif

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

clean:
   rm -f $(OBJ) $(EXE)