首页 > 社交 > 科普中国

C++

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

#include 
#include 
#include 
using MyInt = int;
using MyString = std::string;
using MyVector = std::vector;
int main()
{
    MyInt x = 123;
    MyString s = "Hello World";
    MyVector v = { 1, 2, 3, 4, 5 };
}

2 C++14

C++14 is an ISO C++ standard published in 2014. It brings some additions to the language and the standard library, but mainly complements and fixes the C++11 standard. When we say we want to use the C++11 standard, what we actually want is the C++14 standard. Below are some of the new features for C++14.gV0拜客生活常识网

To compile for C++14, add the -std=c++14 flag to a command-line compilation string if using g++ or clang compiler. In Visual Studio, choose Project / Options / Configuration Properties / C/C++ / Language / C++ Language Standard and choose C++14.gV0拜客生活常识网

14.1 Binary Literals

Values are represented by literals. So far, we have mentioned three different kinds of binary literals: decimal, hexadecimal, and octal as in the example below:gV0拜客生活常识网

int main()
{
    int x = 10;
    int y = 0xA;
    int z = 012;
}

These three variables have the same value of 10, represented by different number literals. C++14 standard introduces the fourth kind of integral literals called binary literals. Using binary literals, we can represent the value in its binary form. The literal has a 0b prefix, followed by a sequence of ones and zeros representing a value. To represent the number 10 as a binary literal, we write:gV0拜客生活常识网

int main()
{
    int x = 0b101010;
}

The famous number 42 in binary form would be:gV0拜客生活常识网

int main()
{
    int x = 0b1010;
}

Important to rememberValues are values; they are some sequence of bits and bytes in memory. What can be different is the value representation. There are decimal, hexadecimal, octal, and binary representations of the value. These different forms of the same thing can be relevant to us humans. To a machine, it is all bits and bytes, transistors, and electrical current.gV0拜客生活常识网

14.2 Digits Separators

In C++14, we can separate digits with a single quote to make it more readable:gV0拜客生活常识网

int main()
{
    int x =100'000'000;
}

The compiler ignores the quotes. The separators are only here for our benefit, for example, to split a large number into more readable sections.gV0拜客生活常识网

14.3 Auto for Functions

We can deduce the function type based on the return statement value:gV0拜客生活常识网

auto myintfn() // integer
{
    return 123;
}
auto mydoublefn() // double
{
    return 3.14;
}
int main()
{
    auto x = myintfn(); // int
    auto d = mydoublefn(); // double
}

14.4 Generic Lambdas

We can use auto parameters in lambda functions now. The type of the parameter will be deduced from the value supplied to a lambda function. This is also called a generic lambda :gV0拜客生活常识网

#include 
int main()
{
    auto mylambda = [](auto p) {std::cout << "Lambda parameter: " << p << '
'; };
    mylambda(123);
    mylambda(3.14);
}

14.5 std::make_unique

C++14 introduces a std::make_unique function for creating unique pointers. It is declared inside a header. Prefer this function to raw new operator when creating unique pointers:

相关阅读:

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