Standard Template Library The STL is a large part of the C++ Standard Library Major components data structures (containers/collections for data – CS2851) vector (array), list, deque, stack, queue, set, … algorithms Common operations (functions) that can be applied to many STL containers iterators link the algorithms to the data structures. iterators are designed for efficient traversal through the data structures. Similar to pointers Fall 2005 CS-1030 Dr. Mark L. Hornick
Linked Lists – the list class Arranges data in non-contiguous blocks Links them together to create order Stored Object Link to Previous Cell Link to Next Cell Fall 2005 CS-1030 Dr. Mark L. Hornick
list is a Doubly Linked List Last First Size Main List Cell Data Data … Data … Fall 2005 CS-1030 Dr. Mark L. Hornick
list in C++ (STL) Syntax #include <list> using namespace std; list<T> collection; // T: datatype stored in vector Fall 2005 CS-1030 Dr. Mark L. Hornick
list Operations Adding elements Removing an element push_back – At the end push_front – At the beginning insert – Anywhere Removing an element pop_back – From the end pop_front – From the beginning erase, remove - Anywhere Fall 2005 CS-1030 Dr. Mark L. Hornick
list Operations (2) Information accessors/inspectors Modification size – How many? empty – Are there none? Modification sort – NOT part of <algorithm> reverse – change the order Fall 2005 CS-1030 Dr. Mark L. Hornick
The vector class vector is a “smart” array Like the Java ArrayList Storage is in consecutive memory locations Location = start + index*size Very little storage overhead Only need the start and object size 1 … n-1 vector start Fall 2005 CS-1030 Dr. Mark L. Hornick
vectors in C++ (STL) User-specified data type is stored Syntax: #include <vector> using namespace std; vector<T> collection; // T: datatype stored in vector // Like Java’s ArrayList<T> Fall 2005 CS-1030 Dr. Mark L. Hornick
vector Operations Adding elements: Removing an element push_back – At the end push_front – At the beginning insert – Anywhere Removing an element pop_back – From the end pop_front – From the beginning erase, remove - Anywhere Fall 2005 CS-1030 Dr. Mark L. Hornick
lists and vectors usage Example Fall 2005 CS-1030 Dr. Mark L. Hornick
lists vs. vectors Advantages Disadvantages Flexible storage Grows easier Ultimately more data can be stored Disadvantages O(n) access time Loss of indexing Storage overhead is higher Fall 2005 CS-1030 Dr. Mark L. Hornick
Iterators A mechanism for sequentially accessing STL containers More important for list than vector Vector can use indexing for access (e.g. x[i]) Automatically included as part of the container Helps describe the next and previous links Fall 2005 CS-1030 Dr. Mark L. Hornick
Iterator Notation Declaration Operators list<TYPE>::iterator NAME; Operators * Access data element (dereferencing) ++,-- Advance or retreat in the list ==,!= Comparison NOTE: No < or > Fall 2005 CS-1030 Dr. Mark L. Hornick
Iterators in a Linked List Main List Cell .begin() Size First Last Data Data … … Data … .end() Fall 2005 CS-1030 Dr. Mark L. Hornick
Iterator Variations Standard iterators can be used to change a list *iter = newValue; Special iterators for const lists (can’t change) list<TYPE>::const_iterator citer; Reverse iterators list<TYPE>::reverse_iterator riter; list<TYPE>::const_reverse_iterator criter; Fall 2005 CS-1030 Dr. Mark L. Hornick
More on Reverse Iterators For going from end to beginning ++ Retreats -- Advances Bounds rbegin() – Last rend() – One “before” the first Fall 2005 CS-1030 Dr. Mark L. Hornick
Other iterator Member Functions These member functions need iterators insert – Where to insert erase – Which one to erase find – Locate the item that was found Iterators also apply to vectors Usage is the same vector<TYPE>::iterator NAME; Fall 2005 CS-1030 Dr. Mark L. Hornick
STL Algorithms An algorithm is an operation (function) that can be applied to many STL containers The STL algorithms are generic - they can operate on a variety of data structures. STL container classes such as vector and list Program-defined data structures Behavior must satisfy the requirements of a particular algorithm STL algorithms achieve generality by accessing and traversing the elements of a container indirectly through iterators. Iterators must be settable and dereferencable Fall 2005 CS-1030 Dr. Mark L. Hornick
Sort Algorithm The sort algorithm is an operation (function) that can be applied to many STL containers notable exception: list container (has it’s own method) sort() orders a container's contents in ascending order as defined by the operator<() as applied to the container’s elements Programmer-defined types can be sorted just as easily as a built-in type Fall 2005 CS-1030 Dr. Mark L. Hornick
find Algorithm searches a subrange of the elements in a container (or all the elements), looks for an element that is "equal to" a specified value; stops when it finds the first element equal to the specified value the equality operator (==) must be defined for the type of the container's elements. search value must be of the same type as the elements stored in the container or of a type that the compiler can automatically convert. Return value is an iterator specifying the position of the first matching element. If no matching element is found, the return value is equal to the iterator specifying the end of the element subrange Fall 2005 CS-1030 Dr. Mark L. Hornick