0% found this document useful (0 votes)
49 views37 pages

Template Classes: STL Has Four Components

The Standard Template Library (STL) is a C++ library that provides common data structures and algorithms. It has four main components: algorithms, containers, functions, and iterators. The document then discusses each component in detail, describing the various classes, functions, and capabilities they provide such as containers for storing data, algorithms for sorting and searching containers, iterators for traversing container elements, and functions for customizing algorithm behavior.

Uploaded by

Anu Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views37 pages

Template Classes: STL Has Four Components

The Standard Template Library (STL) is a C++ library that provides common data structures and algorithms. It has four main components: algorithms, containers, functions, and iterators. The document then discusses each component in detail, describing the various classes, functions, and capabilities they provide such as containers for storing data, algorithms for sorting and searching containers, iterators for traversing container elements, and functions for customizing algorithm behavior.

Uploaded by

Anu Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

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. 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.

Some basic functions associated with Set:


 begin() – Returns an iterator to the first element in the set.
 end() – Returns an iterator to the theoretical element that follows last element in
the set.
 size() – Returns the number of elements in the set.
 max_size() – Returns the maximum number of elements that the set can hold.
 empty() – Returns whether the set is empty.

filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <set>
#include <iterator>

using namespace std;

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);

// printing set gquiz1


set <int, greater <int> > :: iterator itr;
cout << "\nThe set gquiz1 is : ";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;

// assigning the elements from gquiz1 to gquiz2


set <int> gquiz2(gquiz1.begin(), gquiz1.end());

// print all elements of the set gquiz2


cout << "\nThe set gquiz2 after assign from gquiz1 is : ";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;

// remove all elements up to 30 in gquiz2


cout << "\ngquiz2 after removal of elements less than 30 : ";
gquiz2.erase(gquiz2.begin(), gquiz2.find(30));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}

// remove element with value 50 in gquiz2


int num;
num = gquiz2.erase (50);
cout << "\ngquiz2.erase(50) : ";
cout << num << " removed \t" ;
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}

cout << endl;

//lower bound and upper bound for set gquiz1


cout << "gquiz1.lower_bound(40) : "
<< *gquiz1.lower_bound(40) << endl;
cout << "gquiz1.upper_bound(40) : "
<< *gquiz1.upper_bound(40) << endl;
//lower bound and upper bound for set gquiz2
cout << "gquiz2.lower_bound(40) : "
<< *gquiz2.lower_bound(40) << endl;
cout << "gquiz2.upper_bound(40) : "
<< *gquiz2.upper_bound(40) << endl;

return 0;

}
The output of the above program is :
The set gquiz1 is : 60 50 40 30 20 10

The set gquiz2 after assign from gquiz1 is : 10 20 30 40


50 60

gquiz2 after removal of elements less than 30 : 30 40 50 60


gquiz2.erase(50) : 1 removed 30 40 60
gquiz1.lower_bound(40) : 40
gquiz1.upper_bound(40) : 30
gquiz2.lower_bound(40) : 40
gquiz2.upper_bound(40) : 60
Methods of Stack:
 begin() – Returns an iterator to the first element in the set.
 end() – Returns an iterator to the theoretical element that follows last element in
the set.
 rbegin()– Returns a reverse iterator pointing to the last element in the container.
 rend()– Returns a reverse iterator pointing to the theoretical element right before
the first element in the set container.
 crbegin()– Returns a constant iterator pointing to the last element in the container.
 crend() – Returns a constant iterator pointing to the position just before the first
element in the container.
 cbegin()– Returns a constant iterator pointing to the first element in the container.
 cend() – Returns a constant iterator pointing to the position past the last element in
the container.
 size() – Returns the number of elements in the set.
 max_size() – Returns the maximum number of elements that the set can hold.
 empty() – Returns whether the set is empty.
 insert(const g) – Adds a new element ‘g’ to the set.
 iterator insert (iterator position, const g) – Adds a new element ‘g’ at the position
pointed by iterator.
 erase(iterator position) – Removes the element at the position pointed by the
iterator.
 erase(const g)– Removes the value ‘g’ from the set.
 clear() – Removes all the elements from the set.
 key_comp() / value_comp() – Returns the object that determines how the elements
in the set are ordered (‘<‘ by default).
 find(const g) – Returns an iterator to the element ‘g’ in the set if found, else returns
the iterator to end.
 count(const g) – Returns 1 or 0 based on the element ‘g’ is present in the set or
