Questions tagged [range-v3]

range-v3 is a range library for C++14/17/20.

range-v3 is a range library for C++14/17/20. It is the basis of a formal proposal (N4128 Ranges for the Standard Library) to add range support to the C++ standard library. It also is the reference implementation for a Technical Specification (N4560 Working Draft, C++ extensions for Ranges). Source is available on Github.

313 questions
9
votes
3 answers

How to implement flatmap using rangev3 ranges

I have a pretty simple flatmap function implemented in C++ for std::vector, but it has been suggested that ranges are generally better. Here's the vector based solution: // flatmap: [A] -> (A->[B]) -> [B] template
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
8
votes
2 answers

Why aren't temporary container objects pipeable in range-v3?

Why is the following #include #include #include std::vector some_ints() { return { 1,2,3,4,5 }; } int main() { auto num_strings = some_ints() | ranges::views::transform([](int n) {return…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
8
votes
6 answers

Is it possible / advisable to return a range?

I'm using the ranges library to help filer data in my classes, like this: class MyClass { public: MyClass(std::vector v) : vec(v) {} std::vector getEvens() const { auto evens = vec | ranges::views::filter([](int i) {…
Liam Goodacre
  • 381
  • 1
  • 10
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
1 answer

Can I pipe to range-v3 accumulate?

I found older questions from 3y ago that say that in general it is not possible, but I would really like to pipe to accumulate since it is quite nice in some cases, for example this: const double val = data | transform(...) | accumulate (...); So I…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
8
votes
1 answer

Why is this rangev3 implementation of vectors summation slower than the STD equivalent?

I am considering using rangev3 in a library of mine. I like rangev3's syntax, but the priority is performance. The library runs lots of vector multiplications and additions, mostly 128 samples long. I used Google benchmark to assess for instance…
Enzo
  • 964
  • 1
  • 9
  • 20
8
votes
1 answer

Iterating over container or range - problem with constness

I am trying to write a template function that will sum up all elements of some collection - specified either as a plain stl container, or as a ranges-v3's range. (The actual function, as shown below is a bit more generic) I thought this would…
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
8
votes
2 answers

C++ range-v3 library: 'take'-ing first 3 perfect numbers works and halts; 'take'-ing first 4 doesn't stop after 4

As I understand it, the range-v3 library's view operations (requires C++17 currently, but to become an official part of the STL in C++20) provides chainable STL-like algorithms that are lazily evaluated. As an experiment, I created the following…
jwimberley
  • 1,696
  • 11
  • 24
8
votes
2 answers

Why aren't range v3 algorithms pipeable?

It seems that the algorithms in ranges v3 aren't chainable, i.e: const auto ints = std::vector{1,2,1,3,1,4,1,5,1,6}; const auto num_ones = ints | ranges::count(1); ... has to be written functional style: const auto num_ones =…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
8
votes
1 answer

Range-v3: Use view_facade to provide both const and non-const iterators

I am having trouble using view_facade (from range-v3) to create a view that provides both const and non-const access. As an example, I tried modifying the view_facade test (in test/view_facade.cpp) to allow non-const access (by default it only…
edflanders
  • 213
  • 1
  • 6
7
votes
1 answer

ranges-v3 join function to join two containers together

I've been trying to understand the Range-v3 join documentation but I'll be honest, I don't understand it. And I've not been able to find any relevant examples either. Could someone show me how to create a joined view of two deque vectors please.…
Andrew
  • 626
  • 6
  • 16
7
votes
2 answers

Why does `iota(0) | take(0)` not model ranges::sized_range in C++20?

Consider the following code snippet: #include auto r = std::views::iota(0) | std::views::take(0); static_assert(std::ranges::sized_range); gcc-trunk rejects it for required-expression std::ranges::size(r) is invalid. Why does…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
7
votes
2 answers

How to implement a lazily evaluated function on two C++20 ranges?

There is a zip_with function provided by Eric Niebler. But, now that C++20 have support for ranges I would like to build something similar. The problem with filter and transform is that they iterate a range? How would I go about doing this? I have…
Aniket Chowdhury
  • 332
  • 3
  • 13
7
votes
1 answer

Why can't I reverse a split-range using range-v3?

I want to split, reverse, and then join a string using range-v3. However, code below won't compile. #include #include using namespace ranges; int main(int argc, char *argv[]) { auto str =…
joyqat
  • 73
  • 5
7
votes
0 answers

Different assembly when rangifying a simple algorithm

When I was preparing supplementary info for this question, I noticed that “rangified” implementations of a very simple algorithm resulted in important differences (to my eyes) in the resulting assembly, compared with “legacy” implementations. I…
metalfox
  • 6,301
  • 1
  • 21
  • 43
1 2
3
20 21