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
3
votes
3 answers

What is std::views::counted?

On https://en.cppreference.com/w/cpp/ranges, std::views::counted is listed in the range adaptors section. However, it is not tagged as range adaptor object. I guess that why I can't write using the pipe operator like: std::vector vec = {1,…
GabrielGodefroy
  • 198
  • 1
  • 8
3
votes
0 answers

When to use specifically the niebloid std::ranges::construct_at instead of std::construct_at?

In the situation that we have two choices of exactly the same functionality. One is a niebloid, the other is a function. Are there any guidelines for this in general? std::construct_at -…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
3
votes
2 answers

Is it possible to make a vector of ranges in cpp20

Let's say I have a a vector>. I want to use ranges::transform in such a way that I get vector> original_vectors; using T = decltype(ranges::views::transform(original_vectors[0], [&](int x){ return x; …
3
votes
1 answer

why does ranges::view_interface::size require a move constructor

I don't understand where the requirement for moving comes from. I can't find it in forward_range and sized_sentinel... Basic example: #include #include #include class vrange: public…
serafean
  • 157
  • 1
  • 8
3
votes
1 answer

How to define a C++ concept for a range to a std::pair of reference wrappers?

See the code below (also here https://www.godbolt.org/z/hvnvEv1ar). The code fails to compile if I uncomment the constraint for either rng or pair. I feel like I am missing something trivial, but I can't figure out why the constraint is not…
MarkB
  • 672
  • 2
  • 9
3
votes
1 answer

How do I joint iterate over 2 vectors by ref?

I'm trying to iterate over 2 vectors in one go using std::views::join Here is what I'm trying: std::vector a {1, 2, 3}, b {4, 5, 6}; for (auto &v : {a, b} | std::views::join) { std::cout << v << std::endl; } This fails to…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
3
votes
2 answers

ranges and temporary initializer lists

I am trying to pass what I think is a prvalue into a range adapter closure object. It won't compile unless I bind a name to the initializer list and make it an lvalue. What is happening here? #include using namespace std; int…
danielNJ
  • 43
  • 6
3
votes
1 answer

Is std::views::values supposed to work with std::map?

The C++20 ranges library includes some (in my opinion) amazing features. For example I really started to like the std::views::keys and std::views::values range adaptors, that simplify iterating over the keys or values of a map. While previously one…
Jakob Stark
  • 3,346
  • 6
  • 22
3
votes
1 answer

C++: Alternative to this range's view construction with same requirements?

To remove manual logic in my code, I use that construct: std::ranges::drop_view a { std::ranges::take_view(my_range, my_range.size() -X), Y}; with X and Y values I pass at runtime. Even though I check the algorithms, I could not find a shorter way…
Kroma
  • 1,109
  • 9
  • 18
3
votes
0 answers

Why is it undefined behavior to call `std::subrange` in a `std::string` `std::functional` lambda stored in a `std::vector`?

Consider the following code, which contains a templated std::vector type of std::function objects which consume a const & std::range and std::range::iterator. What causes the position std::ranges::iterator_t passed into the std::functional lambda to…
Robbie
  • 287
  • 1
  • 9
3
votes
2 answers

Is there any standard functionality for creating a flattened view of a map with a container as the mapped_type?

Is there any standard functionality to create a range/view over all pairs? The following code illustrates the view I am looking to create: std::unordered_map> m{{"Foo", {1,2}}, {"Hello", {4,5}}}; auto view =…
MarkB
  • 672
  • 2
  • 9
3
votes
2 answers

can ranges split be used with a predicate?

Can this double loop be rewritten using ranges views split() ? #include #include struct MyPair { int a; char b; }; vector path = {{1,'a'},{1,'z'},{2,'b'},{2,'y'}}; vector > spans; for (int i=0; i <…
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
3
votes
1 answer

ranges views size does not compile

#include #include #include #include #include using namespace std; int main() { vector ints = {1,2,3,4,5}; auto v = ints | views::take_while([](int i){return i<3;}) ; for (int i :…
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
3
votes
2 answers

Can't make my example using std::ranges::copy_if(R&& r, O result, Pred pred, Proj proj = {}) compile

I've reduced the code failing to compile to the one below: [Demo] #include // copy_if #include // cout #include // back_inserter #include #include #include struct A { std::string…
rturrado
  • 7,699
  • 6
  • 42
  • 62
3
votes
2 answers

how to use a vector of keys to filter values from a map and produce a set using c++ ranges syntax

I am new to Eric Niebler's ranges-v3 library and I would like to solve the following simple problem: I have a std::map containing the following: std::map map = { {"THIS", 1}, {"IS", 2}, {"A", 3}, {"TEST", 4}, {"WITH", 5},…
johnco3
  • 2,401
  • 4
  • 35
  • 67