C++ & STL

Standard Template Library (STL)

What is STL?

Just like the name, it is a programming library in C++ with set of objects called as containers, capable of processing the data stored in them with the help of algorithmic functions already defined in the library. In other words, most of the tasks related with the implementation of algorithms are executed through the functions defined in STL. Precisely, it is an integration of algorithms and containers to make the programmer’s life easy. Containers hold the data and algorithms process the data irrespective of the type of data and the type of the container. Algorithms are implemented as function templates and the data are processed by passing containers as arguments.  The traversal on the container is performed with the help of pointers called as iterators.  Fundamentally, algorithms, containers, functions and iterators together make the STL work easily for the programmers.  We will discuss each of these one by one, later in this discussion.

Templates in C++

Let’s start with “Templates”, one of the advanced topics in C++. Template is one of the advanced and powerful concepts in C++.  As the name suggests, a program using templates can handle any type of data irrespective of its type. If you have a function that take two arguments of type integer or char, you can call that function only by passing int and char as arguments.  In case if you have a task to be performed that processes a double or float value along with a char, you will have to define another function that accepts double and char as arguments to the function. Templates offer generic programming where the type of data being passed to the function is irrelevant.

Templates are the base for the Standard Template Library (STL) in C++. Quite a good number of algorithms and mathematical functionalities have been added to the library in C++14.

Let us assume that you have set of student data of a class with n students. You are asked to generate two lists. One is based on the alphabetical order of their names and second is based on the highest marks. In this case, you have to write a program with two functions. One is to sort according to the order of marks and other according to the alphabetical order of the names.

To solve the problem and to generate the desired output, you need to have a data structure that can hold this many data and an efficient algorithm for sorting the input data. In conventional programming, you can use arrays to store the data if know the number of student records that is being given as input. If it is unknown, you can use a linked list created dynamically with the help of structure. Following are the steps to solve the above problem in conventional programming:

  1. Define two array variables to store the data
  2. Define two functions to process the same

If you are using a template in C++, there is a simplification in the definition of two functions. Instead of two, you need to define only one function. “template” and “typename” are keywords. “T” is a user defined type. mySort function returns “T” type.  In the first call from the main function, string array is passed for sorting. In the second call, double array is passed to the template function.


//One function works for all data types.  This would work

//even for user defined types if operator ‘>’ is overloaded

template <typename T>

T mySort(T x[])


   //logic to sort the array


int main()




   return 0;


Class Templates

A class template is nothing but a higher version of function template. At this point, we have to start thinking of a class object as a container to hold any type of value.

#include <iostream>

using namespace std;

template <typename T> //define the class as template.

class Array {


T *ptr; //pointer ptr is user defined type i.e., T

int size;


Array(T arr[], int s); //constructor that accepts T type array

void print();


template <typename T> //Constructor

Array<T>::Array(T arr[], int s) {

ptr = new T[s];

size = s;

for(int i = 0; i < size; i++) //assign values to pointer array

ptr[i] = arr[i];


template <typename T> //define the function with return type as T.

void Array<T>::print() {

for (int i = 0; i < size; i++)

cout<<” “<<*(ptr + i);



int main() {

int arr[5] = {1, 2, 3, 4, 5}; //initialize array

Array<int> a(arr, 5); //pass the array values through constructor

a.print(); //print the array

return 0;


The definition of the class starts with “template” keyword and is followed by typename”. The name of the template here is “T”. The pointer declared as a member variable is of type “T”. It means, it is possible to store any type of value in the pointer variable.  Note that the constructor takes a parameter of type T. This means, when you create an object, you can pass an array to the pointer variable in the class. And keep in mind that you can pass any type of value.  It can be float, integer, string, etc. “print()” prints the contents in the array.

To conclude with, a template is something that can accept any type of value to be stored in a container. In the above example, object “a” can contain any type of value stored in *ptr.


A namespace in C++ is like creating a group or collection of functions / variables / objects / classes. A function is a collection of code to be executed when the function is called. A function can have variables which are local to that function. Now, in C++, we have functions as members inside the class. We create objects of classes to access the member functions. Imagine a group or a collection that has all these in it, called as Namespace. If there is a need to have two functions with the same name, it can be part of two different namespaces. Namespaces are used in a program with the keyword “using”. Most commonly used namespace is the “std” – the standard namespace to use the keywords “cout” and “cin”. “std” means standard. If you wish to access cin and cout without “using” namespace in your program, then you have to write the code as std::cin and std::cout.  Example:

namespace myNamespace


int a, b;



Read more here: http://www.cplusplus.com/doc/tutorial/namespaces/

Comments are closed