std-ranges Questions

2

Solved

I'm trying to learn C++ ranges. As far as I can see, the simplest (and the only, short of implementing a custom range view class) way to create a range view object that generates a custom sequence ...
Angers asked 30/9 at 16:44

1

I am testing ranges from C++ 20 , and this is my main.cpp: #include <ranges> #include <iostream> int main() { auto const ints = {0,1,2,3,4,5}; auto even = [](int i) { return 0 == i %...
Tenebrous asked 26/5, 2021 at 8:56

3

Solved

I was working on a simple parser using std::ranges. I was trying to parse ints in a string until all were converted or one failed by doing something like: try_parse_ints(str) | take_while(is valid ...
Aney asked 28/8 at 17:57

3

cppreference states: The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone. ...
Dittany asked 10/3, 2023 at 10:21

3

Solved

In C++20, there are two swap function templates: std::ranges::swap(T&&, U&&) and std::swap(T&, T&). I just wonder: What's the difference between they two?
Markson asked 25/7, 2021 at 7:42

2

Solved

The following code (godbolt): #include <iostream> #include <vector> #include <ranges> struct S { float x = 150.f; S f() const { return *this; } }; int main() { std::vect...
Behlau asked 16/6 at 22:0

3

Solved

Consider the following scenario: struct MyInterface {}; struct Data : MyInterface{}; struct DataWrapper { SomeContainerOf<MyInterface*> getData() {...} private: std::vector<Data...
Streptothricin asked 1/6 at 20:58

1

Solved

auto is really nice to maintain a single definition and avoid some typing. It's also necessary for std::ranges::views. However, auto can make code harder to read it's too far from the definin...
Serviceman asked 15/5 at 1:39

2

Solved

I'm dealing with the last big 4 of C++ 20, attempting to learn the new main features. Trying some code from the web related to ranges, I've written: std::vector ints{ 6, 5, 2, 8 }; auto even = [](i...
Prejudice asked 28/9, 2020 at 12:54

2

Solved

I want to loop through a vector in a sorted way without modifying the underlying vector. Can std::views and/or std::range be used for this purpose? I've successfully implemented filtering using vie...
Irrepressible asked 4/3, 2022 at 8:41

2

The std::ranges::view concept in C++23 requires a view to be movable, which includes move-assignability. I understand why we want a view to be move-constructible, but why is the assignment necessar...

4

Solved

Is there any better way to drop last element in container using c++20 ranges than reverse it twice? #include <iostream> #include <vector> #include <ranges> int main() { std::vec...
Resultant asked 31/3, 2022 at 8:15

1

Solved

I recently discovered the ranges stdandard library and encountered a strange behavior. When chaining multiple range adaptors, I can't chain after using std::ranges::views::join: vec | std::ranges::...
Berar asked 9/3 at 15:5

2

Much of the functionality provided by the various range adaptors seem to be quite similar to the algorithms library. For example std::ranges::transform_view A range adaptor that represents view of...
Finalism asked 12/7, 2022 at 6:51

3

Solved

How can I combine the following two functions into one? Is there something similar to std::forward, but for ranges? #include <ranges> #include <vector> #include <algorithm> templ...
Gregggreggory asked 22/1 at 5:49

1

Solved

I have a C-string (const char *), and I want to convert it to a C++20 range (a forward_range) to apply some standard algorithms to it. I don't want to use std::string_view because I don't want to c...
Tench asked 21/1 at 5:46

3

Solved

How can you convert to a custom data structure using std::ranges::to? Basically something like struct my_data { char b; int foo; std::string baz; }; auto text = "v 3 fool|x 56 description|...
Kempe asked 18/12, 2023 at 14:30

1

Solved

Consider the following example (Godbolt): #include <vector> #include <iostream> #include <ranges> #include <algorithm> struct A { A() {} A( const A& ) { std::cout &lt...
Avaricious asked 2/11, 2023 at 11:39

0

I recently noticed that the following code doesn't work: #include <ranges> #include <algorithm> #include <memory> #include <iostream> int main() { std::cout << *std:...
Deterge asked 17/10, 2023 at 16:43

1

Solved

I wanted to view a 3x3 grid column by column, so I figured I would use std::views::stride like so: #include <array> #include <iostream> #include <ranges> auto main() -> int { ...
Papyrology asked 11/10, 2023 at 17:2

1

Solved

Consider this code: #include <vector> #include <iostream> #include <cstdint> #include <ranges> int main() { struct S { int a; int b; bool operator==(int other) const { ...
Psychotechnology asked 5/10, 2023 at 12:28

3

Solved

The legacy std::for_each returns function as the standard only requires Function to meet Cpp17MoveConstructible according to [alg.foreach]: template<class InputIterator, class Function> con...
Ascension asked 28/9, 2023 at 11:59

2

Solved

I was writing a simple C++ program that generates a list of random integer values from a normal distribution, then takes first N generated items and filters them so that their absolute value is gre...
Sacks asked 27/8, 2023 at 10:40

5

Solved

I find the traditional syntax of most c++ stl algorithms annoying; that using them is lengthy to write is only a small issue, but that they always need to operate on existing objects limits t...
Adamina asked 1/7, 2022 at 10:16

3

Solved

I want to split a string by a token with std::views::split, and for each retrieved substring invoke the std::from_chars function. Here is an MRE (https://godbolt.org/z/1K71qo9s4) that compiles succ...
Glendaglenden asked 26/7, 2023 at 10:36

© 2022 - 2024 — McMap. All rights reserved.