有什么区别 - const int x = 5000;和 const int x = 50'00;在 C++ 中?

What is the difference - const int x = 5000; and const int x = 50'00; in C++?

下面的c++代码有什么区别-

const int x = 5000;
const int x = 50'00;

50'00 使用 C++14 中添加的 digit separator。该数字将解析为 5000,唯一的区别是它可能更易于阅读。通常您会使用数字分隔符来分隔千位,例如 1'000'000,但您可以在数字中的任何位置使用它。

从 C++14 开始,您可以在整数文字中放置 数字分隔符 ' 任意位置(除了从开头或结尾或在基本指示符之后不包括八进制前导零 - 否则你会与 多字符文字 .)

发生冲突

目的是使代码更具可读性:例如

const int x = 5'000;

或者,使用印度符号(解释了 anywhere 的基本原理)

const auto x = 12'34'567;

或者,十六进制

const auto x = 0xee'ef;

请注意 type 之类的 1e3double,它会咬住你,尤其是在 for 中使用时] 循环作为计数器。

参考:https://en.cppreference.com/w/cpp/language/integer_literal