类型 变量名 { }; 或者 类型 变量名 = { };

使用List Initialization (列表初始化)的好处,高精度变低精度,会提示错误。

    int x1{ 1.123 };//提示错误
    //int x1 = 1.123;//不提示错误,x1=1

常见类型写法如下:

    int x{};//int x=0;
    int y{ 10 };
    char c{ 'a' };
    int arr[]{1, 2, 3};//int arr[]={1,2,3};
    char s[]{"hello"};

强制类型转换:static_cast<type> (value)

    cout << static_cast<int> (2.5);
    cout << static_cast<double>(2);

 

相关文章:

  • 2021-12-23
  • 2022-01-04
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2021-04-15
  • 2021-11-12
  • 2021-12-16
猜你喜欢
  • 2021-06-21
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
  • 2021-04-27
相关资源
相似解决方案