Sample Syntax Reference


We often distribute a reference sheet during exams that gives the syntax of common operations so that students do not need to memorize these details. Below is a sample such reference sheet. You may want to have this on hand when practicing or taking an exam.

This list isn’t comprehensive – for that, read the header files (available in Qt Creator) or view the online docs for standard C++ functions or the Stanford C++ libraries.

Standard string

s.at(index) or s[index] // access single char
s + text OR s += text
s.substr(start)     // return new substring
s.substr(start, length)
s.find(key)  // returns index or string::npos
s.erase(index, length)   // erase, insert, replace destructively modify s
s.insert(index, text)
s.replace(index, length, text)
for (char ch: s) { ...  } // chars visited in order

Stanford strlib.h

if (equalsIgnoreCase(s1, s2)) { ... }
if (endsWith(str, suffix))  { ... }
if (startsWith(str, prefix))  { ... }
if (stringContains(str, key)  { ... }

integerToString(int)
stringToInteger(str)
realToString(double)
stringToReal(str)
stringSplit(str, delimiter) // returns Vector<string> of tokens
toLowerCase(str)
toUpperCase(str)
trim(str) // remove leading/trailing whitespace

Vector

Vector<T> v = {elem, elem, ... , elem};
v.add(elem) OR v += elem
v.clear()
v.get(index) OR v[index]
v.insert(index, elem)
if (v.isEmpty()) { ... }
v.remove(index)
v.set(index, elem) OR v[index] = elem
v.size()
for (T elem: v) { ...  }  // visited in order of index

Grid

Grid<T> g(nRows, nCols);
int numCells = grid.numRows() * grid.numCols()
if (grid.inBounds(row, col)) { ...}
grid[row][col] = value
for (T elem: grid) { ...  } // visited left-to-right, top-to-bottom

GridLocation

GridLocation loc = {row, col};
grid[loc] = value
if (grid.inBounds(loc)) { ... }
GridLocation neighbor = {loc.row + 1, loc.col - 1};
if (loc == neighbor) { ... }

Stack

Stack<T> s = {bottom, ... , top};
s.push(elem)
s.peek() // Look at top
s.pop()  // Removes top
s.size()
if (s.isEmpty()) { ... }
s.clear()
// no iterator, no direct access to elements other than top

Queue

Queue<T> q = {front, ... , back};
q.enqueue(elem)
q.peek()  // Look at front
q.dequeue() // Removes front
q.size()
if (q.isEmpty()) { ... }
q.clear()
// no iterator, no direct access to elements other than front

Set

Set<T> s = {elem1, elem2, ... , elemN};
if (s.contains(elem)) { ... }
s.add(elem)
s + elem; s + set2; // union
s.remove(elem)
s - elem; s - set2; // difference
intersectSet = s1 * s2;
s.size()
if (s.isEmpty()) { ... }
if (s.isSubsetOf(s2)) { ... }
s.clear()
// Visits elements in sorted order
for (T elem: set) { ...  }

Map

Map<K, V> m = { { key1, val1}, ...  {keyN, valN } };
m.put(key, value) OR m[key] = value
m.get(key) OR m[key]  // bracket form auto-inserts default value if not present
if (m.containsKey(key)) { ... }
m.size()
if (m.isEmpty()) { ... } 
m.remove(key)
m.clear()
Vector<K> keys = m.keys();
// Visits keys in sorted order 
for (K key: m) { ...  }

Lexicon

Lexicon lex(filename);
if (lex.contains(word)) { ... }
if (lex.containsPrefix(prefix)) { ... }

PriorityQueue (Stanford library version)

PriorityQueue<T> pq;
pq.enqueue(value, priority)
pq.peek()  // Look at frontmost
pq.peekPriority()  // Look at priority of frontmost
pq.dequeue() // Removes frontmost
pq.changePriority(value, priority)
pq.size()
if (pq.isEmpty()) { ... }
pq.clear()
// no iterator, no direct access to elements other than front

ListNode

struct ListNode {
    int data;         
    ListNode* next;  
    // ListNode* prev;  // if problem states that list is doubly-linked
};

TreeNode

struct TreeNode {
    int data;
    TreeNode* left;
    TreeNode* right;
};

Huffman EncodingTreeNode

struct EncodingTreeNode {
    char ch;
    EncodingTreeNode* zero;
    EncodingTreeNode* one;

    EncodingTreeNode(char c); // construct leaf node
    EncodingTreeNode(EncodingTreeNode* z, EncodingTreeNode* o); // construct interior node

    bool isLeaf();
    char getChar();
};