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
7
votes
1 answer

range v3 flattening a sequence

So I recently watched this talk on c++: https://www.youtube.com/watch?v=mFUXNMfaciE And I was very interested on trying it out. So after some toy program I am stuck on how to properly flatten a vector of vectors into a vector. According to the…
user2882307
7
votes
1 answer

Split range into range of overlapping ranges

I attempting to use the Ranges-V3 library to slice up an container of values into a range of ranges such that neighboring ranges share boundary elements. Consider the following: using namespace ranges; std::vector v = { 1, 2, 3, 0, 4, 0, 5, 0,…
apmccartney
  • 743
  • 8
  • 16
7
votes
1 answer

Using gsl::span with range-v3

I tried a little example to get used to the GSL and range-v3 libraries and I wondered how they could work together. I have this toy example #include #include using namespace std; using namespace ranges; void…
Maikel
  • 1,204
  • 7
  • 19
7
votes
2 answers

How to write a range-v3 action for random_shuffle?

Using the range-v3 library (by @EricNiebler), makes writing algorithmic code much more compact, e.g. here's how to generate a bunch of random numbers: #include #include #include int main() { using…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
6
votes
1 answer

Removing elements marked for removal with Ranges-V3

I've got two vectors: struct MyData{ double value; }; std::vector remove_flags = {0, 1, 0, 0, 0, 0, 1, 0}; std::vector data = {{},{},{},{},{},{},{},{}}; The remove_flags vector contains an array of flags of the exact same…
Krupip
  • 4,404
  • 2
  • 32
  • 54
6
votes
2 answers

Why is ranges::basic_istream_view::begin() not cached?

I found that c++20 ranges::basic_istream_view is slightly different from the range-v3 version. The most important difference is that the std::ranges::basic_istream_view does not cache its begin(), so that each begin()s will return the next iterator…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

Generator called twice in C++20 views pipeline

Here in a simple pipeline of views adaptors, there is the gen function called to generate a sequence of values (using an internal state) and then a filter on it. What is surprising and counterintuitive (at least for me) is the fact that the…
Pascal H.
  • 1,431
  • 8
  • 18
6
votes
1 answer

Why can views::reverse transform a non-sized_range into a size_range?

In [range.sized#1]: The sized_­range concept refines range with the requirement that the number of elements in the range can be determined in amortized constant time using ranges​::​size. template concept sized_­range = range && …
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

Transform Temporary Vector with Range-v3

I've been doing a lot of reading regarding range-v3 views, actions, and how they interact with temporary collections, but I still feel like I'm missing something simple that would help me accomplish what I feel like should work. Given the…
Aaron Wright
  • 328
  • 2
  • 11
6
votes
2 answers

Concatenate multiple range adaptors into one single ranges in C++20

Consider the following case: std::vector v{0, 1, 2, 3, 4, 5}; // 0 1 2 3 4 5 auto rng1 = std::views::all(v); // 5 4 3 2 1 0 auto rng2 = std::views::reverse(v); // 4 2 0 auto rng3 = std::views::filter(rng2, [](int x){return x % 2 == 0;}); Is…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
4 answers

How to zip vector of vector with range-v3

(This is a follow-on to Sum vector with range-v3) If I have two (or more) vectors, I can zip them together with range-v3 like this: std::vector< int > v1{1,1,1}; std::vector< int > v2{2,2,2}; auto v = ranges::views::zip( v1, v2 ) |…
jlconlin
  • 14,206
  • 22
  • 72
  • 105
6
votes
2 answers

How to use C++ ranges to implement numpy.ndindex?

I'd like to implement equivalent of numpy.ndindex in C++. It is supposed to generate indices for a multi-dimensional array of specified dimensions. Here is the implementation for a 2D array. template inline auto NDIndex(T d0, T d1) { …
pkubik
  • 780
  • 6
  • 19
6
votes
2 answers

Why do C++20 ranges not provide only pipe syntax?

I understand that question sounds weird, so here is a bit of context. Recently I was disappointed to learn that map reduce in C++20 ranges does not work as one would expect i.e. const double val = data | transform(...) | accumulate (...); does not…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
6
votes
1 answer

How to pass a smart iterator to a function that accepts a classic iterator?

I'm trying to familiarize with the ranges-v3 library, that will be part of the C++20 standard. To do so, I'm trying to refactor some toy code by replacing (where suitable) classic iterators and algorithms with the new available constructs. In this…
Rackbox
  • 283
  • 1
  • 11
6
votes
1 answer

How to hide the complex range type of a range-v3?

I need a class with a method that returns some kind of range using the range-v3 library. In order to implement such a class I can write it everything right in the definition of that class. For example: #include #include #include…
CygnusX1
  • 20,968
  • 5
  • 65
  • 109