Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Chapter 15 Standard Template Library (STL)
The STL STL is Standard Template Library, a collection of generic software component (generic containers) and generic algorithms, glued by objects called Iterators. STL is a different type of library. It has quite a large number of non member functions designed to work on multiple classes of container types.
The non member advantage Feasible to implement all operations in most efficient manner many useful algorithms like find(), replace(), merge(), sort() are implemented in STL Being non member function they can be used with any container even a newly designed one
The generic software components These containers are classes which can in turn contain other objects. Sequence containers Vector, List, DeQueue Sorted associative containers Set, Map, Multi-set, Multi-map Adapted containers Stack , Queue
The Iterators These are pointer like objects They are categorized into various categories unlike pointers. The generic algorithms are written to work on iterators objects rather then any data structure. Containers are also designed in terms of iterators rather then normal pointers.
Types of Iterators Find algorithm works on input Iterators sort is defined to require Random Access Iterators list (the doubly linked list) container is defined to have Bi-directional Iterator Thus sort can not be used with list!
Generic programming Idea of Generic Programming has nothing to do with C++! It is a mechanism of designing generic software components like Vector, list, deque and so on. Unlike normal programming practice, Generic algorithms are not designed having any software component in mind.
Generic programming Designed keeping in mind minimum level of requirements. Can work with many variants of different software components The software components (containers) and algorithms are connected to each other by Iterators as mentioned earlier
The object based model The inefficiency of object oriented model The model presented by STL offers similar advantages with more efficient architecture The generality achieved here is by templates, Iterators and good design rather then having class hierarchy 5/26/2018 The C2C Programme
The Containers STL provides tested and debugged software components available readily. They are reusable in the sense that we can use them as a building block for any other software development projects.
Advantages of containers Small in number Generality Efficient, Tested, debugged and standard Portability and reusability Automatically adjusting growth Similar behavior to built-in types Extensibility
Advantages of Generic Algorithms readymade from STL. Using the best of the breed mechanisms to be as efficient as possible Standardized so have more acceptability then proprietary algos Similar semantics are designed for all these algorithms.
Iterators We need efficient methods to traverse these containers Iterators provide us the way to do that The Functional interface and pointer like interface The array to be accessed using pointers The get() and put() used by files
Iterators The Iterators are provided by the STL designers for finer control. They are not only provided, they are open to the programmer
Iterators Programmers can define and use Iterators like pointers to the component objects, dereference them assign them and so on like pointers.
Why not Pointers? The reason is that although in most of the cases the iterators are implemented as pointers, one can choose the more fitting implementation for a special case.
The Efficiency of Iterators Open design:- having Iterators open to programmers to manipulate Address manipulation by which the job is possible to be done in minimum steps We can have additional checks possible before applying algorithm to contents like checking to see if the sort algorithm is provided with Random Access Iterators and nothing else
The Vector #include <vector> vector <Student> vs; vector <int> vi; vi.push_back(12); //inserting at the end of vector! Student Lara(1,"Brian Charles Lara"); vs.push_back(Lara);
The List operations Implemented using doubly linked list. Insertion at random place is most efficient constant time operation at every location in the list. Vector has only constant time operation in the end deque in the beginning and at the end and not at every place
The Dequeue deque <Student> ds; ds.push_front(Lara);
The Sorted Associative Containers Three is a need for a container which always remains sorted. All database indexes are examples of such container. Every element inserted in sorted order automatically object which is inserted must obey some form of ordering using operator <() const 5/26/2018 The C2C Programme
The Map It is a container which can have two items, a key and a value stored in itself. It is automatically sorted on the key item at the time of insertion The key item must have some form of ordering. For a user defined objects, it is important to overload operator < as const to have ordering
The Set The set is collection of keys only in sorted form. When we are interested only in verifying if a key is a valid we can use sets We can insert all keys and when the key is referenced we can see if key belongs to inserted data or not.
Algorithms: Find int *pos = find(IntArray, IntArray+6, 3); StudentIndex = find(vs.begin(),vs.end(), Maradona); ListIndex = find(ls.begin(),ls.end(),Tiger); DeqIndex = find(ds.begin(),ds.end(),Steffi); Find returns same type of Iterator which is passed as first and second argument. 5/26/2018 The C2C Programme
Algorithms: Find Find() can work with all the types of containers because the Iterators expected are of category called Input Iterators which are supported by all containers.
Algorithm: Copy copy(vs.begin(),vs.end(),AnotherVector.begin()); copy(vs.begin(),vs.end(),ds.begin()) copy(vs.begin(),vs.end(),ls.begin()); copy has three arguments. The first two arguments are same type of iterators indicating a range of values. The third Iterator can be of different type then type of first two Iterators