CKTIS / C++
std::views::take — a view of a range's first elements
std::views::take, declared in <ranges>, creates a view of at most count leading elements of a range. It preserves their order, does not create a separate container, and supports both direct calls and pipeline notation.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <ranges>
std::views::take(range, count)
range | std::views::take(count)std::views::take is a range adaptor object provided by <ranges>. The expression std::views::take(range, count) corresponds to range | std::views::take(count). For a nonnegative count representable in the range's iterator difference type, the result exposes the first count elements in their original order, or the entire range if it contains fewer elements. When count is 0, the result is empty.
This is a view, not a copied container: applying the adaptor itself neither copies elements into a new collection nor removes trailing elements from the source. Elements are read during iteration. The result need not have type std::ranges::take_view; the standard specifies other equivalent view representations for certain kinds of ranges, so auto is usually appropriate.
The count argument must be convertible to the range's iterator difference type. Use a nonnegative count representable in that type, even when the adaptor does not construct a std::ranges::take_view. For example, in the optimized std::span case, the adaptor converts count to that type before taking the minimum of the converted count and the range's length. An unsigned count too large to represent can convert to -1 and lead to invalid iterator arithmetic rather than selecting the whole range. When the adaptor constructs a std::ranges::take_view, the constructor requires the converted count to be nonnegative. A negative count does not mean counting from the end. If the input range is a specialization of std::ranges::empty_view, the adaptor evaluates and discards the count argument without constructing a std::ranges::take_view, so a negative count does not violate that precondition.
When adapting an existing container through an lvalue, the view refers to that container. The source must therefore remain alive while the view is used, and operations that invalidate iterators may also invalidate access through the view. In the example, changing values[0] after creating first is reflected in the output: 11 20 30. The displayed code was run unchanged in an isolated environment. Its standard output exactly matched the stated output.
Examples
#include <array>
#include <iostream>
#include <ranges>
int main() {
std::array<int, 5> values{10, 20, 30, 40, 50};
auto first = values | std::views::take(3);
values[0] = 11;
bool separator = false;
for (int value : first) {
if (separator) std::cout << ' ';
std::cout << value;
separator = true;
}
std::cout << '\n';
}
Result
11 20 30
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.