The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <print>
#include <vector>
#include <chrono>
#include <ranges>
constexpr bool isPrime(int value)
{
for (int i = 2; i <= value/2; ++i) {
if (value % i == 0) {
return false;
}
}
return value > 1; // 0 and 1 are not prime numbers
}
int main()
{
auto asSeconds = [] (auto val) {
return std::chrono::seconds{val};
};
std::vector coll{4, 11, 31, 88, 17, 27, 31, 2, 67, 15};
auto v = coll | std::views::filter(isPrime)
| std::views::take(3)
| std::views::transform(asSeconds);
std::println("v: {}", v);
}