Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

STANDARD TEMPLATE LIBRARY

STL is an acronym for standard template library. It is a set of C++ template classes that
provide generic classes and function that can be used to implement data structures and
algorithms .STL is mainly composed of :

1. Algorithms
2. Containers
3. Iterators

STL is a generic library , i.e a same container or algorithm can be operated on any data
types , you don’t have to define the same algorithm for different type of elements.
For example , sort algorithm will sort the elements in the given range irrespective of
their data type , we don’t have to implement different sort algorithm for different
datatypes.

C++: Algorithms in STL


STL provide number of algorithms that can be used of any container, irrespective of
their type. Algorithms library contains built in functions that performs complex algorithms
on the data structures.
For example: one can reverse a range with reverse() function, sort a range
with sort() function, search in a range with binary_search() and so on.
Algorithm library provides abstraction, i.e you don't necessarily need to know how the
the algorithm works.

C++: Containers in STL


Container library in STL provide containers that are used to create data structures like
arrays, linked list, trees etc.
These container are generic, they can hold elements of any data types, for
example: vector can be used for creating dynamic arrays of char, integer, float and
other types.
C++: Iterators in STL
Iterators in STL are used to point to the containers. Iterators actually acts as a bridge
between containers and algorithms.
For example: sort() algorithm have two parameters, starting iterator and ending
iterator, now sort() compare the elements pointed by each of these iterators and
arrange them in sorted order, thus it does not matter what is the type of the container
and same sort() can be used on different types of containers.
-------------------------------------------------------------------------------------------------------------------------------

What are Containers in STL?


a container can be used to hold different kind of objects and they are dynamic in nature!

Following are some common containers :

 vector : replicates arrays


 queue : replicates queues
 stack : replicates stack
 priority_queue : replicates heaps
 list : replicates linked list
 set : replicates trees
 map : associative arrays

Classification of Containers in STL


Containers are classified into four categories :

 Sequence containers : Used to implement data structures that are sequential in


nature like arrays(array) and linked list(list).
 Associative containers : Used to implement sorted data structures such as
map, set etc.
 Unordered associative containers : Used to implement unsorted data
structures.
 Containers adaptors : Used to provide different interface to the sequence
containers.
 #include <iostream>
 #include <list>

 int main ()
 {
 list<int> list1;
 }
 And that's it! we have a list, and not just that, the containers library also give all
the different methods which can be used to perform different operations on list
such as insertion, deletion, traversal etc.

----------------------------------------------------------------------------------------------------------------

Pair Template: Some Commonly used


Functions
Here are some function for pair template :

 Operator = : assign values to a pair.


 swap : swaps the contents of the pair.
 make_pair() : create and returns a pair having objects defined by parameter list.
 Operators( == , != , > , < , <= , >= ) : lexicographically compares two pairs.
 #include <iostream>

 using namespace std;

 int main ()
 {
 pair<int,int> pair1, pair3; //creats pair of integers
 pair<int,string> pair2; // creates pair of an integer an a string

 pair1 = make_pair(1, 2); // insert 1 and 2 to the pair1
 pair2 = make_pair(1, "Studytonight") // insert 1 and "Studytonight"
in pair2
 pair3 = make_pair(2, 4)
 cout<< pair1.first << endl; // prints 1, 1 being 1st element of
pair1
 cout<< pair2.second << endl; // prints Studytonight

 if(pair1 == pair3)
 cout<< "Pairs are equal" << endl;
 else
 cout<< "Pairs are not equal" << endl;

 return 0;
 }

TUPLE in STL
tuple and pair are very similar in their structure. Just like in pair we can pair two
heterogeneous object, in tuple we can pair three heterogeneous objects.
SYNTAX of a tuple is:
// creates tuple of three object of type T1, T2 and T3

tuple<T1, T2, T3> tuple1;

Tuple Template: Some Commonly


used Functions
Similar to pair, tuple template has its own member and non-member functions, few of which are
listed below :

 A Constructor to construct a new tuple


 Operator = : to assign value to a tuple

 swap : to swap value of two tuples

 make_tuple() : creates and return a tuple having elements described by the parameter list.

 Operators( == , != , > , < , <= , >= ) : lexicographically compares two pairs.

 Tuple_element : returns the type of tuple element

 Tie : Tie values of a tuple to its refrences.


Program demonstrating Tuple
template
#include <iostream>

int main ()

tuple<int, int, int> tuple1; //creates tuple of integers

tuple<int, string, string> tuple2; // creates pair of an integer an 2


string

tuple1 = make_tuple(1,2,3); // insert 1, 2 and 3 to the tuple1

tuple2 = make_pair(1,"Studytonight", "Loves You");

/* insert 1, "Studytonight" and "Loves You" in tuple2 */

int id;

string first_name, last_name;

tie(id,first_name,last_name) = tuple2;

/* ties id, first_name, last_name to

first, second and third element of tuple2 */

cout << id <<" "<< first_name <<" "<< last_name;

/* prints 1 Studytonight Loves You */

return 0;

}
ARRAY Container in STL
SYNTAX of array container:
array<object_type, array_size> array_name;

The above code creates an empty array of object_type with maximum size
of array_size. However, if you want to create an array with elements in it, you can do so
by simply using the = operator, here is an example :
#include <vector>

int main()

array<int, 4> odd_numbers = { 2, 4, 6, 8 };

You might also like