Java, J2EE & SOA Certification Training
- 33k Enrolled Learners
- Live Class
The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. In this STL in C++ article we will discuss the following pointers:
C++ provides us with a feature of templates that allows functions and classes to operate with generic types. This allows the reusability of a function or class and allows it to work on many different data types without being rewritten for each one.
While programming many a time there is a need for creating functions that perform the same operations but work with different data types. So to overcome this problem C++ provides a feature to create a single generic function instead of many functions which can work with different data type by using the template parameter. The collection of these genric classes and functions is called Standard Template Library(STL)
The components of STL which are now part of the standard C++ library are defined in the namespace std. We must, therefore, use the using namespace directive to import them into our program.
Using namespace std;
STL has three components
These three components work together with one another in synergy to provide support to a variety of programming solutions. Algorithm employ iterators to perform operation stored in containers.
A container is an object that stores data in memory into an organized fashion. The containers in STL are implemented by template classes and therefore can be easily modified and customized to hold different types of data.
A procedure that is used to process the data contained in the containers is defined as an algorithm. The STL includes many different kinds of algorithms to provide support to tasks such as initializing, searching, copying, sorting, and merging, copying, sorting, and merging. Algorithms are implemented by template functions.
An iterator can be defined as an object that points to an element in a container. Iterators can be used to move through the contents of containers. Iterators are handled just like pointers. We can increment or decrement them. Iterators connect algorithm with containers and play a key role in the manipulation of data stored in the containers.
STL defines ten containers which are grouped into three categories.
It can be defined as a dynamic array. It permits direct access to any element.
It is a bidirectional linear list. It allows insertion and deletion anywhere
It is a double-ended queue. Allows insertions and deletions at both the ends. Permits direct access to any element.
It is an associate container for storing unique sets. Allows rapid lookup.
It is an associate container for storing non-unique sets.
It is an associate container for storing unique key/value pairs. Each key is associated with only one value.
It is an associate container for storing key/value in which one key may be associated with more than one value (one-to-many mapping). It allows a key-based lookup.
A standard stack follows last-in-first-out(LIFO)
A standard queue follows first-in-first-out(FIFO)
The first element out is always the highest priority element
Sequence containers store elements in a linear order. All elements are related to each other by their position along the line. They allow insertion of element and all of them support several operations on them.
The STL provides three types of sequence elements:
They are designed in such a way that they can support direct access to elements using keys. They are not sequential. There are four types of
All the above containers store data in a structure called tree which facilitates fast
searching, deletion, and insertion unlike sequential. Container set or multiset can store various items and provide operations for manipulating them using the values as the keys.
And map or Multimap are used to store items in pair, one called the key and other
called the value.
The STL provides three derived containers namely, stack, queue, and priority_queue. These are also known as container adaptors.
There are three types of derived containers:
Stacks, queue and priority queue can easily be created from different sequence containers. The derived containers do not support iterators and therefore we cannot use them for data manipulation. However, they support two member function pop() and push() for implementing deleting and inserting operations.
Algorithms are functions that can be used generally across a variety of containers for processing their content. Although each container provides functions for its basic operations, STL provides more than sixty standard algorithms to support more extended or complex operations. Standard algorithms also permit us to work with two different types of containers at the same time.
STL algorithms reinforce the philosophy of reusability. By using these algorithms, programmers can save a lot of time and effort. To have access to the STL algorithms, we must include <algorithm> in our program.
STL algorithm, based on the nature of operations they perform, may be categorized as under :
Iterators act like pointers and are used to access elements of the container. We use iterators to move through the contents of containers. Iterators are handled just like pointers. We can increment or decrement them as per our requirements. Iterators connect containers with algorithms and play a vital role in the manipulation of data stored in the containers. They are often used to pass through from one element to another, this process is called iterating through the container.
There are five types of iterators:
Direction of movement
Cannot be saved
Cannot be saved
Can be saved
Forward and backward
Can be saved
Forward and backward
Can be saved
Different types of iterators must be used with the different types of containers such that only
sequence and associative containers are allowed to travel through iterators. Each type of iterators is used for performing certain functions. The input and output iterators support the least functions. They can be used only to pass through in a container. The forward iterators support all operations of input and output iterators and also retain its position in the container. A Bidirectional iterator, while supporting all forward iterators operations, provides the ability to move in the backward direction in the container.
Thus we have come to an end of this article on ‘STL in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.