STL vector, list and deque
Due to the problem with templates, we never got to the STL data structures in
the last lecture, other than STL strings. Today, we will be introducing a few data
structures provided by the STL library: vector, list and deque.
We will also talk about iterators.
They are all pretty simple conceptually. To find out how to use them, this
page
of references provide pretty much all you need. Chapter 21 on your textbook
also has information about STL data structures.
For the rest of the time today, you'll get to work on your projects.
Templates classwork solution:
#include <iostream.h>
template <class T> class StackNode;
template <class T> class Stack;
template <class T>
class StackNode {
public:
T value;
StackNode<T>* next;
StackNode() {
value = 0;
next = NULL;
};
StackNode(T val, StackNode* ne) {
value = val;
next = ne;
};
// friend class Stack;
};
template <class T>
class Stack {
public:
StackNode<T>* top;
Stack() {
top = NULL;
};
~Stack() {
while (!isEmpty())
pop();
};
void push(T val) {
top = new StackNode<T>(val, top);
};
T pop() {
T temp = top->value;
StackNode<T>* ptr;
ptr = top;
top = top->next;
delete(ptr);
return temp;
};
T getTop() {
return top->value;
};
bool isEmpty() {
if (top == NULL)
return true;
return false;
};
void printStack() {
StackNode<T>* ptr;
ptr = top;
while (ptr != NULL) {
cout<<ptr->value<<" ";
ptr = ptr->next;
}
cout<<endl;
};
};
int main() {
Stack<int> intstack;
Stack<char> charstack;
intstack.push(7);
intstack.push(9);
intstack.printStack();
charstack.push('a');
charstack.push('i');
charstack.printStack();
return 0;
}