Example of using generator iterators with range for
and mnaually.
#include <cstddef>
#include <iostream>
namespace {
{
int v = 0;
while (static_cast<std::size_t>(v) < n) {
co_yield v;
++v;
}
}
}
int main()
{
std::cout << "The first 5 numbers are:\n";
for (auto n : first_n(5)) {
std::cout << n << ' ';
}
std::cout << '\n';
auto gen = first_n(5);
for (auto it = gen.begin(); it != gen.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
return 0;
}
An synchronous generator that produces values of type Result.