not.
 lower_bound(const g) – Returns an iterator to the first element that is equivalent to
‘g’ or definitely will not go before the element ‘g’ in the set.
 upper_bound(const g) – Returns an iterator to the first element that is equivalent to
‘g’ or definitely will go after the element ‘g’ in the set.
 equal_range()– The function returns an iterator of pairs. (key_comp). The pair
refers to the range that includes all the elements in the container which have a key
equivalent to k.
 emplace()– This function is used to insert a new element into the set container,
only if the element to be inserted is unique and does not already exists in the set.
 emplace_hint()– Returns an iterator pointing to the position where the insertion is
done. If the element passed in the parameter already exists, then it returns an
iterator pointing to the position where the existing element is.
 swap()– This function is used to exchange the contents of two sets but the sets
must be of same type, although sizes may differ.
 operator= – The ‘=’ is an operator in C++ STL which copies (or moves) a set to
another set and set::operator= is the corresponding operator function.
 get_allocator()– Returns the copy of the allocator object associated with the set.

Recent Articles on set


perm_identity
Map in C++ Standard Template Library (STL)
Maps are associative containers that store elements in a mapped fashion. Each
element has a key value and a mapped value. No two mapped values can have same
key values.

Some basic functions associated with Map:


begin() – Returns an iterator to the first element in the map
end() – Returns an iterator to the theoretical element that follows last element in the
map
size() – Returns the number of elements in the map
max_size() – Returns the maximum number of elements that the map can hold
empty() – Returns whether the map is empty
pair insert(keyvalue, mapvalue) – Adds a new element to the map
erase(iterator position) – Removes the element at the position pointed by the iterator
erase(const g)– Removes the key value ‘g’ from the map
clear() – Removes all the elements from the map

filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <iterator>
#include <map>

using namespace std;

int main()
{

// empty map container


map<int, int> gquiz1;

// insert elements in random order


gquiz1.insert(pair<int, int>(1, 40));
gquiz1.insert(pair<int, int>(2, 30));
gquiz1.insert(pair<int, int>(3, 60));
gquiz1.insert(pair<int, int>(4, 20));
gquiz1.insert(pair<int, int>(5, 50));
gquiz1.insert(pair<int, int>(6, 50));
gquiz1.insert(pair<int, int>(7, 10));

// printing map gquiz1


map<int, int>::iterator itr;
cout << "\nThe map gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;

// assigning the elements from gquiz1 to gquiz2


map<int, int> gquiz2(gquiz1.begin(), gquiz1.end());

// print all elements of the map gquiz2


cout << "\nThe map gquiz2 after"
<< " assign from gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;

// remove all elements up to


// element with key=3 in gquiz2
cout << "\ngquiz2 after removal of"
" elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}

// remove all elements with key = 4


int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}

cout << endl;

// lower bound and upper bound for map gquiz1 key = 5


cout << "gquiz1.lower_bound(5) : "
<< "\tKEY = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< gquiz1.lower_bound(5)->second << endl;
cout << "gquiz1.upper_bound(5) : "
<< "\tKEY = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\tELEMENT = "
<< gquiz1.upper_bound(5)->second << endl;

return 0;
}
Output:
The map gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
7 10

The map gquiz2 after assign from gquiz1 is :


KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
7 10

gquiz2 after removal of elements less than key=3 :


KEY ELEMENT
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

List of all functions of Map:


 map insert() in C++ STL– Insert elements with a particular key in the map
container. .
 map count() function in C++ STL– Returns the number of matches to element with
key value ‘g’ in the map.
 map equal_range() in C++ STL– Returns an iterator of pairs. The pair refers to the
bounds of a range that includes all the elements in the container which have a key
equivalent to k.
 map erase() function in C++ STL– Used to erase element from the container.
 map rend() function in C++ STL– Returns a reverse iterator pointing to the
theoretical element right before the first key-value pair in the map(which is
considered its reverse end).
 map rbegin() function in C++ STL– Returns a reverse iterator which points to the
last element of the map.
 map find() function in C++ STL– Returns an iterator to the element with key value
‘g’ in the map if found, else returns the iterator to end.
 map crbegin() and crend() function in C++ STL– crbegin() returns a constant
reverse iterator referring to the last element in the map container. crend() returns a
constant reverse iterator pointing to the theoretical element before the first element
in the map.
 map cbegin() and cend() function in C++ STL– cbegin() returns a constant iterator
