首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
To create a string_view for a substring of the first five characters, we use the different constructor overload. This string_view constructor takes a pointer to the first string element and the length of the substring:gV0拜客生活常识网

#include 
#include 
#include 
int main()
{
    std::string s = "Hello World.";
    std::string_view sw(s.c_str() , 5);
    std::cout << sw;
}

Once we create a string_view, we can use its member functions. To create a substring out of a string_view, we use the .substr() member function. To create a substring, we supply the starting position index and length. To create a substring of the first five characters, we use:gV0拜客生活常识网

#include 
#include 
#include 
int main()
{
    std::string s = "Hello World";
    std::string_view sw(s);
    std::cout << sw.substr(0, 5);
}

A string_view allows us to parse (not change) the data that is already in the memory, without having to make copies of the data. This data is owned by another string or character array object.gV0拜客生活常识网

17.6 std::any

The std::any container can hold a single value of any type. This container is declared inside the header file. Example:gV0拜客生活常识网

可以理解为一种特殊的void*(void*在解引用前也需要类型显式转换为具体特定的类型)。gV0拜客生活常识网

#include 
int main()
{
    std::any a = 345.678;
    std::any b = true;
    std::any c = 123;
}

To access the value of an std::any object in a safe manner, we cast it to a type of our choice using the std::any_cast function:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::any a = 123;
    std::cout << "Any accessed as an integer: " << std::any_cast(a) << '
';
    a = 456.789;
    std::cout << "Any accessed as a double: " << std::any_cast(a) << '
';
    a = true;
    std::cout << "Any accessed as a boolean: " << std::any_cast(a) << '
';
}

Important, the std::any_cast will throw an exception if we try to convert, for example, 123 to type double. This function performs only the type-safe conversions.Another std::any member function is .has_value() which checks if the std::any object holds a value:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::any a = 123;
    if (a.has_value())
    {
        std::cout << "Object a contains a value." << '
';
    }
    std::any b{};
    if (b.has_value())
    {
        std::cout << "Object b contains a value." << '
';
    }
    else
    {
        std::cout << "Object b does not contain a value." << '
';
    }
}

17.7 std::variant

There is another type of data in C++ called union. A union is a type whose data members of different types occupy the same memory. Only one data member can be accessed at a time. The size of a union in memory is the size of its largest data member. The data members overlap in a sense. To define a union type in C++, we write:gV0拜客生活常识网

union MyUnion
{
    char c;        // one byte
    int x;         // four bytes
    double d;      // eight bytes
};

Here we declared a union type that can hold characters or integers or doubles. The size of this union is the size of its largest data member double, which is probably eight bytes, depending on the implementation. Although the union declares multiple data members, it can only hold a value of one member at any given time. This is because all the data members share the same memory location. And we can only access the member that was the last written-to. Example:gV0拜客生活常识网

#include 
union MyUnion
{
    char c;        // one byte
    int x;         // four bytes
    double d;      // eight bytes
};
int main()
{
    MyUnion o;
    o.c = 'A';
    std::cout << o.c << '
';
    // accessing o.x or o.d is undefined behavior at this point
    o.x = 123;
    std::cout << o.c;
    // accessing o.c or o.d is undefined behavior at this point
    o.d = 456.789;
    std::cout     

相关阅读:

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