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
1
vote
3 answers

C++20: Why can't range adaptors and ranges be combined in one expression?

I am looking at the following snippet: std::vector elements{ 1,2,3 }; // this won't compile: elements | std::views::filter([](auto i) { return i % 2 == 0; }) | std::ranges::for_each([](auto e) {std::cout << e << std::endl; }); // but…
Angle.Bracket
  • 1,438
  • 13
  • 29
1
vote
1 answer

Read file and split and trim each line with cpp20

I have created a code that fits my specific need - to split the string (read from a file) at comma stripping any whitespaces. also, I want to convert these substrings to double and store them in std::vector. I use ranges library in c++20 and…
AmirSalar
  • 325
  • 2
  • 14
1
vote
1 answer

Standard algorithm to operate on adjacent elements

std::adjacent_find looks for the first two consecutive elements that satisfies the given predicate. I am looking for other algorithms that also has a predicate that takes the (previous, current) pair. This is useful for rendering polygons, computing…
user877329
  • 6,717
  • 8
  • 46
  • 88
1
vote
1 answer

Error with C++20 ranges and std::views::take

I was experimenting with C++20 ranges and I got the following strange behavior when compiling with GCC 11.1.0 and CMake 3.20.3. Specifically, the following code doesn't compile: auto Foo() { std::vector x{1, 2, 3, 4, 5, 6}; return…
fdev
  • 127
  • 12
1
vote
1 answer

C++20: Concept function, restricted with an archtype takes wider range of inputs, than desired

Given concept test which has a function that takes an input range. template concept test = requires(T t, archtypes::Input_Range t_range) { { t.count(t_range) } -> std::same_as; }; This archtype allows for the count function to…
freeman23
  • 11
  • 1
1
vote
2 answers

What is the point of c++20 ranges?

I struggle to understand what c++20 ranges add compared to good old fashioned iterators. Yes, I guess there is no need to use begin and end anymore, but simple overloads such as: namespace std { template auto…
SomeProgrammer
  • 1,134
  • 1
  • 6
  • 12
1
vote
4 answers

Is it possible to create a view of references from objects?

To illustrate what I mean: I have three objects: Foo first, even, odd; And I want to construct a view consisting of references to these objects like this: first&, odd&, even&, odd& ... up to N. To be able to iterate over them: for (const auto &…
cos
  • 940
  • 1
  • 10
  • 25
1
vote
1 answer

Why can't I construct a string_view from range iterators?

C++20 adds a constructor for basic_string_view which takes two iterators. However, when I try to construct a string_view with the iterators from a common range I get an error. #include #include using namespace std::views; int…
Chris_F
  • 4,991
  • 5
  • 33
  • 63
1
vote
1 answer

error: no match for call to '(const std::ranges::__sort_fn)

I was practicing vectors and ranges in c++ 20 was stuck at following state. #include #include #include #include #include namespace ranges = std::ranges; struct Model { double…
Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38
1
vote
0 answers

How can I rotate a particular subset of a vector?

I want to rotate a subset of an vector inside the whole vector. The subset to be rotated is defined by another vector. What I want to achieve is this: template void rotateSubset(CONTAINER& whole, const CONTAINER& subset) { //…
Liam Goodacre
  • 381
  • 1
  • 10
1
vote
3 answers

Can / should I use the ranges library to emulate set functions?

I want to apply a view::filter to a vector in order to find the set_intersection (or set_difference, etc) of it with another vector. My interest in doing this is that it would allow you to alter a particular subset of the original container (and…
Liam Goodacre
  • 381
  • 1
  • 10
1
vote
3 answers

C++ std:: string starts_with/ends_with case insensitive versions?

C++20 added starts_with, ends_with to std::string. Is there a nice way to get it to be case insensitive? Note that perf matters so I do not want to lowercase/uppercase both strings(or std::min(len1, len2) parts of them). Unlike regular
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
2 answers

Is the code for sorting a std::vector by length correct in C++20?

I wrote a code that sorts vector strings by length. Unfortunately, I'm not sure whether it will work in the next standard in this form. Is this the correct code in C++20? #include #include #include #include
Dessus
  • 301
  • 2
  • 10
0
votes
0 answers

Added and removed elements between two data structures of type std::unordered_map>

What's the most efficient way to retrieve the added and removed elements between two structure of type: using T = unordered_map>; I wrote this code. Could you please advise? #include #include…
Peter
  • 109
  • 7
0
votes
0 answers

what is faster, std::sort then set_intersection, or std::unordered_map with double for-loop find?

what is a faster? sort two vectors and then do set_intersection... or auto intersection = std::make_shared(); std::sort(m_currentSearchResults->begin(), m_currentSearchResults->end()); std::sort(graphSearchResultMediaItems->begin(),…
chrisg
  • 200
  • 8