referring to the first element in the map container. cend() returns a constant
iterator pointing to the theoretical element that follows last element in the multimap.
 map emplace() in C++ STL– Inserts the key and its element in the map container.
 map max_size() in C++ STL– Returns the maximum number of elements a map
container can hold.
 map upper_bound() function in C++ STL– Returns an iterator to the first element
that is equivalent to mapped value with key value ‘g’ or definitely will go after the
element with key value ‘g’ in the map
 map operator= in C++ STL– Assigns contents of a container to a different
container, replacing its current content.
 map lower_bound() function in C++ STL– Returns an iterator to the first element
that is equivalent to mapped value with key value ‘g’ or definitely will not go before
the element with key value ‘g’ in the map.
 map emplace_hint() function in C++ STL– Inserts the key and its element in the
map container with a given hint.
 map value_comp() in C++ STL– Returns the object that determines how the
elements in the map are ordered (‘<' by default).
 map key_comp() function in C++ STL– Returns the object that determines how the
elements in the map are ordered (‘<' by default).
 map::size() in C++ STL– Returns the number of elements in the map.
 map::empty() in C++ STL– Returns whether the map is empty.
 map::begin() and end() in C++ STL– begin() returns an iterator to the first element
in the map. end() returns an iterator to the theoretical element that follows last
element in the map
 map::operator[] in C++ STL– This operator is used to reference the element
present at position given inside the operator.
 map::clear() in C++ STL– Removes all the elements from the map.
 map::at() and map::swap() in C++ STL– at() function is used to return the
reference to the element associated with the key k. swap() function is used to
exchange the contents of two maps but the maps must be of same type, although
sizes may differ.

Recent articles on Map


Multiset in C++ Standard Template Library (STL)
Multisets are a type of associative containers similar to set, with an exception that
multiple elements can have same values.
Some Basic Functions associated with multiset:
begin() – Returns an iterator to the first element in the multiset
end() – Returns an iterator to the theoretical element that follows last element in the
multiset
size() – Returns the number of elements in the multiset
max_size() – Returns the maximum number of elements that the multiset can hold
empty() – Returns whether the multiset is empty

filter_none
edit
play_arrow
brightness_4
#include <iostream>
#include <set>
#include <iterator>

using namespace std;

int main()
{
// empty multiset container
multiset <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); // 50 will be added again to the multiset unlike set
gquiz1.insert(10);

// printing multiset gquiz1


multiset <int, greater <int> > :: iterator itr;
cout << "\nThe multiset gquiz1 is : ";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;

// assigning the elements from gquiz1 to gquiz2


multiset <int> gquiz2(gquiz1.begin(), gquiz1.end());

// print all elements of the multiset gquiz2


cout << "\nThe multiset gquiz2 after assign from gquiz1 is : ";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;

// remove all elements up to element with value 30 in gquiz2


cout << "\ngquiz2 after removal of elements less than 30 : ";
gquiz2.erase(gquiz2.begin(), gquiz2.find(30));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}

// remove all elements with value 50 in gquiz2


int num;
num = gquiz2.erase(50);
cout << "\ngquiz2.erase(50) : ";
cout << num << " removed \t" ;
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}

cout << endl;

//lower bound and upper bound for multiset gquiz1


cout << "gquiz1.lower_bound(40) : "
<< *gquiz1.lower_bound(40) << endl;
cout << "gquiz1.upper_bound(40) : "
<< *gquiz1.upper_bound(40) << endl;

//lower bound and upper bound for multiset gquiz2


cout << "gquiz2.lower_bound(40) : "
<< *gquiz2.lower_bound(40) << endl;
cout << "gquiz2.upper_bound(40) : "
<< *gquiz2.upper_bound(40) << endl;

return 0;

The output of the above program is :


The multiset gquiz1 is : 60 50 50 40 30 20
10

The multiset gquiz2 after assign from gquiz1 is : 10 20 30


40 50 50 60

gquiz2 after removal of elements less than 30 : 30 40 50


50 60
gquiz2.erase(50) : 2 removed 30 40 60
gquiz1.lower_bound(40) : 40
gquiz1.upper_bound(40) : 30
gquiz2.lower_bound(40) : 40
gquiz2.upper_bound(40) : 60
List of functions of Multiset:
 begin() – Returns an iterator to the first element in the multiset.
 end() – Returns an iterator to the theoretical element that follows last element in
