首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
gV0拜客生活常识网

a. std::integral – specifies the type should be an integral typegV0拜客生活常识网

b. std::boolean – specifies the type can be used as a boolean typegV0拜客生活常识网

c. std::move_constructible – specifies that the object of a particular type can be constructed using the move semanticsgV0拜客生活常识网

d. std::movable – specifies that the object of a certain type T can be movedgV0拜客生活常识网

e. std::signed_integral – says the type is both integral and is a signed integralgV0拜客生活常识网

20.3 Lambda Templates

We can now use template syntax in our lambda functions . Example:gV0拜客生活常识网

auto mylambda = [](T param)
{
    // code
};

For example, to printout the generic type name, using a templated lambda expression, we would write:gV0拜客生活常识网

#include 
#include 
#include 
int main()
{
    auto mylambda = [](T param)
    {
        std::cout << typeid(T).name() << '
';
    };
    std::vector v = { 1, 2, 3, 4, 5 };
    mylambda(v); // integer
    std::vector v2 = { 3.14, 123.456, 7.13 };
    mylambda(v2); // double
}

20.4 [likely] and [unlikely] Attributes

If we know that some paths of execution are more likely to be executed than others, we can help the compiler optimize the code by placing attributes. We use the [[likely]] attribute before the statement that is more likely to be executed. We can also put the [[unlikely]] attribute before the statement that is unlikely to be executed. For example, the attributes can be used on case branches inside the switch statement:gV0拜客生活常识网

#include 
void mychoice(int i)
{
    switch (i)
    {
    [[likely]] case 1:
        std::cout << "Likely to be executed.";
        break;
    [[unlikely]] case 2:
        std::cout << "Unlikely to be executed.";
        break;
    default:
        break;
    }
}
int main()
{
    mychoice(1);
}

If we want to use these attributes on the if-else branches, we write:gV0拜客生活常识网

#include 
int main()
{
    bool choice = true;
    if (choice) [[likely]]
    {
        std::cout << "This statement is likely to be executed.";
    }
    else [[unlikely]]
    {
        std::cout << "This statement is unlikely to be executed.";
    }
}

20.5 Ranges

A range, in general, is an object that refers to a range of elements. The new C++20 ranges feature is declared inside a header. The ranges themselves are accessed via the std::ranges name. With classic containers such as an std::vector, if we want to sort the data, we would use:gV0拜客生活常识网

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

The std::sort function accepts vector’s .begin() and end() iterators. With ranges, it is much simpler, we just provide the name of the range, without iterators:gV0拜客生活常识网

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

Ranges have a feature called adaptors. One of the range adaptors is views. The views adaptors are accessed via std::ranges::views. Views are not owning. They cannot change the values of the underlying elements. It is also said they are lazily executed. This means the code from the views adaptors will not be executed until we iterate over the result of such views.gV0拜客生活常识网

Let us create an example which uses range views

相关阅读:

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