Learn C++ · lesson 56 of 61 · about 10 minutes
More Algorithms
Find, change, test and remove items without writing the loops yourself.
What you'll learn
- Find the first match with find_if
- Change every item with transform
- Test with any_of and all_of, and remove with erase_if
The archery range
Across the bridge is the castle's archery range. The player types how many arrows the hero shoots, then the score of each, from 0 for a miss up to 10. The adventure says which arrow missed first, takes the misses away, then gives the hits and the best one, Hits: 3, best 10, and praises a steady aim if every hit scored 5 or more. Each of those questions has an algorithm made for it, so the only loop left is the one that reads the arrows.
Algorithms with lambdas
<algorithm> has a tool for most loops you'd write:
- std::find_if gives an iterator to the first item that passes a test (or end() if none does)
- std::any_of, std::all_of and std::none_of answer yes-or-no questions
- std::max_element and std::min_element find the biggest or smallest, optionally by a lambda
- std::transform changes every item
Finding and testing
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> temps = {12, 18, 25, 9, 31};
auto hot = std::find_if(temps.begin(), temps.end(), [](int t) { return t > 20; });
if (hot != temps.end()) std::cout << "First hot day: " << *hot << "\n";
bool any_freezing = std::any_of(temps.begin(), temps.end(), [](int t) { return t <= 0; });
bool all_positive = std::all_of(temps.begin(), temps.end(), [](int t) { return t > 0; });
std::cout << any_freezing << " " << all_positive << "\n";
std::cout << *std::max_element(temps.begin(), temps.end()) << "\n";
}Output
First hot day: 25
0 1
31Changing and removing
std::transform(v.begin(), v.end(), v.begin(), f) replaces every item with f(item). C++20's std::erase_if(v, test) removes every item that passes a test and gives back how many it removed.
transform and erase_if
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> prices = {4, 15, 8, 22};
std::transform(prices.begin(), prices.end(), prices.begin(), [](int p) { return p * 2; });
auto removed = std::erase_if(prices, [](int p) { return p > 20; });
for (int p : prices) std::cout << p << " ";
std::cout << "(" << removed << " removed)\n";
}Output
8 16 (2 removed)Which arrow, and in what order
find_if gives back an iterator, so miss - shots.begin() is its index, and adding 1 gives the arrow's number as a player would count it. Ask about the first miss before erase_if takes the misses away, and only use max_element once you know there's at least one hit left: on an empty vector it gives back end(), which can't be read.
Tip
find_if, max_element and min_element give back an iterator, not a value. Use * to get the item, and check find_if's result against end() first.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.