the multiset.
 size() – Returns the number of elements in the multiset.
 max_size()– Returns the maximum number of elements that the multiset can hold.
 empty() – Returns whether the multiset is empty.
 pair insert(const g) – Adds a new element ‘g’ to the multiset.
 iterator insert (iterator position,const g) – Adds a new element ‘g’ at the position
pointed by iterator.
 erase(iterator position)– Removes the element at the position pointed by the
iterator.
 erase(const g)– Removes the value ‘g’ from the multiset.
 clear()– Removes all the elements from the multiset.
 key_comp() / value_comp()– Returns the object that determines how the elements
in the multiset are ordered (‘<' by default).
 find(const g)– Returns an iterator to the element ‘g’ in the multiset if found, else
returns the iterator to end.
 count(const g)– Returns the number of matches to element ‘g’ in the multiset.
 lower_bound(const g)– Returns an iterator to the first element that is equivalent to
‘g’ or definitely will not go before the element ‘g’ in the multiset.
 upper_bound(const g)– Returns an iterator to the first element that is equivalent to
‘g’ or definitely will go after the element ‘g’ in the multiset.
 multiset::swap()– This function is used to exchange the contents of two multisets
but the sets must be of same type, although sizes may differ.
 multiset::operator=– This operator is used to assign new contents to the container
by replacing the existing contents.
 multiset::emplace()– This function is used to insert a new element into the multiset
container.
 multiset equal_range()– Returns an iterator of pairs. The pair refers to the range
that includes all the elements in the container which have a key equivalent to k.
 multiset::emplace_hint() – Inserts a new element in the multiset.
 multiset::rbegin()– Returns a reverse iterator pointing to the last element in the
multiset container.
 multiset::rend()– Returns a reverse iterator pointing to the theoretical element right
before the first element in the multiset container.
 multiset::cbegin()– Returns a constant iterator pointing to the first element in the
container.
 multiset::cend()– Returns a constant iterator pointing to the position past the last
element in the container.
 multiset::crbegin()– Returns a constant reverse iterator pointing to the last element
in the container.
 multiset::crend()– Returns a constant reverse iterator pointing to the position just
before the first element in the container.
 multiset::get_allocator()– Returns a copy of the allocator object associated with the
multiset.
Recent articles on Multiset
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above

Sort in C++ Standard Template Library (STL)


Sorting is one of the most basic functions applied to data. It means arranging the data in
a particular fashion, which can be increasing or decreasing. There is a builtin function in
C++ STL by the name of sort().
Internally this function is implemented as Quick-sort. The complexity of it is O(N*log(N)).
The prototype for sort is :
sort(startaddress, endaddress)

startaddress: the address of the first element of the array


endaddress: the address of the last element of the array
filter_none
edit
play_arrow
brightness_4
#include <iostream>

#include <algorithm>

using namespace std;

void show(int a[])

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

cout << a[i] << " ";

int main()

int a[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};

cout << "\n The array before sorting is : ";

show(a);

sort(a, a+10);

cout << "\n\n The array after sorting is : ";

show(a);

return 0;

The outut of the above program is :


The array before sorting is : 1 5 8 9 6 7 3 4 2 0
The array after sorting is : 0 1 2 3 4 5 6 7 8 9

Refer std::sort() for more details.


List in C++ Standard Template Library (STL)
Lists are sequence containers that allow non-contiguous memory allocation. As
compared to vector, list has slow traversal, but once a position has been found,
insertion and deletion are quick. Normally, when we say a List, we talk about doubly
linked list. For implementing a singly linked list, we use forward list.

Below is the program to show the working of some functions of List:

filter_none
edit
play_arrow
brightness_4
#include <iostream>

#include <list>

#include <iterator>

using namespace std;

//function for printing the elements in a list

void showlist(list <int> g)

list <int> :: iterator it;

for(it = g.begin(); it != g.end(); ++it)

cout << '\t' << *it;

cout << '\n';

int main()

{
list <int> gqlist1, gqlist2;

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

gqlist1.push_back(i * 2);

gqlist2.push_front(i * 3);

cout << "\nList 1 (gqlist1) is : ";

showlist(gqlist1);

cout << "\nList 2 (gqlist2) is : ";

showlist(gqlist2);

cout << "\ngqlist1.front() : " << gqlist1.front();

cout << "\ngqlist1.back() : " << gqlist1.back();

cout << "\ngqlist1.pop_front() : ";

gqlist1.pop_front();

showlist(gqlist1);

cout << "\ngqlist2.pop_back() : ";

gqlist2.pop_back();

showlist(gqlist2);

cout << "\ngqlist1.reverse() : ";

gqlist1.reverse();

showlist(gqlist1);

cout << "\ngqlist2.sort(): ";


gqlist2.sort();

showlist(gqlist2);

return 0;

The output of the above program is :


List 1 (gqlist1) is : 0 2 4 6
8 10 12 14 16 18

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>

using namespace std;

int main()
{
multimap <int, int> gquiz1; // empty multimap container

// insert elements in random order


gquiz1.insert(pair <int, int> (1, 40));
gquiz1.insert(pair <int, int> (2, 30));
gquiz1.insert(pair <int, int> (3, 60));
gquiz1.insert(pair <int, int> (4, 20));
gquiz1.insert(pair <int, int> (5, 50));
gquiz1.insert(pair <int, int> (6, 50));
gquiz1.insert(pair <int, int> (6, 10));

// printing multimap gquiz1


multimap <int, int> :: iterator itr;
cout << "\nThe multimap gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;

// assigning the elements from gquiz1 to gquiz2


multimap <int, int> gquiz2(gquiz1.begin(),gquiz1.end());

// print all elements of the multimap gquiz2


cout << "\nThe multimap gquiz2 after assign from gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;

// remove all elements up to element with value 30 in gquiz2


cout << "\ngquiz2 after removal of elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}

// remove all elements with key = 4


int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n" ;
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}

cout << endl;

//lower bound and upper bound for multimap gquiz1 key = 5


cout << "gquiz1.lower_bound(5) : " << "\tKEY = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.lower_bound(5)->second << endl;
cout << "gquiz1.upper_bound(5) : " << "\tKEY = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.upper_bound(5)->second << endl;

return 0;

}
Output:
The multimap gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
6 10

The multimap gquiz2 after assign from gquiz1 is :


KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
6 50
6 10

gquiz2 after removal of elements less than key=3 :


KEY ELEMENT
3 60
4 20
5 50
6 50
6 10
gquiz2.erase(4) : 1 removed
KEY ELEMENT
3 60
5 50
6 50
6 10

gquiz1.lower_bound(5) : KEY = 5 ELEMENT = 50


gquiz1.upper_bound(5) : KEY = 6 ELEMENT = 50
List of Functions of Multimap:
 multimap::operator= in C++ STL– It is used to assign new contents to the container
by replacing the existing contents.
 multimap::crbegin() and multimap::crend() in C++ STL– crbegin() returns a
constant reverse iterator referring to the last element in the multimap
container. crend() returns a constant reverse iterator pointing to the theoretical
element before the first element in the multimap.
 multimap::emplace_hint() in C++ STL– Inserts the key and its element in the
multimap container with a given hint.
 multimap clear() function in C++ STL– Removes all the elements from the
multimap.
 multimap empty() function in C++ STL– Returns whether the multimap is empty.
 multimap maxsize() in C++ STL– Returns the maximum number of elements a
multimap container can hold.
 multimap value_comp() function in C++ STL– Returns the object that determines
how the elements in the multimap are ordered (‘<‘ by default)
 multimap rend in C++ STL– Returns a reverse iterator pointing to the theoretical
element preceding to the first element of the multimap container.
 multimap::cbegin() and multimap::cend() in C++ STL– cbegin() returns a constant
iterator referring to the first element in the multimap container. cend() returns a
constant iterator pointing to the theoretical element that follows last element in the
multimap.
 multimap::swap() in C++ STL– Swap the contents of one multimap with another
multimap of same type and size.
 multimap rbegin in C++ STL– Returns an iterator pointing to the last element of the
container.
 multimap size() function in C++ STL– Returns the number of elements in the
multimap container.
 multimap::emplace() in C++ STL– Inserts the key and its element in the multimap
container.
 multimap::begin() and multimap::end() in C++ STL– begin() returns an iterator
referring to the first element in the multimap container. end() returns an iterator to
the theoretical element that follows last element in the multimap.
 multimap upper_bound() function in C++ STL– Returns an iterator to the first
element that is equivalent to multimapped value with key value ‘g’ or definitely will
go after the element with key value ‘g’ in the multimap.
 multimap::count() in C++ STL– Returns the number of matches to element with key
value ‘g’ in the multimap.
 multimap::erase() in C++ STL– Removes the key value from the multimap.
 multimap::find() in C++ STL– Returns an iterator to the element with key value ‘g’
in the multimap if found, else returns the iterator to end.
 multimap equal_range() in C++ STL– Returns an iterator of pairs. The pair refers to
the bounds of a range that includes all the elements in the container which have a
key equivalent to k.
 multimap insert() in C++ STL– Used to insert elements in the multimap container.
 multimap lower_bound() function in C++ STL– Returns an iterator to the first
element that is equivalent to multimapped value with key value ‘g’ or definitely will
not go before the element with key value ‘g’ in the multimap.
 multimap key_comp() in C++ STL– Returns the object that determines how the
elements in the multimap are ordered (‘<‘ by default).
perm_identity
Pair in C++ Standard Template Library (STL)
The pair container is a simple container defined in <utility> header consisting of two
data elements or objects.
 The first element is referenced as ‘first’ and the second element as ‘second’ and
the order is fixed (first, second).
 Pair is used to combine together two values which may be different in type. Pair
provides a way to store two heterogeneous objects as a single unit.
 Pair can be assigned, copied and compared. The array of objects allocated in a
map or hash_map are of type ‘pair’ by default in which all the ‘first’ elements are
unique keys associated with their ‘second’ value objects.
 To access the elements, we use variable name followed by dot operator followed
by the keyword first or second.
Syntax :
pair (data_type1, data_type2) Pair_name;

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' ;

cout << PAIR1.first << " " ;


cout << PAIR1.second << endl ;

return 0;
}
Output:
100 G
Initializing a pair
We can also initialize a pair.
Syntax :

