STL Algorithms
Algorithms are set of generic and optimal implementations of some useful algorithms to make programming easier and more efficient.
- These algorithms work with STL containers and iterators.
- Algorithms are defined inside the <algorithm> header file.
- C++ STL contains around 114 Algorithms which are listed in the article – Algorithm Library in C++ STL
Some of the commonly used algorithms are:
1. Sort
The std::sort algorithm is used to sort data in any given order.
Syntax of std::sort
sort (beginIterator, endIterator);
sort (beginIterator, endIterator, comparator); // for custom comparator
Note: Iterators must be RandomAccessIterators.
Example
// C++ program to demonstrate default behaviour of
// sort() in STL.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
/*Here we take two parameters, the beginning of the
array and the length n upto which we want the array to
be sorted*/
sort(arr, arr + n);
cout << "\nArray after sorting using "
"default sort is : \n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
Output
Array after sorting using default sort is : 0 1 2 3 4 5 6 7 8 9
2. Copy
The std::copy method efficiently copies a range of elements to another container using its iterators.
Syntax of std::copy
copy (beginIterator, endIterator, destIterator);
Note: Iterators can be of InputIterator, OutputIterator or ForwardIterator.
Example
// C++ Program to print vector using copy function and input
// and output stream iterators
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
// creating vector
vector<int> v = { 1, 2, 3, 4, 5 };
// copying data to ostream
copy(v.begin(), v.end(),
ostream_iterator<int>(cout, " "));
return 0;
}
Output
1 2 3 4 5
3. Max Element
The std::max_element implements an efficient algorithm to find the maximum element in the container. To find minimum element, use std::min_element.
Syntax of std::max_element
max_element (firstIterator, lastIterator);
Note: The iterators can be of type ForwardIterators.
Example
// C++ program to demonstrate the use of std::max_element
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
// creating vector
vector<int> v = { 10, 88, 2, 9, 45, 82, 546, 42, 221 };
// Finding the maximum value between the first and the
// fourth element
auto max = max_element(begin(v), end(v));
cout << "Maximum Element: " << *max << "\n";
return 0;
}
Output
Maximum Element: 546
4. Find
The std::find function is used to find the element in the given range.
Syntax of std::find
find (firstIterator, lastIterator, value);
Note: The iterators can be of the type InputIterator, ForwardIterator.
Example
// C++ program to illustrate the find()
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// creating vector
vector<int> v
= { 1, 8, 97, 3, 654, 132, 65, 4, 321, 5, 45 };
// finding 5
auto itr = find(v.begin(), v.end(), 5);
if (itr != v.end()) {
cout << *itr << " is found!" << endl;
}
else {
cout << "5 is not found!" << endl;
}
return 0;
}
Output
5 is found!
5. For Each
The std::for_each algorithm applies the specified instruction to each of the elements in the given range.
Syntax of std::for_each
for_each (firstIterator, lastIterator, unaryFunction);
Note: The iterators can be of the type ForwardIterator, InputIterator.
Example
// C++ program to print vector using for
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
// creating vector
vector<int> v = { 1, 2, 3, 4, 5 };
// adding 1 to each element
for_each(v.begin(), v.end(), [](int& i){
i = i + 1;
});
// printing vector
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Output
2 3 4 5 6
C++ STL Cheat Sheet
The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and example.
Contact Us