Standard Template Library (STL) in C++

We hope you have already understood about Templates in C++ before knowing about STL(Standard Template Library) in C++. If you don't, then please understand the concept of C++ templates, it will help you understand STL very easily.

STL in C++

1. STL is a Standard Template Library in C++.

2. It is a powerful set of C++ template classes.

3. It is helpful in implementing popular and commonly used algorithms and data structures.

4. STL contains different pre-defined functions which helps you performing various complicated tasks in very easy manner.

5. At the core of C++ Standard Template Library are three well-structured components (containers, algorithms and iterators).


Components of STL

There are 3 main components of STL in C++. They are:

1. Containers
2. Algorithms
3. Iterators


1. Containers are used to manage groups of objects of a certain kind.

2. Container classes store data and objects.

3. Container library in STL provide containers that are used to create data structures like arrays, linked list, trees, map, etc.

4. These containers are generic and can hold elements of any data types.

Example: Vector can be used to create dynamic arrays of char, integer, float and other types.


Some Common Containers

1. Vector : replicates arrays.
2. Queue : replicates queues.
3. Stack: replicates stack.
4. Priority_queue: replicates heaps.
5. List: replicates linked list.
6. Set: replicates trees.
7. Map: associative arrays.

Classification of Containers

1. Sequence Containers
2. Associative Containers
3. Unordered Associative Containers
4. Containers Adapters

Sequence Container

1. Sequence containers are used for data structures that keep objects of same type in a linear sequence.

2. It implement data structures which are accessed sequentially.

Example: array, vector, forward_list, list, deque.

Associative Container

1. Associative containers implement data structures that are quickly searched.

2. It can be classified in two ways namely containers which require unique key and containers which allow multiple entries using name key.

3. It has complexity of O(log n).

Example: set, map, multiset, multimap.

Unordered Associative Containers

1. It provides unsorted data structures that are accessed using a hash. 

2. The access times are O(n) in worst-case and can be reduced to O(1)  but it is much faster than that of linear time for most operations.

3. For each and every STL Unordered Associative Container, a hashed key is used for accessing the data.

4. Like Associative Container, it can also be classified into two types (one which require unique keys and others which do not require unique keys).

5. You can override the hashing function, key comparison function and allocator if you want but remember you must override it during its declaration.

Example: unordered set, unordered_map, unordered_multiset, unordered_multimap.

Container Adapters

1. Container adapters are not full container classes on their own, but wrappers around another containers like vector, list or deque.

2. It simply provide a different interface for sequential containers.

Example: stack, queue, priority_queue.


1. Algorithms act on containers.

2. Algorithms allow you to perform initialization, sorting, searching and transforming containers' contents.

3. Algorithms library contains several built-in functions that perform complex algorithms on the data structures.

4.It provides abstraction which means you necessarily need not to know how algorithm works.

Example: you can reverse a range with reverse() function. You can sort a range with sort() function and you can search in a range with binary_search().


1. Iterators are used to step through the elements of collections of objects. These collections may be containers or its subsets.

2. Iterators in STL are used to point to the containers.

3. Iterators actually acts as a bridge between containers and algorithms.

Example: sort() function have two parameters  ( one is starting iterators and another is ending iterators). sort() function compares the elements pointed by each of the two iterators and arranges them in sorted order.

Thus, it does not matter what is the type of container and same sort() function can be used on different types of containers.

For Example: STL in c++
using namespace std;
int main() 
 // create a vector to store int
 vector <int> vec; 
 int i;

 // display the original size of vec
 cout << "vector size = " << vec.size() << endl;

 // push 5 values into the vector
   for(i = 0; i < 5; i++) {

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;

   // access 5 values from the vector
   for(i = 0; i < 5; i++) {
      cout << "value of vec [" << i << "] = " << vec[i] << endl;

   // use iterator to access the values
   vector <int> :: iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "value of v = " << *v << endl;

   return 0;

vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4

push_back() function inserts value at the end of the vector.
size() function displays the size of the vector.
begin() function returns an iterator to start of vector.
end() function returns an iterator to the end of vector.

<< Previous                                                                                      Next >>
Next Post »