There are a ton of detailed references available for STL. Even several books
written about them. But I want cheat sheets long enough to explicitly tell me
how to use them and implicitly, tell me why/when to use them. C++
STL contains
stupefyingly useful algorithms. I will just arbitrarily list them here for
reference (not comprehensive by any means). It’s a little hard to do this
easily, so I am going to start here:
List of Useful STL
Algorithms
std::find_if
:
find odd
auto iter = std::find::if(v.begin(), v.end(), [](const auto& e_) { return (e_ % 2); }
std::transform
:
std::string s1 {"BLAH"};
std::string s2;
std::transform(s1.begin(), s1.end(), s2.begin(), std::tolower);
std::stable_partition
:
pivot_
is an iterator with prime nos. before and composites after
auto pivot_ = std::stable_partition(v.begin(), v.end(), [](const auto& e_) { return is_prime(e_); });
std::rotate
:
auto mid_ = v.begin() + 5;
std::rotate(v.begin(), mid_, v.end());
std::is_permutation
:
std::is_permutation(v1.begin(), v.begin() + 5, v2.begin());
std::next_permutation
:
bool next_possible_ = std::next_permutation(v1.begin(), v1.begin() + 11);
if (next_possible_) {
// v1 through 11 eles are rearranged based on the next avail permutation
}
std::lower_bound
:
auto iter = std::lower_bound(v.begin(), v.end(), 50);
std::move_backward
:
std::move_backward(v.begin(), v.begin() + 4, v.begin() + 5);
// [1, 2, 3, 4] => [_, 1, 2, 3, 4]
std::any_of
:
bool is_found = std::any_of(v.begin(), v.end(), [](const auto& e_) { return (is_condition(e_)); });
std::lexicographical_compare
:
std::lexicographical_compare(str1.begin(), str1.end(), str2.begin(), [](const
auto& ch1, const auto& ch2) { return (std::tolower(ch1) < std::tolower(ch2));});
std::adjacent_find
:
This snippet finds words that are out of dictionary order
auto iter = std::adjacent_find(words.begin(), words.end(),
[=](const string& w1,
const string& w2) {
return std::lexicographical_compare(w1.begin(), w1.end(), w2.begin(),
w2.end(), [](char c1, char c2) { return (!(c1 < c2)); });
});
std::is_permutation
:
bool is_perm = std::is_permutation(str1.begin(), str1.end(), str2.begin());
std::count_if
:
int count_odds_ = std::count_if(v.begin(), v.end(),
[](const int e_) { return ((e_ % 2) == 1); });
std::for_each
:
std::for_each(v.begin(), v.end(), [](auto& e_) { do_foo(e_); });
std::equal
:
std::equal(v.begin(), v.end(), [](const atuo& e1, const auto& e2) { return foo(e1) == foo(e2); });
std::set_intersection
:
auto iter = std::set_intersection(v1.begin(), v2.end, v2.begin(), v2.end(), std::back_inserter(v_intersection));
std::binary_search
:
bool found_ = std::binary_search(sorted_v.begin(), sorted_v.end(), e,
[](const auto& i, const auto& j) { return is_sorted_order(i, j); });
std::unique
:
Useful in merging adjacent/consecutive equal entries like merging intervals, even.
auto iter = std::unique(v.begin(), v.end(),
[&repeats](auto& i, auto& j) {
if (i == j) { repeats++; return false; }
return true;
});
Merging Vectors: std::move
:
v2 is the final vector v1 is the arr to be merged/deleted
std::move(v1.begin(), v1.end(), std::back_inserter(v2));
std::priority_queue
:
comp is a predicate to compare only it’s type is passed here Note: A priority queue cannot be iterated over
std::priority_queue<T, std::vector<T>, decltype(comp)> pq_;
My Podcast!
If you like topics such as this then please consider subscribing to my podcast. I talk to some of the stalwarts in tech and ask them what their favorite productivity hacks are:
Available on iTunes Podcast
Visit Void Star Podcast’s page on iTunes Podcast Portal. Please Click ‘Subscribe’, leave a comment.