The C++ algorithms library is a vast collection of functions. It provides us with various inbuilt functions on searching, sorting, counting, manipulating, etc. The algorithms library operates on a range of elements and the range are defined as [first, last) where the last element is not included in the range. The algorithms library is present in #include<algorithm> header file.
Algorithms operate directly on values through iterators and do not change the structure of the container. Thus, we can use the algorithms on any type of container. Also, this library saves our time and effort and provides us with the most reliable implementation of the algorithm.
Types of Algorithms in Algorithm Library
- Modifying algorithms
- Non-modifying algorithms
- Sorting algorithms
- Searching algorithms
- Maximum and minimum operations
Modifying algorithms
Some examples of modifying algorithms are:
copy(first1, last1, first2): It copies the elements from the range first1 to last1 excluding last1 into the range starting from first2.fill(first, last, value): It assigns the given value to all the elements in the range.swap(container1, container2): It swaps the elements of one container with another.reverse(first, last): It reverses the order of elements in the given range.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(vector <int> & v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
int main() {
vector <int> v1, v2;
for (int i = 0; i < 6; i++) {
v1.push_back(i + 1);
v2.push_back(i * 10);
}
cout << "Initially the vector1 is ";
print(v1);
cout << "Initially the vector2 is ";
print(v2);
copy(v2.begin(), v2.end(), v1.begin());
cout << "After copy operation, the vector1 is ";
print(v1);
fill(v1.begin(), v1.end(), 5);
cout << "After fill operation, the vector1 is ";
print(v1);
swap(v1, v2);
cout << "After swapping, the vectors are\n" << "vector1 is: ";
print(v1);
cout << "vector2 is: ";
print(v2);
reverse(v1.begin(), v1.end());
cout << "After reverse operation, the vector1 is ";
print(v1);
}Output
Initially the vector1 is 1 2 3 4 5 6
Initially the vector2 is 0 10 20 30 40 50
After copy operation, the vector1 is 0 10 20 30 40 50
After fill operation, the vector1 is 5 5 5 5 5 5
After swapping, the vectors are
vector1 is: 0 10 20 30 40 50
vector2 is: 5 5 5 5 5 5
After reverse operation, the vector1 is 50 40 30 20 10 0 Non Modifying algorithms
Some examples of non-modifying algorithms are:
count(first, last, value): It returns the number of occurrences of the given value in the given range.equal(first1, last1, first2): It compares the value in the given range and returns true if the values are equal otherwise false.search(first1, last1, first2, last2): It swaps the elements of one container with another.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(vector <int> & v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
int main() {
vector <int> v1, v2;
for (int i = 0; i < 6; i++) {
v1.push_back(i + 1);
v2.push_back(i * 10);
}
v1.push_back(1);
v1.push_back(1);
cout << "Initially the vector1 is ";
print(v1);
cout << "Initially the vector2 is ";
print(v2);
int c = count(v1.begin(), v1.end(), 1);
cout << "Number of occurrences of 1 in vector 1: " << c << '\n';
cout << "Is vector1 equal to vector2, 0 represents false, 1 represents true: ";
cout << equal(v1.begin(), v1.end(), v2.begin() + 2) << '\n';
vector < int > ::iterator i;
i = search(v1.begin(), v1.end(), v2.begin(), v2.end());
//i now points to the end of v1
}Output
Initially the vector1 is 1 2 3 4 5 6 1 1
Initially the vector2 is 0 10 20 30 40 50
Number of occurrences of 1 in vector 1: 3
Is vector1 equal to vector2, 0 represents false, 1 represents true: 0Sorting algorithms
Some examples of sorting algorithms are:
sort(start_iterator, end_iterator): It sorts the elements of the container in the given range.partial_sort(start, middle, end): It sorts the elements partially in the range (start, end) such that elements from start to middle are sorted in ascending order and are smallest in the container,is_sorted(start_iterator, end_iterator): It returns true if the elements in the given range are sorted.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(vector <int> & v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
int main() {
vector <int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};;
cout << "Initially the vector is ";
print(v);
partial_sort(v.begin(), v.begin() + 4, v.begin() + 9);
cout << "After partial sorting, the vector is ";
print(v);
sort(v.begin(), v.end());
cout << "After sorting, the vector is ";
print(v);
cout << "Is the vector sorted now, 1 represents true, 0 represents false: " << is_sorted(v.begin(), v.end());
}Output
Initially the vector is 9 8 7 6 5 4 3 2 1
After partial sorting, the vector is 1 2 3 4 9 8 7 6 5
After sorting, the vector is 1 2 3 4 5 6 7 8 9
Is the vector sorted now, 1 represents true, 0 represents false: 1Searching algorithms
Some examples of searching algorithms are:
binary_search(first, last, value): It searches for the value in the given range and returns true if it is found.equal_range(first, last, value): It returns a subrange of elements where the elements are equal to the given value.upper_bound(start_iterator, end_iterator, value): It returns the upper bound of the value in the given sorted range.lower_bound(start_iterator, end_iterator, value): It returns the lower bound of the value in the given sorted range.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(vector <int> & v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
int main() {
vector <int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};;
cout << "Initially the vector is ";
print(v);
partial_sort(v.begin(), v.begin() + 4, v.begin() + 9);
cout << "After partial sorting, the vector is ";
print(v);
sort(v.begin(), v.end());
cout << "After sorting, the vector is ";
print(v);
cout << "Is the vector sorted now, 1 represents true, 0 represents false: " << is_sorted(v.begin(), v.end());
}Output
Initially the vector is 9 8 7 6 5 4 3 2 1
After partial sorting, the vector is 1 2 3 4 9 8 7 6 5
After sorting, the vector is 1 2 3 4 5 6 7 8 9
Is the vector sorted now, 1 represents true, 0 represents false: 1Maximum and Minimum operations
Some functions performing minimum and maximum operations are:
min(value1, value2): It returns the minimum of the two values.max(value1, value2): It returns the maximum of the two values.min_element(start_iterator, end_iterator): It returns the minimum element in the given range.max_element(start_iterator, end_iterator): It returns the maximum element in the given range.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(vector <int> & v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << '\n';
}
int main() {
vector <int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1};
cout << "Initially the vector is ";
print(v);
sort(v.begin(), v.end());
cout << "Is element 7 present in the vector, 1 represents true, 0 represents false: ";
cout << binary_search(v.begin(), v.end(), 7) << '\n';
pair < vector <int> ::iterator, vector <int> ::iterator > sub_range;
sub_range = equal_range(v.begin(), v.end(), 2);
vector <int> ::iterator it1, it2;
it1 = upper_bound(v.begin(), v.end(), 6);
cout << "Upper bound of 6 is " << * it1 << '\n';
it2 = lower_bound(v.begin(), v.end(), 9);
cout << "Lower bound of 9 is " << * it2 << '\n';
}Output
Initially the vector is 9 8 7 6 5 4 3 2 1
Is element 7 present in the vector, 1 represents true, 0 represents false: 1
Upper bound of 6 is 7
Lower bound of 9 is 9