The STL
- Reference for this section:
- Stroustrup, Bjarne. The C++ Programming Language, Third Edition,Addison-Wesley, 1997
- The Standard Template Library Containers
- Provides access to a standard set of ADT's.
- vector<T> - a one dimensional vector (array)
- list<T> - a doubly linked list
- deque<T> - a double ended queue (insert at front and back)
- queue<T> - a queue
- stack<t>
- Others
- Design Criteria
- Flexibility
- Common user interface
- We still need to know what is efficient and what is not.
- Ie with a common user interface, we might use a list where we
want an array.
- Each type has additional operators.
- The Vector container
- #include <vector>
- vector <type> V;
- Iterators
- vector <type>::iterator i;
- vector <type>::const_iterator ci;
- vector <type>::reverse_iterator ri;
- vector <type>::const_reverse_iterator cri;
- Const iterators point to items that we can't change.
- V.begin() - points to the first element in the vector
- V.end() points one past the last
- for(i=V.begin();i != V.end(),i++)
- The same is true for V.rbegin() - the last element
- V.rend() - one past the first (a non element);
- Iterators and reverse iterators are not the same type
-
- Insertion and extraction
- V.push_back(item) inserts item at the end of the list.
- V.pop_back - deletes the last item from the list, no return
- V[i] - accesses element i
- V.at(i) same as V[i]
- V.insert(iterator pos, item) inserts at poisition pos
- V.clear() - remove all elements from the vector
- V.back() - reference to last element.
- V.front - reference to first element.
- Others
- V.erase(iterator) - remove this item
- = - make a copy of this vector
- V.size() - the number of elements in the vector
- V.Swap(vector) - swaps two vectors
- comparison operators , ==, !=, <, <=, >, >=
- As we will see later, resizing an array has some expense associated
with it, so we can give an initial size.
- vector <element> name (size);
- a silly example
- Stack
- Notice, a vector with push_back, and back and pop_back() will work just fine.
(for push, top, and pop)
- "This happens not to be my favorite style of stack, but it's arguably more efficient and it's the standard", Stroustrup
- Has pop(), push(), and top()
- But pop does not return a value.
- list
- Vector sans at and []
- has splice, merge, and sort
- has push_front, pop_front
- deque - double ended queue
- queue
- built on dequeue
- added functions front(), back(), push(), pop()
- pop does not return a value.
- some code