Make your iterators compatible with STL iterator traits
Many STL algorithms require iterators to conform to certain traits. Unfortunately, these requirements are inconsistent across compilers, systems, and C++ versions.
For our purposes, we'll use the class from the Create an iterable range recipe to illustrate the issue. You may find this makes more sense if you read that recipe before continuing.
In main()
, if I add a call to the minmax_element()
algorithm:
Seq<int> r{ 100, 110 }; auto [min_it, max_it] = minmax_element(r.begin(), r.end()); cout << format("{} - {}\n", *min_it, *max_it);
It does not compile. The error messages are vague, cryptic, and cascading, but if you look closely, you'll see that our iterator does not meet the requirements to be compatible with this algorithm.
Okay, let's fix that.
How to do it…
We need to make a few simple additions to our iterator to make it compatible with the algorithm. Our iterator...