//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include template struct EndValue { bool operator== (auto pos) const { return *pos == End; // end is where iterator points to End } }; int main() { std::vector coll = {42, 8, 0, 15, 7, -1}; // define a range referring to coll with the value 7 as end: std::ranges::subrange range{coll.begin(), EndValue<7>{}}; // sort the elements of this range: std::ranges::sort(range); // print the elements of the range: std::ranges::for_each(range, [] (auto val) { std::cout << ' ' << val; }); std::cout << '\n'; // print all elements of coll up to -1: std::ranges::for_each(coll.begin(), EndValue<-1>{}, [] (auto val) { std::cout << ' ' << val; }); std::cout << '\n'; }