pair (data_type1, data_type2) Pair_name (value1, value2) ;


Different ways to initialize pair:
pair g1; //default
pair g2(1, 'a'); //initialized, different data type
pair g3(1, 10); //initialized, same data type
pair g4(g3); //copy of g3
Another way to initialize a pair is by using the make_pair() function.
g2 = make_pair(1, 'a');

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);

cout << PAIR2.first << " " ;


cout << PAIR2.second << endl ;

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>

using namespace std;

int main()
{
pair <int, double> PAIR1 ;
pair <string, char> PAIR2 ;

cout << PAIR1.first ; //it is initialised to 0


cout << PAIR1.second ; //it is initialised to 0

cout << " ";

cout << PAIR2.first ; //it prints nothing i.e NULL


cout << PAIR2.second ; //it prints nothing i.e NULL

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' ;

PAIR3 = make_pair ("GeeksForGeeks is Best",4.56);

cout << PAIR1.first << " " ;


cout << PAIR1.second << endl ;

cout << PAIR2.first << " " ;


cout << PAIR2.second << endl ;

cout << PAIR3.first << " " ;


cout << PAIR3.second << endl ;

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);

cout << (pair1 == pair2) << endl;


cout << (pair1 != pair2) << endl;
cout << (pair1 >= pair2) << endl;
cout << (pair1 <= pair2) << endl;
cout << (pair1 > pair2) << endl;
cout << (pair1 < pair2) << endl;

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>

