这些功能不需要包含任何头文件即可使用它们。因此,它提供了更快的用法,因为它们是GCC编译器的内置函数,即使在竞争性编程中,它们也是最常用的编译器。
注意:如果编译器显示溢出警告,请在__builtin_inf()函数之前使用强制类型转换,如下所示:
(data_type)__builtin_inf()
- __builtin_inf(void) :此函数返回正无穷大,然后将其转换为C语言limit.h头文件Macro的DBL_MAX。其返回数据类型为double。
例子:if __builtin_inf() is used it returns infinte Output: inf
这是一个C++程序,显示了此函数的用法:
#include
using namespace std; int main() { // infinite value (inf) double double_inf = __builtin_inf(); cout << double_inf << endl; return 0; } 输出:inf
- __builtin_infd32(void) :这些函数返回4字节整数的最大值,并且返回数据类型为32位整数。
例子:
if __builtin_infd32() is used it returns the maximum value of the 32-bit integer Output: 2147483647
这是一个C++程序,显示了此函数的用法:
#include
using namespace std; int main() { // function returns highest signed integer value int int_inf = (int)__builtin_infd32(); // you can use __builtin_inf() function // as well and then cast to integer int int_inf2 = (int)__builtin_inf(); cout << int_inf << endl; cout << int_inf2 << endl; // function returns highest unsigned integer value unsigned int unsinged_int_inf = (unsigned int)__builtin_infd32(); // you can use __builtin_inf() function as well // and then cast to unsigned integer unsigned int unsinged_int_inf2 = (unsigned int)__builtin_inf(); cout << unsinged_int_inf << endl; cout << unsinged_int_inf2 << endl; return 0; } 输出:2147483647 2147483647 4294967295 4294967295
- __builtin_infd64(void) :此函数返回可以用8字节整数表示的最大值,这是我们可以在C++中使用的整数的最大值。
例子:
if __builtin_infd64() is used it returns the maximum value of the 64-bit integer Output: 9223372036854775807
这是一个C++程序,显示了此函数的用法:
#include
using namespace std; int main() { // highest signed 8 byte value long long int long_inf = (long long int)__builtin_infd64(); // you can use __builtin_inf() as well // and then cast to long long int long long int long_inf2 = (long long int)__builtin_inf(); cout << long_inf << endl; cout << long_inf2 << endl; // highest unsiged 8 byte value unsigned long long int unsigned_longlong_inf = (unsigned long long int)__builtin_infd64(); // you can use __builtin_inf() as well // and then cast to unsigned long long int unsigned long long int unsigned_longlong_inf2 = (unsigned long long int)__builtin_inf(); cout << unsigned_longlong_inf << endl; cout << unsigned_longlong_inf2 << endl; return 0; } 输出:9223372036854775807 9223372036854775807 18446744073709551615 18446744073709551615
- __builtin_infd128(void) :由于在C / C++中可用于整数的最大大小为64位,因此此函数给出的结果与__builtin_infd64()相同。
例子:
if __builtin_infd128() is used it returns the maximum value of the 64-bit integer Output: 9223372036854775807
- __builtin_inff(void) :这些函数返回浮点最大值,表示4字节最大小数点值。
例子:
if __builtin_inff() is used it returns the maximum value of the 32-bit float Output: inf
这是一个C++程序,显示了此函数的用法:
#include
using namespace std; int main() { float float_inf = __builtin_inff(); // you can use __builtin_inf() as well // and then cast it to float data type float float_inf2 = (float)__builtin_inf(); cout << float_inf << endl; cout << float_inf2 << endl; return 0; } 输出:inf inf
- __builtin_infl(void) :这些函数返回最大的long long double数据类型值。
例子:
if __builtin_infl() is used it returns the maximum value of the long double type Output: inf
这是一个C++程序,显示了此函数的用法:
#include
using namespace std; int main() { long double long_double_inf = __builtin_infl(); // you can use __builtin_inf() as well // and then cast it to float data type long double long_double_inf2 = (long double)__builtin_inf(); cout << long_double_inf << endl; cout << long_double_inf2 << endl; return 0; } 输出:inf inf
- 在最新版本的GCC编译器中,这是两个新函数: __builtin_infn(void)和__builtin_infnx(void) 。一个人可以用32或64替换n ,它将分别返回32位,64位的inf值。
相似的文章:GCC编译器的内置函数
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。