有没有办法在 C++ 源代码中用空格编写大量数字以使其更具可读性?

2023-09-27C/C++开发问题
8

本文介绍了有没有办法在 C++ 源代码中用空格编写大量数字以使其更具可读性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

假设我有代码:

vector<int> temp = vector<int>(1 000 000 000);

以上不会编译,因为编译器会抱怨空格.是否可以指示 C++ 在编译时省略这些空格,或者以其他方式使数字更易于阅读?

The above will not compile as the compiler will complain about the spaces. Is it possible to indicate to C++ to ommit those spaces when compiling, or otherwise make the number easier to read?

推荐答案

试试数字分隔符:

int i = 1'000'000'000;

此功能是 自 C++14 引入的.它使用单引号 (') 作为数字分隔符.

This feature is introduced since C++14. It uses single quote (') as digit separator.

另见:

  • 为什么没有为 C++14 数字分隔符选择空格字符?
  • 泛化 C++2000 的重载(C++ 之父 4 月的笑话)
  • Why was the space character not chosen for C++14 digit separators?
  • Generalizing Overloading for C++2000 (April's joke by the father of C++ himself)

这篇关于有没有办法在 C++ 源代码中用空格编写大量数字以使其更具可读性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6