Questions tagged [std-ranges]

A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from header and of range adaptors divided into views and actions.

379 questions
4
votes
2 answers

Why use std::ranges algorithms over regular algorithms?

cppreference states: The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone. The library creates and manipulates range views,…
dnl
  • 95
  • 7
4
votes
2 answers

Why doesn't std::ranges::contains try using member contains just like std::ranges::begin tries using member begin? Or does it?

std::begin can call the .begin member function if the argument has it. Also std::ranges::begin seems to do the same. However, at the page for std::ranges::contains I see no mention of the word member, nor of .contains. Why is that? I mean, if I was…
Enlico
  • 23,259
  • 6
  • 48
  • 102
4
votes
1 answer

Ranges filter_view::iterator element modification results in UB

The initial question was why with the following code, std::vector coll{1,4,7,10}; auto iseven = [](auto&& i){return i % 2 == 0; }; auto colleven = coll | std::views::filter(iseven); // first view materialization for(int& i : colleven) { …
gonidelis
  • 885
  • 10
  • 32
4
votes
1 answer

What is the rationale behind the iterator/sentinel of range adaptors providing the base() accessor?

Legacy iterator adaptors such as reverse_iterator and move_iterator, or C++20/23 newly introduced adaptors such as counted_iterator, basic_const_iterator, and move_sentinel, all of them provide base() member to allow us to access the underlying…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
1 answer

How to correctly manage resource ownership when using range::views?

I am currently reading about the range library. My question is about resource ownerships when using views, and how to use these safely. Let's consider the following code, which print the even year in reverse order: #include #include…
GabrielGodefroy
  • 198
  • 1
  • 8
4
votes
5 answers

Is using ranges in c++ advisable at all?

I find the traditional syntax of most c++ stl algorithms annoying; that using them is lengthy to write is only a small issue, but that they always need to operate on existing objects limits their composability considerably. I was happy to see the…
Bubaya
  • 615
  • 3
  • 13
4
votes
2 answers

Does std::ranges::to allow converting to a std::map?

In the std::ranges::to paper wg21.link/p1206 ths overview section has the following //Supports converting associative container to sequence containers auto f = ranges::to(m); However I can't find where the detail of converting to a std::map…
goneskiing
  • 1,659
  • 1
  • 13
  • 22
4
votes
1 answer

What does the vertical pipe | mean in the context of c++20 and ranges

There are usages of | which look more like function pipe-lining or chaining rather than a bitwise or, seen in combination with the c++20 ranges. Things like: #include #include template std::vector square_vector(const…
kabanus
  • 24,623
  • 6
  • 41
  • 74
4
votes
3 answers

Parallel for loop with std::for_each and std::views::iota

I want to set up an easy workaround for parallelized index-based for-loops using std::views. For running in sequence the code looks like this: int main() { //pseudo-random numbers random_device rd; default_random_engine eng(rd()); …
Urwald
  • 343
  • 1
  • 10
4
votes
2 answers

Why does c++ for each loops accept r-values but std::ranges do not?

A statement like this compiles without error: for (int i : std::vector({0, 1})) std::cout << i; But a statement like this does not: std::vector({0, 1}) | std::views::filter([](int i) { return true; }); Why are r-values allowed in for…
dromodel
  • 9,581
  • 12
  • 47
  • 65
4
votes
1 answer

How to sort a vector using std::views C++20 feature?

I want to loop through a vector in a sorted way without modifying the underlying vector. Can std::views and/or std::range be used for this purpose? I've successfully implemented filtering using views, but I don't know if it is possible to sort using…
Victor
  • 133
  • 7
4
votes
1 answer

why do ranges algorithms take rvalue reference as argument

if I take for example the ranges::fill algorithm: https://en.cppreference.com/w/cpp/algorithm/ranges/fill the signature is: template< class T, ranges::output_range R > constexpr ranges::borrowed_iterator_t fill( R&& r, const T& value…
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
4
votes
2 answers

Get projected value from std::ranges algorithms

I am using algorithms from std::ranges (max and max_element) with a projection. Is it possible for the result to also be the projected value? Currently I have to call the projection function again on the returned value. Example: Here I want the size…
perivesta
  • 3,417
  • 1
  • 10
  • 25
4
votes
1 answer

Convert borrowed_iterator> to borrowed_iterator

I have buffer: char line[1024] which contains a line read from a file. I want to find the last new-line (\n) in it, then replace all , with (space) before it. The code I came up with: const auto end = rng::find(line | rng::views::reverse, '\n');…
Pirulax
  • 80
  • 1
  • 7
4
votes
2 answers

Why doesn't std::ranges::upper_bound accept heterogenous comparing?

This code works and returns an iterator to foo{5} from the vector: struct foo { int value; }; auto main() -> int { auto ints = std::vector{{3}, {2}, {5}, {6}, {7}, {0}, {4}, {6}}; std::ranges::sort(ints, {}, &foo::value); auto…
Fureeish
  • 12,533
  • 4
  • 32
  • 62