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
77
votes
6 answers

How do I write a range pipeline that uses temporary containers?

I have a third-party function with this signature: std::vector f(T t); I also have an existing potentially infinite range (of the range-v3 sort) of T named src. I want to create a pipeline that maps f to all elements of that range and flattens…
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
32
votes
2 answers

Why didn't `accumulate` make it into Ranges for C++20?

I suspect that accumulate isn't the only algorithm that didn't make it. Maybe now there's a better way to perform accumulation (folding) over a range and therefore the accumulate is obsolete?
GreenScape
  • 7,191
  • 2
  • 34
  • 64
30
votes
1 answer

Why is Range-v3 slower than the STL in this example?

I'm playing around with the Range-v3 library to perform a glorified find_if and was curious why google-benchmark consistently ranks my Range-v3 code worse than my std::find_if approach. Both g++ and clang give the same pattern with -O3 and #define…
fast asleep
  • 315
  • 3
  • 8
25
votes
3 answers

Range-v3 operator overloading to write shorter code

For my matrix class I want to do some sort of operator overloading (probably using expression templates) on range-v3 views for + - / * % . For example if I want to get a view of the sum of two columns, I want to write col_1 + col_2 instead…
Porsche9II
  • 629
  • 5
  • 17
24
votes
1 answer

Is there a trait or any convention to check if a range or a `view_facade` that owns things? (e.g. getlines)

Given auto empty_line = [](auto& str){ return str.size() == 0; }; For that fact that ranges::getlines returns an owning view_facade that owns a buffer for its frontal iterator. So we are kind of obligated to make that kind of range an lvalue before…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
22
votes
3 answers

How do I create a range from a begin and end iterator?

I have an object with functions for getting the begin and end iterators: const_iterator err_begin() const const_iterator err_end() const Because they are not named begin and end, I cannot pass my object directly to functions in range-v3. Is…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
17
votes
1 answer

How to split a std::string into a range (v3) of std::string_views?

I need to split a std::string at all spaces. The resulting range should however transform it's element to std::string_views. I'm struggling with the "element type" of the range. I guess, the type is something like a c_str. How can I transform the…
dani
  • 3,677
  • 4
  • 26
  • 60
16
votes
5 answers

Finding minimum element based on a transformed value

Here is the task came to me from a code review. I want to select a minimum value from a set, based on a special kind of compare predicate. Like this: struct Complex { ... }; float calcReduction(Complex elem); Complex…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
15
votes
1 answer

Why can ranges not be used for the pipes library functionality?

Jonathan Boccara (author of Fluent C++) wrote a library called pipes. This "piping", the repository's main page says, is not like the use of ranges, even though it looks the same: It's not based on lazy pulling, but rather eager pushing. But it's…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
14
votes
1 answer

Will we be able to construct containers with views in C++20?

Ranges are coming to C++ with the C++20 standard version. My question: Will we be able to construct (existing) standard-library containers with any range? And more importantly, with range views? For example, will this: #include #include…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
14
votes
2 answers

STL/ranges algorithm to calculate weighted average

Assume I have a vector of grades, where the grade is struct Grade{ const int grade; const int ECTS; // weight }; Is there a STL/range-v3 algorithm/algorithms that enable me to do this? I know I could do it with std:: accumulate with some…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
14
votes
1 answer

Why aren't ranges' algorithms compatible with std's iterators?

#include #include #include int main() { auto coll = std::vector{ 1, 2, 3 }; ranges::copy( coll, ranges::ostream_iterator{ std::cout, ", " } ); // ok ranges::copy( …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
13
votes
1 answer

What is the difference between iterator_category and iterator_concept in C++20?

C++20 brings a more powerful iterator system, one of them is to introduce iterator_concept on the basis of iterator_category. I found that the iterator_concept and iterator_category of many iterators in C++20 are inconsistent. Take the most famous…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
13
votes
0 answers

Why does clang think gcc's subrange does not satisfy gcc's __ranges_begin function concept equirements?

The code that fails for clang (while gcc seems okey with it) int arr[] { 111, 222, 333}; ranges::subrange( ranges::begin(arr),ranges::end(arr) ); It looks like clang claim gcc's subrange do not have begin…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
13
votes
2 answers

With Range v3 ranges, how to combine views and actions into a single pipeline?

I'm learning C++20 ranges (using Range-V3-VS2015). I have this code that works fine: string clean; auto tmp1 = input | view::remove_if(not_alpha) | view::transform(::tolower); std::copy(tmp1.begin(), tmp1.end(), std::back_inserter(clean)); auto tmp2…
busfahrer
  • 211
  • 1
  • 8
1
2 3
20 21