Skip to content

Instantly share code, notes, and snippets.

@ola-sk
Last active November 21, 2021 20:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ola-sk/8a95296debee637d3801797fd467c8ab to your computer and use it in GitHub Desktop.
Save ola-sk/8a95296debee637d3801797fd467c8ab to your computer and use it in GitHub Desktop.
C++ STL Algorithms
#include <vector>
#include <string>
#include <algorithm>
#include <map>
int main()
{
std::vector<int> v { 2, 5, 7, 1, 0, 6, 6, 2, -2, 4, 4, 7, 9, 0, 5 }; //6 odd values
//——————————————————————————————————————————————————————————————————————————
//COUNTING
//FUNCTIONS: count | count_if | all_of | any_of | none_of |
// count()
//——————————————————————————————————————————————————————————————————————————
//count how many entries have the target value (2)
int const target = 2;
int count_twos = count(begin(v), end(v), target);
//count_if()
//——————————————————————————————————————————————————————————————————————————
// count how many entries are odd
int count_odds = count_if(begin(v), end(v), [](auto elem) { return elem % 2 != 0; });
// count how many long months are there
std::map<int, int> month_lengths{{1, 31}, {2, 28}, {3, 31}, {4, 30}, {5, 31}, {6, 30}, {7, 31}, {8, 31}, {9, 30}, {10, 31}, {11, 30}, {12, 31}};
int long_months = count_if(begin(month_lengths), end(month_lengths), [](auto elem) { /*elem is a pair*/ return elem.second == 31; });
//all_of(), any_of(), none_of()
//——————————————————————————————————————————————————————————————————————————
// See whether all elements of a collection are odd
bool allof = all_of(begin(v), end(v), [](auto elem) { return elem % 2 != 0; });
// See whether any element of a collection is odd
bool anyof = any_of(begin(v), end(v), [](auto elem) { return elem % 2 != 0; });
// See whether none of elements of a collection are odd
bool noneof = none_of(begin(v), end(v), [](auto elem) { return elem % 2 != 0; });
//——————————————————————————————————————————————————————————————————————————
//FINDING
//FUNCTIONS: find | find_if | find_if_not | find_first_of | search | find_end | search_n | adjacent_find |
// find()
//——————————————————————————————————————————————————————————————————————————
// find the first zero in the collection
std::vector<int>::iterator result = find(begin(v), end(v), 0);
int we_looked_for;
if (result != end(v))
{
we_looked_for = *result; // result is a pointer
}
// find the first 2 after that 0
result = find(result, end(v), 2); // result as a "starting from" iterator
if (result != end(v))
{
we_looked_for = *result;
}
//find the first 'a'
std::string s{ "So, find this a!" };
auto letter = find(begin(s), end(s), 'a');
if (letter != end(s))
{
char a = *letter; // result is a pointer
}
//find_if()
//——————————————————————————————————————————————————————————————————————————
// find the first occurence of an odd value (in a vector)
result = find_if(begin(v), end(v), [](auto elem) { return elem % 2 != 0; });
if (result != end(v))
{
we_looked_for = *result;
}
// find_if_not()
//——————————————————————————————————————————————————————————————————————————
// find first even value
result = find_if_not(begin(v), end(v), [](auto elem) { return elem % 2 != 0; });
if (result != end(v))
{
we_looked_for = *result;
}
// find_first_of()
//——————————————————————————————————————————————————————————————————————————
// here as third and fourth arguments we pass the iterators to
// the beginning and an end of a collection of values, one of which we wanna find.
std::vector<int> primes = { 1, 2, 3, 5, 7, 11, 13, 17 };
result = find_first_of(begin(v), end(v), begin(primes), end(primes));
if (result != end(v))
{
we_looked_for = *result;
}
// search()
//——————————————————————————————————————————————————————————————————————————
// Rather than for a single value—it looks for a sequence.
std::vector<int> subsequence = { 2, 4 };
result = search(begin(v), end(v), begin(subsequence), end(subsequence));
if (result != end(v))
{
we_looked_for = *result;
}
// find_end()
//——————————————————————————————————————————————————————————————————————————
// it's exactly like search, but it gives you back the iterator to the first element of the
// last occurrence of a subsequence.
//std::vector<int> subsequence = { 2, 4 };
result = search(begin(v), end(v), begin(subsequence), end(subsequence));
if (result != end(v))
{
we_looked_for = *result;
}
// search_n()
//——————————————————————————————————————————————————————————————————————————
result = search_n(begin(v), end(v), 2, 4);
if (result != end(v))
{
we_looked_for = *result;
}
//adjacent_find()
//——————————————————————————————————————————————————————————————————————————
// Finds two consequtive elements that have the same value (any value—that's why the function
//doesn't take any more arguments)
// they can be any types of values: two int values for a collection of integers
// or two same customers for a collection of customers.
// returns an iterator to the first occurence of adjacent elements
result = adjacent_find(begin(v), end(v));
int six;
if (result != end(v))
{
//there are two sixes in a row in v,
//so the value expected is 6, so the variable name for dereferencing the result
six = *result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment