Introduction-: We have seen how template can be used to create generic class and function that could extended support for generic Programming. In order to help the c++ user in generic programming.Alexander Stepanov and Meng Lee of Hewlett packard developed a set of general purpose templatized (algorithms) that could be use as a standard approach for storing and processing data.
The collection of these generic classes and function is called the standard template library(STL). The standard template library has now become part of the ANSI standard c++ class library. STL is a large and very complex.
Using stl can save considerable time and effort and lead to high quality program.All these benefits are possible because we are basically “reusing” the well written and well tested components defined in stl.
STL components which are now part of the standard template C++ library are defined in the namespace std. We must therefore use the using namespace directive.
using namespace std;
Components of standard template library-: The STL has several components. But at its core are 3 components. these are the
These three components work in conjunction with one another to provide support to a variety of programming solution. The relationship between the three components is shown here
Containers in c++ –: A container is an object that actually stores data. it is away data is orgainized in memory . The STL containers are implemented by template classes and therefore can be easily customized to hold different data type.
The STL define 10 containers which are grouped into three categories these are
vector –: a dynamic array. allow insertion and deletion at back.permits direct access to any element.
header file <vector>
List-: A bidirectional linear list. Allow insertion and deletion anywhere.
header file <list>
Deque-: a double ended queue.Allow insertion and deletion at both the ends. permit direct access to any element.
header file <deque>
set-: an associate container for storing unique sets.allow rapid lookup.
header file <set>
Multiset-: an associate container for storing non unique sets.
header file <multiset>
Map- an associate container for string unique key pairs.
header file <map>
multimap- an associate container for storing key/value pairs in which one key may be associated with more than one value.
header file <map>
stack-:a standard stack. Last in first out.
header file <stack>
queue-: a standard queue. first in first
header file <queue>
priority queue -: The first element out is always the highest priority elements.
header file <queue>
Sequence container-:It store elements in a linear sequence.
Associate containers -: these are designed to support direct access to elements using keys.They are not sequential.
Derived containers -: This is known as containers adaptors.
Algorithms in c++-: An algorithm is a procedure that is used to process the data contained in the containers.The STL include many different kinds of algorithm to provide support to task such as initializing searching copying sorting and merging.
These are function that can be used generally across a variety of containers for processing their contents. Although each container provide function for its basic operation STL provide more than sixty standard algorithm to support more extended or complex operation.
Standard algorithm also permit us to work with two different type of containers at the same time.
STL algorithm reinforce the philosophy of reuse-ability. by using these algorithm programmers can save a lot of time and effort. To have access to the stl algorithm we must include <algorithm> in our program.
STL algorithm based on the nature of operation they perform may be categorized under
- Retrieve or non mutating algorithm
- Mutating algorithm
- Sorting algorithm
- Relation algorithm
Iterator -: It is an object that point to an element in containers. we can use iterator to move through the contents of containers. Iterator are handled just like pointer. we can increment or decrements them.Iterators connect algorithm with containers and play a key role in the manipulation of data stored in the containers.