首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型


gV0拜客生活常识网

20. 1 ModulesgV0拜客生活常识网

export and importgV0拜客生活常识网

20. 2 ConceptsgV0拜客生活常识网

template type requiresgV0拜客生活常识网

20. 3 Lambda TemplatesgV0拜客生活常识网

[]<>(){}gV0拜客生活常识网

20. 4 [likely] and [unlikely] AttributesgV0拜客生活常识网

if (choice) [[likely]]gV0拜客生活常识网

20. 5 RangesgV0拜客生活常识网

ranges::sort(vec);gV0拜客生活常识网

20. 6 CoroutinesgV0拜客生活常识网

co_awaitgV0拜客生活常识网

20. 7 std: : spangV0拜客生活常识网

span is = vec;gV0拜客生活常识网

20. 8 Mathematical ConstantsgV0拜客生活常识网

numbers::log2egV0拜客生活常识网

1 C++11

C++11 is an ISO C++ standard, published in 2011. To compile for this standard, add the -std=c++11 flag to a command-line compilation string if compiling with g++ or clang. If using Visual Studio, choose Project / Options / Configuration Properties / C/C++ / Language / C++ Language Standard and choose C++11. New Visual Studio versions already support this standard out of the box.gV0拜客生活常识网

11.1 Automatic Type Deduction

This standard introduces the auto keyword which deduces the type of the variable based on the variable’s initializer:gV0拜客生活常识网

int main()
{
    auto mychar = 'A';
    auto myint = 123 + 456;
    auto mydouble = 456.789;
}

11.2 Range-based Loops

The range-based loops allow us to iterate over the range, such as C++ standard-library containers:gV0拜客生活常识网

#include 
#include 
int main()
{
    std::vector v = { 10, 20, 40, 5, -20, 75 };
    for (auto el : v)
    {
        std::cout << el << '
';
    }
}

The range-based for loop is of the following form: for (type element : container). This is read as for each element in a container (do something).gV0拜客生活常识网

11.3 Initializer Lists

Initializer lists, represented by braces { } allow us to initialize objects in a uniform way. We can initialize single objects:gV0拜客生活常识网

int main()
{
    int x{ 123 };
    int y = { 456 };
    double d{ 3.14 };
}

And containers:gV0拜客生活常识网

#include 
int main()
{
    std::vector v = { 1, 2, 3, 4, 5 };
}

List initialization also prevents narrowing conversions. If we tried to initialize our integer object with a double value inside the initializer list, the compilation would fail:gV0拜客生活常识网

int main()
{
    int x = { 123.45 }; // Error, does not allowing narrowing
}

When initializing our objects, we should prefer initializer lists {} to old-style parentheses ().gV0拜客生活常识网

11.4 Move Semantics

C++ 11 standard introduces the move semantics for classes. We can initialize our objects by moving the data from other objects. This is achieved through move constructors and move assignment operators. Both accept the so-called rvalue reference as an argument. Lvalue is an expression that can be used on the left-hand side of the assignment operation. rvalues are expressions that can be used on the right-hand side of an assignment. The rvalue reference has the signature of some_type&&. To cast an expression to an rvalue reference, we use the std::move function. A simple move constructor and move assignment signature are:gV0拜客生活常识网

class MyClass
{
public:
    MyClass(MyClass&& otherobject) // move constructor
    {
        //implement the move logic here
    }
    MyClass& operator=(MyClass&& otherobject) // move assignment operator
    {
        // implement the copy logic here
        return *this;
    }
};    

相关阅读:

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