Standard Template Library • The Standard Template Library was recently added to standard C++. • The STL contains generic template classes. • The STL permits the development of general programs that do not depend on the underlying container.
Standard Template Library • The STL is composed on six types of components: • Containers • Iterators • Generic Algorithms • Function Objects • Adaptors • Allocators
Containers • The Containers hold data for manipulation.
Sequence Containers • The Sequence Containers are vector, deque, and list. • vector and deque are both based upon arrays. • vectors can change size dynamically and can be assigned to one another. • Essentially a resizable array. • The vector class provide the best random-access performance. • The vector class permits insertions and deletions at the back of the vector.
Sequence Containers • The deque container is a double-ended queue and provides efficient index access to data. • The deque container provides insertions and deletions at both the front and back of the queue. • The list container implements a doubly linked-list. • The list container provides insertions and deletions anywhere in the list.
Associative Containers • The Associative Containers provide direct access to store and retrieve elements via a key. • The keys of associative containers are maintained in a sorted order. • The iterator will traverse through the container in sorted order.
Associative Containers • The set container provides rapid look-up but does not permit duplicates. • If an attempt is made to insert a duplicate key, the duplicate is ignored. • A multiset provides rapid look-up but does permit duplicates. • The order of data stored in a multiset is determined by a comparator function object that is specified when the container is created. typedef std::multiset< int, std::less<int>> ims; ims intMultiSet;
Associative Containers • The map class provides rapid lookup using a rapid key based lookup. • The map class does not permit duplicates. • A map is commonly called an associative array. • Insertions and deletions occur anywhere in the map. • The map elements are pairs containing a key and a value.
Associative Containers • The insertion of an element into a map requires a pair object that has the key and a value. • The multimap container is the same as the map container but it permits duplicates. • A typical use of the multimap is to implement red-black binary trees.
First-Class Containers • Sequence and Associative Containers are considered first-class containers. • First-class containers support iterators. • First-class containers support actual data structure implementation that store elements.
Containers Adopters • Container Adopters are consider near-containers because they are similar in concept to first-class containers but do not provide all of the capabilities of first-class containers. • Container Adopters do not provide the actual data structure implementation into which elements are stored because adopters do not support iterators.
Container Adopters • Container Adopters adapt to other components for special purposes. • A stack container adopter will adapt a vector, list, or deque so that the container will appear to the user as a stack that defines a LIFO data structure. • Permits the use of Push and Pop. • The queue container adopter permits the insertion of data at the back of the underlying data structure and deletions from the front of the underlying data structure. • The typical FIFO data structure.
Container Adopters • The priority_queue adopter provides insertions in sorted order into the data structure and deletions from the front of the data structure. • By default this priority_queue is implemented using the vector class. • Insertions occur such that the highest priority item (for example, the largest value) is inserted at the front of the queue.
Iterators • Iterators provide the ability to move through containers. • Iterators are the interface between the containers and the algorithms that manipulate the data. • Iterators are implemented separately from the container in the STL. • This is different from other “object-oriented” libraries. • It does permit a high level of reuse but reduces the object-oriented nature of the program.
Iterators • Iterators are used as pointers to the elements in first-class containers. • The type of iterator associated with a container determines if that container can be used with specific algorithms in the STL. • Containers that support random-access iterators can be used with all algorithms in the STL. • Figure 20.8 list the type of iterators permitted for each type of container.
Iterators • There are five types of iterators. • Input, output, forward, bi-directional, and random access. • There are also constant iterators that can not modify the containers they are associated with. • Additional iterator variations are reverse iterators and iostream iterators.
Iterators • The operator++ is required to move through a container with a forward iterator. • The operator++ and operator– can be used to move through a container with a bi-directional iterator. • Random access iterators permit operator++, operator--, operator+, operator-, and iterator arithmetic. • Ordinary pointers are automatically random access iterators.
Iterators • The begin() function returns an iterator pointing to the first element in the container. • The end() function returns an iterator pointing to the first element past the end of the container. • The first element that does not exist.
Algorithms • The STL provides algorithms that are used generically across the containers. • There are about 70 algorithms implemented in the STL. • Algorithms manipulate the elements of containers indirectly through the iterator. • The design of the STL algorithms, iterators, and containers avoids the overhead of virtual function calls.
Algorithms • Some algorithms manipulate sequences of elements defined in iterator pairs. • The pairs contain a first iterator pointing to the first element to manipulate and the last iterator that points to one past that last element to manipulate.
Algorithms • Mutating-sequence algorithms modify the containers that they are applied to. • Such algorithms include: • copy(), remove(), fill(), replace(), rotate(), etc. • A list of mutating-sequence algorithms can be found on page 984, Figure 20.11
Algorithms • Non-mutating-sequence algorithms do not modify the containers they are applied to. • Such algorithms include: • count(), equal(), find, search(), etc. • A list can be found on page 985, Figure 20.12.
Algorithms • Numeric algorithms obviously conduct numerical manipulations of the data in the container. • The <numeric> header file must be included to use these algorithms. • A list of the algorithms is found on page 985, Figure 20.13
Allocators • Allocators are used to allocate and deallocate memory for data storage. • Allocators are used in place of new and delete. • Allocators are most useful on machines that have several memory models available, for example IBM PCs. • The allocators decouple the algorithms from assumptions about a particular memory model.
Example • Create a vector of integers and replace certain elements.
Examples • Store integers into a list. • Sort integers that are stored in a vector. • Sum input integer values.
Advantages of Using STL • There are many benefits of using the STL: • The STL provides a significant level of flexible functionality. • The STL provides very good performance at low run-time space costs.
Advantages of Using STL • The STL only uses generalized algorithms when their efficiency is good. • The STL also provides specialized algorithms. • For example, the generic sort algorithm works well on deques and vectors and could also be used for lists but the efficiency would be poor. • A special optimized sort algorithm for lists is provided.
Disadvantages of Using STL • As with anything, there disadvantages of using the STL: • Programming with the STL does not increase the safety of the programs. • Programming with the STL requires to time to adjust to the differences of standard object-oriented programming. • Containers, iterators, and algorithms.
Disadvantages of Using STL • Iterators have all the efficiency and drawbacks of pointers, since they generalize. • The programmer is still responsible for verifying subscript bounds, dangling references, deallocation issues, etc. • For example, the vector class permits the use of the operator but the subscripts are not checked.
Disadvantages of Using STL • The error messages provided by the compiler for STL issues are very poor. • The error messages detailed all the nested templates used at the time.
List Error "/opt/SUNWspro/SC5.0/include/CC/./algorithm.cc", line 1015: Error: The operation "std::list<int, std::allocator<int>>::iterator - std::list<int,std::allocator<int>>::iterator" is illegal. "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: While instantiating "std::__final_insertion_sort<std::list<int,std::allocator<int>>::iterator>(std::list<int, std::allocator<int>>::iterator, std::list<int, std::allocator<int>>::iterator)". "/opt/SUNWspro/SC5.0/include/CC/./algorithm", line 776: Where: Instantiated from non-template code.