std::ranges::subrange
and std::span
both provide views over a sequence of elements without owning the elements themselves, but they serve different purposes and have distinct characteristics.
std::ranges::subrange
has the following characteristics:
std::ranges::subrange
is part of the Ranges library, introduced in C++20. It provides a way to create views of a range defined by iterators.The following program uses std::ranges::subrange
to view part of an underlying std::vector
:
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> Nums{1, 2, 3, 4, 5};
std::ranges::subrange View{
Nums.begin() + 1, Nums.end() - 1};
for (int n : View) {
std::cout << n << ", ";
}
}
2, 3, 4,
std::span
has these characteristics:
std::span
is part of the Utilities library and was introduced in C++20. It provides a view over a contiguous sequence of elements, typically from an array or a std::vector
.std::span
views are fixed at the time of creation and cannot be resized.std::ranges::subrange
, focusing on contiguous memory.The following example program uses std::span
to create a view of an underlying std::vector
:
#include <vector>
#include <span>
#include <iostream>
int main() {
std::vector<int> Nums{1, 2, 3, 4, 5};
std::span<int> View{Nums.data(), Nums.size()};
for (int n : View) {
std::cout << n << ", ";
}
}
1, 2, 3, 4, 5,
std::ranges::subrange
is more flexible, working with various iterators and non-contiguous memory, while std::span
is limited to contiguous memory.std::ranges::subrange
is useful for range-based operations and slicing, whereas std::span
is more suited for working with contiguous arrays and interfacing with legacy APIs.std::ranges::subrange
for general range operations and std::span
when you need a simple, contiguous view of data.By understanding these differences, you can choose the appropriate type for your specific needs in C++.
Answers to questions are automatically generated and may not have been reviewed.
std::ranges::subrange
This lesson introduces std::ranges::subrange, allowing us to create non-owning ranges that view some underlying container