English 中文(简体)
C++20中将多个区域适应器连接成一个单一区域
原标题:Concatenate multiple range adaptors into one single ranges in C++20
Consider the following case: std::vector v{0, 1, 2, 3, 4, 5}; // 0 1 2 3 4 5 auto rng1 = std::views::all(v); // 5 4 3 2 1 0 auto rng2 = std::views::reverse(v); // 4 2 0 auto rng3 = std::views::filter(rng2, [](int x){return x % 2 == 0;}); Is there a elegant way to concatenate those three adaptors into one single view like this: // 0 1 2 3 4 5 5 4 3 2 1 0 4 2 0 auto final_rng = std::views::concat(rng1, rng2, rng3); It seems impossible since rng1, rng2, and rng3 s are very different types. Can someone give an alternative solution? thanks. Upate: C++26 finally introduced views::concat, so the example in my question will work perfectly in C++26 (Demo).
最佳回答
Yes, what you wrote just in a different namespace - there s no concat in the Standard Library but there is one in range-v3: auto final_rng = ranges::views::concat(rng1, rng2, rng3); The fact that the ranges are different types doesn t pose a problem. You just have an iterator that s built up of a variant of the underlying iterators of the ranges. The important part is that the value type of the ranges is the same - and here it is, so that s totally fine.
问题回答
There is actually a concat() in the standard coming with C++26. Check out the spec: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2542r7.html As of today (June 2024), it is implemented in g++-15.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签