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

std::ranges::view as mutable class member

I have a huge data structure and it's content is updated regularly. I need to apply a filter to it each time I get a change request. So to avoid duplicating/copying it, I use an std::view + std::views::filter. (as lightweight container) Having this…
Peter
  • 109
  • 7
0
votes
1 answer

C++20 range get value by index

I want to access the first value from a range: #include #include #include using namespace std; int main() { auto rg = views::split("hello there!", ' ') | views::transform([](auto word)->char{return word[0];}) ; …
Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28
0
votes
1 answer

converting a flat table to a tree structure using ranges-v3

I have a flat representation of a tree shown in the table below. The unsorted data, std::vector is: unsorted vector (id) (path) (fn) (line) (extra) 1 /abc/file3.c foo0 10 1 2 /abc/file3.c foo0 15 2 3…
johnco3
  • 2,401
  • 4
  • 35
  • 67
0
votes
0 answers

Handling lifetime and iterator validity when returning filtered ranges in C++20

I'm working with filter adaptors in C++20. However, I'm facing a problem when I try to return a filtered range from a function. The issue arises because the filter adaptor caches the iterator of the underlying range. When i return from the function,…
0
votes
1 answer

Return range from different sources

I'm wondering how to make same type for return value? Consider an example: auto get_securities_by_mic(const std::string& mic) { auto it = some_map.find(mic); // I want to write like this: if (it == some_map.end()) return…
Alexey Subbota
  • 938
  • 6
  • 23
0
votes
3 answers

Why does the C++20 range concept specify apparently misleading requirements?

I'm struggling to satisfy a C++20 concept for my class. struct C1 { int* begin(); int* end(); }; static_assert(std::ranges::range); // Compiles fine struct C2 {}; namespace std::ranges // (A) { auto begin(C2&); auto…
Frank Heimes
  • 100
  • 6
0
votes
1 answer

Right associativity for `std::views`

Left associativity is already guaranteed by C++ core semantics: R|V1|V2 is parsed as (R|V1)|V2). What if the coder wants to explicitly change the order of operations to R|(V1|V2)? Is this possible under C++23? The reason behind the question is that…
Red.Wave
  • 2,790
  • 11
  • 17
0
votes
1 answer

Are there any versions of clang that have complete implementations of std::ranges?

I'm just trying to figure out how I can use clang++ to compile this: for (int i : std::ranges::iota_view{1, 10}) std::cout << i << ' '; The latest Apple Clang failed to recognize iota_view, and so I downloaded LLVM 15.0.7 using Homebrew.…
Oreese17
  • 7
  • 1
0
votes
1 answer

Transpose vector of vectors with views::zip

Duplicate: https://stackoverflow.com/a/61819663/11998382 This should be possible if we know the size of the outer vector at compile time. You just have to write the c++ variadic-template equivalent of the following python code: def…
Tom Huntington
  • 2,260
  • 10
  • 20
0
votes
2 answers

Pass initializer lists and ranges seamlessly to a function parameter

I am trying to create a function that will initialize an internal std::set, and I want to expose an API that allows any type of ranges as input, including initializer lists. So what I'm aiming for is : // API definition class…
0
votes
1 answer

Workaround to decrement iterator from views::transform?

Consider the following example: int main() { std::string_view foo = "This is a test"; auto split_foo = foo | std::views::split(' ') | std::ranges::views::transform( …
Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32
0
votes
1 answer

Correct syntax for views::istream or ranges::istream_view?

I am trying to get into ranges::views. The following demo program does not compile: #include #include #include #include #include #include namespace rng = std::ranges; struct…
A M
  • 14,694
  • 5
  • 19
  • 44
0
votes
1 answer

Binary search over function results in C++

I am looking for a way to do a binary search over function results with a signature close to the following: template
0
votes
1 answer

How to create a view that will split the data in half

I want to create a view that will have views of both halves of the string. I added some code examples of what I would like to achieve. How could I do it? #include #include #include #include #include…
Dessus
  • 301
  • 2
  • 10
0
votes
1 answer

pipe istream_view and views::transform. How?

Sorry, I was searching a lot, but I still do not understand. Let's assume that I want to read line by line with an istream_view using a line proxy and convert the result to lowercase. It would be nice, if I could write #include #include…
A M
  • 14,694
  • 5
  • 19
  • 44