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
10
votes
0 answers

clang++-12 couldn't find library ranges

I am testing ranges from C++ 20 , and this is my main.cpp: #include #include int main() { auto const ints = {0,1,2,3,4,5}; auto even = [](int i) { return 0 == i % 2; }; auto square = [](int i) { return i * i; }; …
Rahn
  • 4,787
  • 4
  • 31
  • 57
10
votes
1 answer

Vector initialisation using ranges vs lambda inline initialisation

I would like to initialise a vector by transforming the other one. I made a test with two ways of inline initialisation a transformed std::vector. One using lambda inline initialisation (with std::transform): std::vector foo(100,42); …
Patryk
  • 136
  • 8
9
votes
3 answers

O(1) find/contains in std::ranges::views::iota

I understand that iota can be complex(e.g. infinite), so this can not be done easily in general case, but in some cases it should be possible to do find/contains operation in O(1). For example int main() { auto vals = views::iota(10'000'000'000,…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
9
votes
0 answers

Is there a way in Range-v3 to prepend or append an element to a range/view?

Range-v3 has ranges::views::drop and ranges::views::drop_last to remove elements from the front or the back of a view. Does it offer a similar functions to prepend/append elements to a view? For now, the shortest way I've found is to concat the…
Enlico
  • 23,259
  • 6
  • 48
  • 102
9
votes
1 answer

How can I collect an istream_view into a container?

I was trying to implement a generic reduction operation for my extensions for c++20's ranges that would collect the elements of any range into a given container. To achieve that, I first created a dummy type for extracting a template template…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
8
votes
2 answers

If we have ranges::zip and views::transform, why do we need ranges::zip_transform?

In C++23, the ranges (sub)library has gained std::ranges::zip, which zips multiple ranges into a single range of std::tuple's (or pairs). This is nice, and precludes requiring implementing this ourselves, using boost::zip_iterator or resorting to…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
8
votes
1 answer

Passing std::ranges::views as parameters in C++20

I have a method that prints a list of integers (my actual method is a bit more complicated but it is also read-only): void printElements(const std::vector &integersList) { std::for_each(integersList.begin(), integersList.end(), [](const…
mathripper
  • 299
  • 1
  • 12
8
votes
1 answer

How to use std::views::transform on a vector?

I'm trying to create a view into a vector that transforms the type. From the doc I'd read that the following should work, but the compiler output is very confusing. What am I missing? #include #include int main() { …
OutOfBound
  • 1,914
  • 14
  • 31
8
votes
1 answer

How would you implement a lazy "range factory" for C++20 ranges that just calls a generator function?

I like the idea of the lazy ranges you can make with std::views::iota but was surprised to see that iota is currently the only thing like it in the standard; it is the only "range factory" besides views::single and views::empty. There is not…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
8
votes
2 answers

Why does std::views::take_while from the Ranges library require a const predicate?

TL;DR: I'm playing around with ranges and the corresponding range adaptors from the Ranges library. Both range adaptors std::views::take_while and std::views::filter take a predicate to exclude certain elements from the input sequence. Why does…
honk
  • 9,137
  • 11
  • 75
  • 83
8
votes
1 answer

Why does the C++20 range library have its own namespace?

Why is std::range::sort (and other range-based algorithms) implemented in the range namespace? Why isn't it defined as an overload of std::sort taking a range?
Touloudou
  • 2,079
  • 1
  • 17
  • 28
8
votes
0 answers

Why does views::split ignore an empty sub-range after a delimiter if it's the last element of the input range?

For the following code: #include #include #include int main() { std::string s = " text "; auto sv = std::ranges::views::split(s, ' '); std::cout << std::ranges::distance(sv.begin(),…
cigien
  • 57,834
  • 11
  • 73
  • 112
8
votes
2 answers

recursive application of C++20 range adaptor causes a compile time infinite loop

The ranges library in C++20 supports the expression auto view = r | std::views::drop(n); to remove the first n elements of a range r with the range adaptor drop. However if I recursively drop elements from a range, the compiler enters an infinite…
7
votes
1 answer

Is it valid to use aliased containers in `ranges::to`?

Currently only MSVC supports the nifty helper ranges::to, so I am unable to validate this in another compiler. Basically I have a type alias for a STL container and as soon as I attempt to pass it to ranges::to, compilation fails. So is this a valid…
Carsten
  • 11,287
  • 7
  • 39
  • 62
7
votes
3 answers

Sorting two arrays using C++23 zip view

There is a rather typical task of sorting two arrays simultaneously, assuming that same indexed elements of the arrays form virtual pairs, which are sorted. Such questions appear at least 10 years ago: boost zip_iterator and std::sort Now this task…
Fedor
  • 17,146
  • 13
  • 40
  • 131
1 2
3
25 26