Lecture 20 Flashcards
In C++, there is a vector class as part of the
std namespace.
Arrays
–Good for adding items to the end of lists and for random access to items within the list.
–Bad for cases with many additions and removals at various places within the list.
Linked Lists
–Better for adding and removing items at random locations within the list.
–Bad at randomly accessing items from the list.
What is the function of an iterator?
to get each contained object in order, one at a time, in a controllable manner.
the iterator code
vector numbers; // omitted code initializing numbers. iterator iter; for(iter = numbers.begin(); iter != numbers.end(); iter++) { cout << *iter << ‘ ’; }
In C++, iterators are designed to look like and act something like pointers.
The * and -> operators are overloaded to give pointer-like semantics, allowing users of the iterator object to “dereference” the object currently “referenced” by the iterator.
Both the std::vector and std::list classes of C++ implement iterators.
–begin() returns an iterator to the list’s first element.
–end() is a special iterator “just after” the final element of the list, useful for checking when we’re done with iteration.
Stacks
“Last In, First Out” (LIFO) structure.
–That is, the last input to the structure is the first output obtained from it.
–Consider a stack of papers: when searching through it, one typically starts at the top and searches downward, from newest to oldest.
Stacks are a very good model for
function calls. –Stacks are the model of how recursion mechanically works. –In turn, recursion is necessary for operating upon many data structures.
The data structure known as a queue is a
“First In, First Out” (FIFO) structure.
–That is, the first input to the structure is the first output obtained from it.
–Consider a line of people: the person in front has priority to whatever the line is waiting on… like buying tickets at the movies or gaining access to a sports event.
Queues are significantly like lists, except
that we have additional restrictions placed on them.
–Additions may only happen at the list’s end.
–Removals may only happen at the list’s beginning.
The “deque”, or double-ended queue, combines
the behaviors of stacks and queues into a single structure.
–Items may be added or removed at either end of the structure.
–This allows for either LIFO or FIFO behavior
– it’s all in how you use the structure.
–Mixed behavior is also possible, so beware!
Math done in “standard” (i.e, infixnotation) is typically
first converted to postfix notation for actual computation.
–This “conversion” is known as the Shunting-yard algorithm.
Uses of the C++ std::stack class
–This implementation is something of a “wrapper class” that uses a vector, list, or deque internally, limiting it to stack-like behavior.
–We’ll see deques in a moment.
–The methods push_back(), pop_back(), and back() are designed from a stack perspective.