c++ 20为什么可以“ tipe”变成视图::逆转,就像其他视图功能一样吗?

发布于 2025-02-09 23:21:44 字数 1065 浏览 3 评论 0原文

我正在测试一些精美的新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 在这种情况下,我将如何获得b3b4使用上面的语法?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2025-02-16 23:21:45

将管道输送到单词的适配器的语法不是:

| views::reverse()

在这种情况下,这

| views::reverse

不是括号。 std :: view ::反向(a)可以写为a | std :: view ::反向也要开始。

类似于所有其他单一词汇范围适配器(joinvalues, elements&lt; n&gt; , ETC。)。

The syntax for piping into single-argument adaptors isn't:

| views::reverse()

It's

| views::reverse

No parentheses in this case. std::views::reverse(a) could be written as a | std::views::reverse to start with as well.

Similar for all the other single-argument range adaptors (join, keys, values, elements<N>, etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文