Reputation: 4673
How do I create a ranges-v3-compatible range, given a traditional pair of "begin" and "end" iterators?
Let's say that I am writing a generic function that accepts two iterators, for compatibility with legacy code.
struct result;
bool keep_line(const std::string&);
result parse_line(const std::string&);
template <typename InputIt>
std::vector<result> parse_lines(InputIt begin, InputIt end)
{
// This is what I want to do...
auto lines = ranges::make_range_out_of_legacy_iterators(begin, end);
return lines
| ranges::view::filter(keep_line)
| ranges::view::transform(parse_line)
| ranges::to<std::vector<result>>();
}
Upvotes: 15
Views: 4824
Reputation: 4673
To create a range from a pair of iterators in ranges-v3, use the subrange
view:
#include <range/view/subrange.hpp>
auto lines = ranges::subrange(begin, end); // Requires C++17-style deduction
auto lines = ranges::make_subrange(begin, end); // If template deduction not available
In old versions of the library, the iterator_range
class in range/v3/iterator_range.hpp
was apparently used, but that header is marked deprecated in the current ranges-v3
release (0.9.1).
Upvotes: 17