using namespace std;

int main()
{
pair<char, int>pair1 = make_pair('A', 1);
pair<char, int>pair2 = make_pair('B', 2);

cout << "Before swapping:\n " ;


cout << "Contents of pair1 = " << pair1.first << " " << pair1.second ;
cout << "Contents of pair2 = " << pair2.first << " " << pair2.second ;
pair1.swap(pair2);

cout << "\nAfter swapping:\n ";


cout << "Contents of pair1 = " << pair1.first << " " << pair1.second ;
cout << "Contents of pair2 = " << pair2.first << " " << pair2.second ;

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;

cout << "This is pair g" << g3.second


<< " with value " << g3.first
<< "This pair was initialized as a copy of "
<< "pair g2" << endl << endl;

cout << "This is pair g" << g2.second


<< " with value " << g2.first
<< "\nThe values of this pair were"
<< " changed after initialization."
<< endl << endl;

cout << "This is pair g4 with values "


<< g4.first << " and " << g4.second
<< " made for showing addition. \nThe "
<< "sum of the values in this pair is "
<< g4.first+g4.second
<< "." << endl << endl;

cout << "We can concatenate the values of"


<< " the pairs g1, g2 and g3 : "
<< g1.first + g3.first + g2.first << endl << endl;

cout << "We can also swap pairs "


<< "(but type of pairs should be same) : " << endl;
cout << "Before swapping, " << "g1 has " << g1.first
<< " and g2 has " << g2.first << endl;
swap(g1, g2);
cout << "After swapping, "
<< "g1 has " << g1.first << " and g2 has " << g2.first;
return 0;
}
Output:
This is pair g1 with value Geeks.

