首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
to filter-out even numbers and print only the odd numbers from a vector by creating a range view:gV0拜客生活常识网

#include 
#include 
#include 
#include 
int main()
{
    std::vector v = { 1, 2, 3, 4, 5 };
    auto oddnumbersview = v | std::views::filter([](int x) { return x % 2 == 1; });
    for (auto el : oddnumbersview)
    {
        std::cout << el << '
';
    }
}

Explanation: we have a simple vector with some elements. Then we create a view range adaptor on that vector, which filters the numbers in the range. For this, we use the pipe operator |. Only the numbers for which the predicate is true are included. In our case, this means the even numbers are excluded. Then we iterate over the filtered view and print out the elements.gV0拜客生活常识网

Important to note, the underlying vector’s elements are unaffected as we are operating on a view, not on a vector.gV0拜客生活常识网

Let us create an example which creates a view that returns only numbers greater than 2:gV0拜客生活常识网

#include 
#include 
#include 
#include 
int main()
{
    std::vector v = { 1, 2, 3, 4, 5 };
    auto greaterthan2view = v | std::views::filter([](int x) { return x > 2; });
    for (auto el : greaterthan2view)
    {
        std::cout << el << '
';
    }
}

Now, let us combine the two views into one big view by separating them with multiple pipe | operators:gV0拜客生活常识网

#include 
#include 
#include 
#include 
int main()
{
    std::vector v = { 1, 2, 3, 4, 5 };
    auto oddandgreaterthan2 = v | std::views::filter([](int x) { return x % 2 == 1; })
                                | std::views::filter([](int x) { return x > 2; });
    for (auto el : oddandgreaterthan2)
    {
        std::cout << el << '
';
    }
}

This example creates a view range adaptor containing odd numbers greater than two. We create this view by combining two different range views into one.gV0拜客生活常识网

Another ranges adaptors are algorithms. The idea is to have the algorithms overload for ranges. To call an algorithm adaptor we use: std::ranges::algorithm_name(parameters). Example using the std::ranges::reverse() algorithm:gV0拜客生活常识网

#include 
#include 
#include 
#include 
int main()
{
    std::vector v = { 1, 2, 3, 4, 5 };
    std::ranges::reverse(v);
    for (auto el : v)
    {
        std::cout << el << '
';
    }
}

Unlike views, the ranges algorithms modify the actual vector content.gV0拜客生活常识网

40.4.6 Coroutines

A coroutine is a function that can be suspended and be resumed. The ordinary function is a coroutine if it uses any of the following operators in its function body:gV0拜客生活常识网

a. co_await – suspends the execution of the coroutine until some other computation is performed, that is until the coroutine itself resumesgV0拜客生活常识网

b. co_yield – suspends a coroutine and return a value to the callergV0拜客生活常识网

c. co_return – returns from a coroutine and stops its executiongV0拜客生活常识网

20.7 std::span

std::string_view定义的是特定的类型(string),std::span是与std:string_view相同概念的全部类型的应用。gV0拜客生活常识网

Some containers and types store their elements in a sequence, one next to the other. This is the case for arrays and vectors. We can represent such containers

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。