首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
with a pointer to their first element plus the length of the container. A std::span class template from a header is just that. A reference to a span of contiguous container elements. One reason to use the std::span, is that it is cheap to construct and copy. Span does not own a vector or an array it references. However, it can change the value of the elements. To create a span from a vector we use:gV0拜客生活常识网

#include 
#include 
#include 
int main()
{
    std::vector v = { 1, 2, 3 };
    std::span myintspan = v;
    myintspan[2] = 256;
    for (auto el : v)
    {
        std::cout << el << '
';
    }
}

Here, we created a span that references vector elements. Then we used the span to change the vector’s third element. With span, we do not have to worry about passing a pointer and a length around, and we just use the neat syntax of a span wrapper. Since the size of the vector can change, we say our span has a dynamic extent . We can create a fixed-size span from a fixed-sized array. We say our span now has a static extent. Example:gV0拜客生活常识网

#include 
#include 
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    std::span myintspan = arr;
    myintspan[4] = 10;
    for (auto el : arr)
    {
        std::cout << el << '
';
    }
}

20.8 Mathematical Constants

C++20 standard introduces a way to represent some of the mathematical constants. To use them, we need to include the header. The constants themselves are inside the std::numbers namespace. The following example shows how to use numbers pi and e, results of logarithmic functions and square roots of numbers 2 and 3:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::cout << "Pi: " << std::numbers::pi << '
';
    std::cout << "e: " << std::numbers::e << '
';
    std::cout << "log2(e): " << std::numbers::log2e << '
';
    std::cout << "log10(e): " << std::numbers::log10e << '
';
    std::cout << "ln(2): " << std::numbers::ln2 << '
';
    std::cout << "ln(10): " << std::numbers::ln10 << '
';
    std::cout << "sqrt(2): " << std::numbers::sqrt2 << '
';
    std::cout << "sqrt(3): " << std::numbers::sqrt3 << '
';
}

ref:gV0拜客生活常识网

Slobodan Dmitrović 《Modern C++ for Absolute Beginners》gV0拜客生活常识网

-End-gV0拜客生活常识网

相关阅读:

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