📜  C ++ 14中的数字分隔符

📅  最后修改于: 2021-05-31 21:13:34             🧑  作者: Mango

在本文中,我们将讨论C++中数字分隔符的使用。有时,很难读取包含许多数字的数字。例如, 1000是可读的,但是如果向其中添加更多的零,比如说1000000 ,现在将变得有点难以阅读,而如果向其添加更多的零,将会发生什么。在现实生活中,逗号(,)被添加到数字中。例如: 10,00,000 。现在很容易读,就是一十万。

现在出现的问题是C++不接受这样的分隔符(逗号),因此如何处理大数。为了解决这个问题, C++ 14引入了一项功能,其名称为 数字分隔符,用简单的引号(’)表示。这可以使用户更容易阅读大量数字。

程序1:

以下是显示单引号值时将其忽略的实现:

C++14
// C++ program to demonstrate
// the above approach
#include 
using namespace std;
 
// Driver code
int main()
{
    long long int a = 10'00'000;
 
    // Print the value
    cout << a;
 
    return 0;
}


C++14
// C++ program to demonstrate
// the above approach
#include 
using namespace std;
 
// Driver Code
int main()
{
    long long int a = 1'23'456;
    long long int b = 12'34'56;
    long long int c = 123'456;
  
    // Print all the value
    cout << "a:" << a << endl;
    cout << "b:" << b << endl;
    cout << "c:" << c << endl;
 
    return 0;
}


输出
1000000

程式2:

下面是显示单引号仅用于用户的程序。在任何位置使用它们都不会影响编译器。

C++ 14

// C++ program to demonstrate
// the above approach
#include 
using namespace std;
 
// Driver Code
int main()
{
    long long int a = 1'23'456;
    long long int b = 12'34'56;
    long long int c = 123'456;
  
    // Print all the value
    cout << "a:" << a << endl;
    cout << "b:" << b << endl;
    cout << "c:" << c << endl;
 
    return 0;
}
输出
a:123456
b:123456
c:123456
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”