Template Classes: STL Has Four Components
Template Classes: STL Has Four Components
common programming data structures and functions such as lists, stacks, arrays, etc. It
is a library of container classes, algorithms and iterators. It is a generalized library and
so, its components are parameterized. A working knowledge of template classes is a
prerequisite for working with STL.
STL has four components
Algorithms
Containers
Functions
Iterators
Algorithms
The header algorithm defines a collection of functions especially designed to be used
on ranges of elements.They act on containers and provide means for various
operations for the contents of the containers.
Algorithm
Sorting
Searching
Important STL Algorithms
Useful Array algorithms
Partition Operations
Numeric
valarray class
Containers
Containers or container classes store objects and data. There are in total seven
standard “first-class” container classes and three container adaptor classes and only
seven header files that provide access to these containers or container adaptors.
Sequence Containers: implement data structures which can be accessed in a
sequential manner.
vector
list
deque
arrays
forward_list( Introduced in C++11)
Container Adaptors : provide a different interface for sequential containers.
queue
priority_queue
stack
Associative Containers : implement sorted data structures that can be quickly
searched (O(log n)complexity).
set
multiset
map
multimap
Functions
The STL includes classes that overload the function call operator. Instances of such
classes are called function objects or functors. Functors allow the working of the
associated function to be customized with the help of parameters to be passed.
Functors
Iterators
As the name suggests, iterators are used for working upon a sequence of values. They
are the major feature that allow generality in STL.
Iterators
Utility Library
Defined under <utility header>
pair
Set in C++ Standard Template Library (STL)
Sets are a type of associative containers in which each element has to be unique,
because the value of the element identifies it. The value of the element cannot be
modified once it is added to the set, though it is possible to remove and add the
modified value of that element.
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <set>
#include <iterator>
int main()
{
// empty set container
set <int, greater <int> > gquiz1;
// insert elements in random order
gquiz1.insert(40);
gquiz1.insert(30);
gquiz1.insert(60);
gquiz1.insert(20);
gquiz1.insert(50);
gquiz1.insert(50); // only one 50 will be added to the set
gquiz1.insert(10);
return 0;
}
The output of the above program is :
The set gquiz1 is : 60 50 40 30 20 10
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <iterator>
#include <map>
int main()
{
return 0;
}
Output:
The map gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
7 10
gquiz2.erase(4) : 1 removed
KEY ELEMENT
3 60
5 50
6 50
7 10
gquiz1.lower_bound(5) : KEY = 5 ELEMENT = 50
gquiz1.upper_bound(5) : KEY = 6 ELEMENT = 50
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <set>
#include <iterator>
int main()
{
// empty multiset container
multiset <int, greater <int> > gquiz1;
return 0;
#include <algorithm>
int main()
show(a);
sort(a, a+10);
show(a);
return 0;
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <list>
#include <iterator>
int main()
{
list <int> gqlist1, gqlist2;
gqlist1.push_back(i * 2);
gqlist2.push_front(i * 3);
showlist(gqlist1);
showlist(gqlist2);
gqlist1.pop_front();
showlist(gqlist1);
gqlist2.pop_back();
showlist(gqlist2);
gqlist1.reverse();
showlist(gqlist1);
showlist(gqlist2);
return 0;
List 2 (gqlist2) is : 27 24 21 18
15 12 9 6 3 0
gqlist1.front() : 0
gqlist1.back() : 18
gqlist1.pop_front() : 2 4 6 8
10 12 14 16 18
gqlist2.pop_back() : 27 24 21 18
15 12 9 6 3
gqlist1.reverse() : 18 16 14 12
10 8 6 4 2
gqlist2.sort(): 3 6 9 12
15 18 21 24 27
front() – Returns the value of the first element in the list.
back() – Returns the value of the last element in the list .
push_front(g) – Adds a new element ‘g’ at the beginning of the list .
push_back(g) – Adds a new element ‘g’ at the end of the list.
pop_front() – Removes the first element of the list, and reduces size of the list by 1.
pop_back() – Removes the last element of the list, and reduces size of the list by 1
list::begin() and list::end() in C++ STL– begin() function returns an iterator pointing
to the first element of the list
end()– end() function returns an iterator pointing to the theoretical last element
which follows the last element.
list rbegin() and rend() function in C++ STL– rbegin() returns a reverse iterator
which points to the last element of the list. rend() returns a reverse iterator which
points to the position before the beginning of the list.
list cbegin() and cend() function in C++ STL– cbegin() returns a constant random
access iterator which points to the beginning of the list. cend() returns a constant
random access iterator which points to the end of the list.
list crbegin() and crend() function in C++ STL– crbegin() returns a constant
reverse iterator which points to the last element of the list i.e reversed beginning of
container. crend() returns a constant reverse iterator which points to the
theoretical element preceding the first element in the list i.e. the reverse end of the
list.
empty() – Returns whether the list is empty(1) or not(0).
insert() – Inserts new elements in the list before the element at a specified position.
erase() – Removes a single element or a range of elements from the list.
assign() – Assigns new elements to list by replacing current elements and resizes
the list.
remove() – Removes all the elements from the list, which are equal to given
element.
list::remove_if() in C++ STL– Used to remove all the values from the list that
correspond true to the predicate or condition given as parameter to the function.
reverse() – Reverses the list.
size() – Returns the number of elements in the list.
list resize()function in C++ STL– Used to resize a list container.
sort() – Sorts the list in increasing order.
list max_size() function in C++ STL– Returns the maximum number of elements a
list container can hold.
list unique() in C++ STL– Removes all duplicate consecutive elements from the list.
list::emplace_front() and list::emplace_back() in C++
STL– emplace_front() function is used to insert a new element into the list
container, the new element is added to the beginning of the
list. emplace_back() function is used to insert a new element into the list
container, the new element is added to the end of the list.
list::clear() in C++ STL– clear() function is used to remove all the elements of the
list container, thus making it size 0.
list::operator= in C++ STL– This operator is used to assign new contents to the
container by replacing the existing contents.
list::swap() in C++ STL– This function is used to swap the contents of one list with
another list of same type and size.
list splice() function in C++ STL– Used to transfer elements from one list to
another.
list merge() function in C++ STL– Merges two sorted lists into one
list emplace() function in C++ STL– Extends list by inserting new element at a
given position.
perm_identity
Multimap in C++ Standard Template Library (STL)
Multimap is similar to mapwith an addition that multiple elements can have same keys.
Rather than each element being unique, the key value and mapped value pair has to be
unique in this case.
Some Basic Functions associated with multimap:
begin() – Returns an iterator to the first element in the multimap
end() – Returns an iterator to the theoretical element that follows last element in
the multimap
size() – Returns the number of elements in the multimap
max_size() – Returns the maximum number of elements that the multimap can
hold
empty() – Returns whether the multimap is empty
pair<int,int> insert(keyvalue,multimapvalue) – Adds a new element to the
multimap
C++ implementation to illustrate above functions
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <map>
#include <iterator>
int main()
{
multimap <int, int> gquiz1; // empty multimap container
return 0;
}
Output:
The multimap gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
6 10
filter_none
edit
play_arrow
brightness_4
//CPP program to illustrate pair STL
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair <int, char> PAIR1 ;
PAIR1.first = 100;
PAIR1.second = 'G' ;
return 0;
}
Output:
100 G
Initializing a pair
We can also initialize a pair.
Syntax :
filter_none
edit
play_arrow
brightness_4
//CPP program to illustrate Initializing of pair STL
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair <string,double> PAIR2 ("GeeksForGeeks", 1.23);
return 0;
}
Output:
GeeksForGeeks 1.23
Note: If not initialized, the first value of the pair gets automatically initialized.
filter_none
edit
play_arrow
brightness_4
//CPP program to illustrate auto-initializing of pair STL
#include <iostream>
#include <utility>
int main()
{
pair <int, double> PAIR1 ;
pair <string, char> PAIR2 ;
return 0;
}
Output:
00
Member Functions
1. make_pair() : This template function allows to create a value pair without writing
the types explicitly.
Syntax :
Pair_name = make_pair (value1,value2);
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <utility>
using namespace std;
int main()
{
pair <int, char> PAIR1 ;
pair <string, double> PAIR2 ("GeeksForGeeks", 1.23) ;
pair <string, double> PAIR3 ;
PAIR1.first = 100;
PAIR1.second = 'G' ;
return 0;
}
Output:
100 G
GeeksForGeeks 1.23
GeeksForGeeks is Best 4.56
2. operators(=, ==, !=, >=, <=) : We can use operators with pairs as well.
using equal(=) : It assigns new object for a pair object.
Syntax :
pair& operator= (const pair& pr);
This Assigns pr as the new content for the pair object. The first value is
assigned the first value of pr and the second value is assigned the second
value of pr .
Comparison (==) operator with pair : For given two pairs say pair1 and
pair2, the comparison operator compares the first value and second value of
those two pairs i.e. if pair1.first is equal to pair2.first or not AND if
pair1.second is equal to pair2.second or not .
Not equal (!=) operator with pair : For given two pairs say pair1 and pair2,
the != operator compares the first values of those two pairs i.e. if pair1.first is
equal to pair2.first or not, if they are equal then it checks the second values of
both.
Logical( >=, <= )operators with pair : For given two pairs say pair1 and
pair2, the =, >, can be used with pairs as well. It returns 0 or 1 by only
comparing the first value of the pair.
.
filter_none
edit
play_arrow
brightness_4
//CPP code to illustrate operators in pair
#include <iostream>
#include<utility>
using namespace std;
int main()
{
pair<int, int>pair1 = make_pair(1, 12);
pair<int, int>pair2 = make_pair(9, 12);
return 0;
}
Output:
0
1
0
1
0
1
3. swap : This function swaps the contents of one pair object with the contents of
another pair object. The pairs must be of same type.
Syntax :
pair1.swap(pair2) ;
For two given pairs say pair1 and pair2 of same type, swap function will swap the
pair1.first with pair2.first and pair1.second with pair2.second.
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include<utility>
int main()
{
pair<char, int>pair1 = make_pair('A', 1);
pair<char, int>pair2 = make_pair('B', 2);
return 0;
}
Output:
Before swapping:
Contents of pair1 = (A, 1)
Contents of pair2 = (B, 2)
After swapping:
Contents of pair1 = (B, 2)
Contents of pair2 = (A, 1)
filter_none
edit
play_arrow
brightness_4
//CPP program to illustrate pair in STL
#include <iostream>
#include <utility>
#include <string>
using namespace std;
int main()
{
pair <string, int> g1;
pair <string, int> g2("Quiz", 3);
pair <string, int> g3(g2);
pair <int, int> g4(5, 10);
g1 = make_pair(string("Geeks"), 1);
g2.first = ".com";
g2.second = 2;
cout << "This is pair g" << g1.second << " with "
<< "value " << g1.first << "." << endl << endl;
perm_identity
Queue in Standard Template Library (STL)
Queues are a type of container adaptors which operate in a first in first out (FIFO) type
of arrangement. Elements are inserted at the back (end) and are deleted from the front.
#include <iostream>
#include <queue>
while (!g.empty())
g.pop();
int main()
gquiz.push(10);
gquiz.push(20);
gquiz.push(30);
cout << "The queue gquiz is : ";
showq(gquiz);
gquiz.pop();
showq(gquiz);
return 0;
Output:
The queue gquiz is : 10 20 30
gquiz.size() : 3
gquiz.front() : 10
gquiz.back() : 30
gquiz.pop() : 20 30
perm_identity
Deque in C++ Standard Template Library (STL)
Double ended queues are sequence containers with the feature of expansion and
contraction on both the ends. They are similar to vectors, but are more efficient in case
of insertion and deletion of elements at the end, and also the beginning. Unlike vectors,
contiguous storage allocation may not be guaranteed.
The functions for deque are same as vector, with an addition of push and pop
operations for both front and back.
filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <deque>
int main()
{
deque <int> gquiz;
gquiz.push_back(10);
gquiz.push_front(20);
gquiz.push_back(30);
gquiz.push_front(15);
cout << "The deque gquiz is : ";
showdq(gquiz);
return 0;
}
The output of the above program is :
The deque gquiz is : 15 20 10 30
gquiz.size() : 4
gquiz.max_size() : 4611686018427387903
gquiz.at(2) : 10
gquiz.front() : 15
gquiz.back() : 30
gquiz.pop_front() : 20 10 30
gquiz.pop_back() : 20 10
Methods of Deque:
deque insert() function in C++ STL: Returns an iterator that points to the first of the
newly inserted elements.
deque rbegin() function in C++ STL: Returns a reverse iterator which points to the
last element of the deque (i.e., its reverse beginning).
deque rend() function in C++ STL: Returns a reverse iterator which points to the
position before the beginning of the deque (which is considered its reverse end).
deque cbegin() in C++ STL: Returns an iterator pointing to the first element of the
container.
deque max_size() function in C++ STL: Returns the maximum number of elements
that a deque container can hold.
deque assign() function in C++ STL: Assign values to the same or different deque
container.
deque resize() function in C++ STL: Function which changes the size of the deque.
deque::push_front() in C++ STL: This function is used to push elements into a
deque from the front.
deque::push_back() in C++ STL: This function is used to push elements into a
deque from the back.
deque::pop_front() and deque::pop_back() in C++ STL: pop_front() function is
used to pop or remove elements from a deque from the front. pop_back() function
is used to pop or remove elements from a deque from the back.
deque::front() and deque::back() in C++ STL: front() function is used to reference
the first element of the deque container. back() function is used to reference the
last element of the deque container.
deque::clear() and deque::erase() in C++ STL: clear() function is used to remove
all the elements of the deque container, thus making its size 0. erase() function is
used to remove elements from a container from the specified position or range.
deque::empty() and deque::size() in C++ STL: empty() function is used to check if
the deque container is empty or not. size() function is used to return the size of the
deque container or the number of elements in the deque container.
deque::operator= and deque::operator[] in C++ STL:
operator= operator is used to assign new contents to the container by replacing
the existing contents. operator[] operator is used to reference the element present
at position given inside the operator.
deque::at() and deque::swap() in C++ STL: at() function is used reference the
element present at the position given as the parameter to the
function. swap() function is used to swap the contents of one deque with another
deque of same type and size.
deque::begin() and deque::end in C++ STL: begin() function is used to return an
iterator pointing to the first element of the deque container. end() function is used
to return an iterator pointing to the last element of the deque container.
deque::emplace_front() and deque::emplace_back() in C++
STL: emplace_front() function is used to insert a new element into the deque
container, the new element is added to the beginning of the
deque. emplace_back() function is used to insert a new element into the deque
container, the new element is added to the end of the deque.
perm_identity
Binary Search in C++ Standard Template Library
(STL)
Binary search is a widely used searching algorithm that requires the array to be
sorted before search is applied. The main idea behind this algorithm is to keep
dividing the array in half (divide and conquer) until the element is found, or all the
elements are exhausted.
It works by comparing the middle item of the array with our target, if it matches, it
returns true otherwise if the middle term is greater than the target, the search is
performed in the left sub-array.
If the middle term is less than target, the search is performed in the right sub-
array.
The prototype for binary search is :
binary_search(startaddress, endaddress, valuetofind)
filter_none
edit
play_arrow
brightness_4
// CPP program to implement
// Binary Search in
#include <algorithm>
#include <iostream>
int main()
int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
show(a, asize);
sort(a, a + asize);
show(a, asize);
else
return 0;
The output of the above program is :
The array is : 1 5 8 9 0 6 7 3 4 2 0