c++ 20为什么可以“ tipe”变成视图::逆转,就像其他视图功能一样吗?
我正在测试一些精美的新C ++功能,其中之一是范围和相关视图。我发现您可以链接一个容器要做的事情特别有趣。
您可以将二进制操作员|()
用于链件东西,这真的很好。我注意到您可以将std :: views :: take(int)
,std :: views :: vistion :: transform()
用lambda或功能指针,std :: views :: filter()
带有lambda或功能指针。您唯一无法使用的是std :: view ::反向。只能通过将范围放入参数来使用。为什么那样?还有其他选择吗?
#include <iostream>
#include <vector>
#include <string>
#include <ranges>
int main()
{
using std::string;
using std::cout, std::endl;
const std::vector<string> vec = { "A", "B", "a", "b", "b2", "a2", "a3", "b3", "b4" };
for (auto const& val : std::views::reverse(vec) // ok
| std::views::take_while([](const string& s) {return s.size() > 0 && s[0] == 'b'; })
// | std::views::reverse() // Error?!
)
{
cout << val << endl;
}
}
用g ++ -std = C ++ 20 main.cpp -o main
在这种情况下,我将如何获得b3
,b4
使用上面的语法?
I am testing some of the fancy new c++ features, one of which is ranges and the associated views. I find it particularly interesting that you can chain what you wish to do with a container.
You can use the binary operator|()
to chain things, which is really nice. I noticed that you can chain into std::views::take(int)
, std::views::transform()
with a lambda or function pointer, std::views::filter()
with a lambda or function pointer. The only thing you can't use is std::views::reverse. That one can only be used by putting a range into it's argument. Why it that? Is there an alternative?
#include <iostream>
#include <vector>
#include <string>
#include <ranges>
int main()
{
using std::string;
using std::cout, std::endl;
const std::vector<string> vec = { "A", "B", "a", "b", "b2", "a2", "a3", "b3", "b4" };
for (auto const& val : std::views::reverse(vec) // ok
| std::views::take_while([](const string& s) {return s.size() > 0 && s[0] == 'b'; })
// | std::views::reverse() // Error?!
)
{
cout << val << endl;
}
}
Compile with g++ -std=c++20 main.cpp -o main
How would I get b3
, b4
in that situation using the syntax above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将管道输送到单词的适配器的语法不是:
在这种情况下,这
不是括号。
std :: view ::反向(a)
可以写为a | std :: view ::反向
也要开始。类似于所有其他单一词汇范围适配器(
join
,键
,values
, elements&lt; n&gt; , ETC。)。The syntax for piping into single-argument adaptors isn't:
It's
No parentheses in this case.
std::views::reverse(a)
could be written asa | std::views::reverse
to start with as well.Similar for all the other single-argument range adaptors (
join
,keys
,values
,elements<N>
, etc.).