This is pair g3 with value QuizThis pair was initialized as a copy of


pair g2

This is pair g2 with value .com


The values of this pair were changed after initialization.

This is pair g4 with values 5 and 10 made for showing addition.


The sum of the values in this pair is 15.

We can concatenate the values of the pairs g1, g2 and g3 :


GeeksQuiz.com

We can also swap pairs (but type of pairs should be same) :


Before swapping, g1 has Geeks and g2 has .com
After swapping, g1 has .com and g2 has Geeks

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.

The functions supported by queue are :


1. empty() – Returns whether the queue is empty.
2. size() – Returns the size of the queue.
3. queue::swap() in C++ STL: Exchange the contents of two queues but the queues must be
of same type, although sizes may differ.
4. queue::emplace() in C++ STL: Insert a new element into the queue container, the new
element is added to the end of the queue.
5. queue::front() and queue::back() in C++ STL– front() function returns a reference to the
first element of the queue. back() function returns a reference to the last element of the
queue.
6. push(g) and pop() – push() function adds the element ‘g’ at the end of the
queue. pop() function deletes the first element of the queue.
filter_none
edit
play_arrow
brightness_4
// CPP code to illustrate

// Queue in Standard Template Library (STL)

#include <iostream>

#include <queue>

using namespace std;

void showq(queue <int> gq)

queue <int> g = gq;

while (!g.empty())

cout << '\t' << g.front();

g.pop();

cout << '\n';

int main()

queue <int> gquiz;

gquiz.push(10);

gquiz.push(20);

gquiz.push(30);
cout << "The queue gquiz is : ";

showq(gquiz);

cout << "\ngquiz.size() : " << gquiz.size();

cout << "\ngquiz.front() : " << gquiz.front();

cout << "\ngquiz.back() : " << gquiz.back();

cout << "\ngquiz.pop() : ";

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>

using namespace std;

void showdq(deque <int> g)


{
deque <int> :: iterator it;
for (it = g.begin(); it != g.end(); ++it)
cout << '\t' << *it;
cout << '\n';
}

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);

cout << "\ngquiz.size() : " << gquiz.size();


cout << "\ngquiz.max_size() : " << gquiz.max_size();

cout << "\ngquiz.at(2) : " << gquiz.at(2);


cout << "\ngquiz.front() : " << gquiz.front();
cout << "\ngquiz.back() : " << gquiz.back();

cout << "\ngquiz.pop_front() : ";


gquiz.pop_front();
showdq(gquiz);

cout << "\ngquiz.pop_back() : ";


gquiz.pop_back();
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)

 startaddress: the address of the first element of the array.


 endaddress: the address of the last element of the array.
 valuetofind: the target value which we have to search for.

 filter_none
 edit
 play_arrow
 brightness_4
// CPP program to implement

// Binary Search in

// Standard Template Library (STL)

#include <algorithm>
#include <iostream>

using namespace std;

void show(int a[], int arraysize)

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

cout << a[i] << " ";

int main()

int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };

int asize = sizeof(a) / sizeof(a[0]);

cout << "\n The array is : ";

show(a, asize);

cout << "\n\nLet's say we want to search for 2 in the array";

cout << "\n So, we first sort the array";

sort(a, a + asize);

cout << "\n\n The array after sorting is : ";

show(a, asize);

cout << "\n\nNow, we do the binary search";

if (binary_search(a, a + 10, 2))

cout << "\nElement found in the array";

else

cout << "\nElement not found in the array";

cout << "\n\nNow, say we want to search for 10";

if (binary_search(a, a + 10, 10))

cout << "\nElement found in the array";


else

cout << "\nElement not found in the array";

return 0;


The output of the above program is :
 The array is : 1 5 8 9 0 6 7 3 4 2 0

 Let's say we want to search for 2 in the array


 So, we first sort the array

 The array after sorting is : 0 1 2 3 4 5 6 7 8 9


 Now, we do the binary search


 Element found in the array

 Now, say we want to search for 10


 Element not found in the array

You might also like