在cstdlib头文件中定义了abs(),labs(),llabs()函数。这些函数返回输入到它们的整数绝对值作为其参数。
- ABS()函数:输入该函数在C int类型的值和int类型,长整型或长长整型的值在C++中。在C中,输出是int类型,在C++中,输出具有与输入相同的数据类型。
以下是示例C++程序,用于显示abs()函数。// CPP program to illustrate // abs() function #include
#include using namespace std; int main() { int val1, val2; /// finding absolute value using /// abs() function. val1 = abs(22); val2 = abs(-43); cout << "abs(22) = " << val1 << "\n"; cout << "abs(-43) = " << val2 << "\n"; return 0; } Output: abs(22) = 22 abs(-43) = 43
- labs()函数:这是abs()函数的长整数版本。输入和输出均为long int类型。
以下是示例C++程序,用于显示labs()函数。// CPP program to illustrate // labs() function #include
#include using namespace std; int main() { int val1, val2; /// finding absolute value using /// labs() function. val1 = labs(1234355L); val2 = labs(-4325600L); cout << "labs(1234355L) = " << val1 << "\n"; cout << "labs(-4325600L) = " << val2 << "\n"; return 0; } Output: labs(1234355L) = 1234355 labs(-4325600L) = 4325600
- llabs()函数:这是abs()函数的long long int版本。输入和输出均为long long int类型。
以下是示例C++程序,用于显示llabs()函数。// CPP program to illustrate // llabs() function #include
#include using namespace std; int main() { int val1, val2; /// finding absolute value using /// labs() function. val1 = llabs(1234863551LL); val2 = llabs(-432592160LL); cout << "llabs(1234863551LL) = " << val1 << "\n"; cout << "llabs(-432592160LL) = " << val2 << "\n"; return 0; } Output: llabs(1234863551LL) = 1234863551 llabs(-432592160LL) = 432592160
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。