Below are the operations that are used to perform on STL containers.
Most of the functions are function template. Hence they can be used to work with primitive and also user defined data types.
To use these algorithms, below header should be included:
#include <algorithm>
all_of : Test condition on all elements in range [C++ 11]
any_of : Test if any element in range fulfills condition [C++ 11]
none_of : Test if no elements fulfill condition [C++ 11]
for_each : Apply function to range
find : Find value in range
find_if : Find element in range
find_if_not : Find element in range [C++ 11]
find_end : Find last subsequence in range
find_first_of : Find element from set in range
adjacent_find : Find equal adjacent elements in range
count : Count appearances of value in range
count_if : Return number of elements in range satisfying condition
mismatch : Return first position where two ranges differ
equal : Test whether the elements in two ranges are equal
is_permutation :Test whether range is permutation of another [C++ 11]
search : Search range for subsequence
search_n : Search range for elements
In below series of examples, we shall see operations:
Example 1: Usage of all_of, any_of, for_each
#include <iostream>
#include <vector>
#include <array>
#include <algorithm> // std::all_of
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
void myfunction (int i) // function to print
{
std::cout << ' ' << i;
}
int main ()
{
std::vector<int> foo = {3,5,7,11,13,17,19,23};
// below code will check if the elements in foo are all odd numbers
if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
std::cout << "All the elements are odd numbers by using all_of.\n";
// below code will check if any one of the element is a negative number
std::array<int,7> myarr = {0,1,1,3,3,5,-5};
if ( std::any_of(myarr.begin(), myarr.end(), [](int i){return i<0;}) )
std::cout << "There are negative elements in the range.\n";
std::cout << "\nDisplaying all the elements in the vector \"foo\" by using for_each\n";
for_each (foo.begin(), foo.end(), myfunction);
std::cout << '\n';
return 0;
}
Output:
All the elements are odd numbers by using all_of.
There are negative elements in the range.
Displaying all the elements in the vector "foo" by using for_each
3 5 7 11 13 17 19 23
Example 2: Usage of find, find_if, count, count_if
#include <iostream>
#include <vector>
#include <array>
#include <algorithm> // std::all_of
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;
bool IsOdd (int i)
{
return ((i%2)==1);
}
int main ()
{
std::vector<int> myvector = {3,5,7,11,13,17,19,23};
std::vector<int>::iterator it;
// check if 30 is present by using find()
it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
std::cout << "Element 30 found in myvector: " << *it << '\n';
else
std::cout << "Element 30 not found in myvector\n";
//check if there is an odd value by using find_if
it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
std::cout << "The first odd value is " << *it << '\n';
int mycount = std::count (myvector.begin(), myvector.end(), 5);
std::cout << "5 appears " << mycount << " times.\n";
mycount = count_if (myvector.begin(), myvector.end(), IsOdd);
std::cout << "myvector contains " << mycount << " odd values.\n";
return 0;
}
Output:
Element 30 not found in myvector
The first odd value is 3
5 appears 1 times.
myvector contains 8 odd values.