C++ Functions Flashcards
toupper() & tolower()
Function
toupper
converts a character to uppercase. tolower
converts a character to lowercase.
- Included in the standard library.
- Only works with
char
.
Syntax
char ch = 'a'; char upper = toupper(ch); // Converts 'a' to 'A' char lower = tolower(ch); // Converts 'a' to 'a' (no change)
stoi()
Function
stoi
is a C++ function used to convert a string to an integer.
- It’s included in the <string> library.</string>
-
stoi
doesn’t work with strings containing non-integer characters or numbers exceeding the integer range.
Syntax
int myInt = stoi(myString);
min () and max()
Function
In C++, the min
function returns the smaller of two values, while the max
function returns the larger of two values.
- Included in the
<algorithm>
header. - Also works with
char
andstring
.
Syntax
float a = 3.5; float b = 2.0; float result = std::min(a, b); // result will be 2.0
setprecision()
Function
setprecision() is a C++ function that sets the decimal precision for output, controlling the number of decimal places in floating-point values.
- included in the
iomanip
header - does not work with integer values
- the
fixed
andscientific
prefixes respectively work with decimal places and significant figures
Syntax
int main() { double num = 123.456789; cout << fixed << setprecision(2) << num << endl; // Displays as fixed-point with 2 decimal places. cout << scientific << setprecision(2) << num << endl; // Displays in scientific notation with 2 decimal places.
.length () and .size()
Function
In C++, .length()
is used for strings to get the number of characters, while .size()
is a general method for containers to retrieve their size (element count).
- Both can be used to find the length of a string variable.
-
.size
can also be used to fine the element size in an array. - Only
.length
is included in the<string>
header
Syntax
string str = "Hello, World!"; // Using .size() to get the length of the string cout << "Length using .size(): " << str.size() << endl; // Using .length() to get the length of the string cout << "Length using .length(): " << str.length() << endl;
pow()
Function
pow() is a C++ function used to calculate the power of a number. It raises a given base to a specified exponent.
- Included in the
<cmath>
library. - Does not work with complex numbers.
Syntax
double result = pow(base, exponent);
round(), floor(), and ceil()
Function
The round
function is used to round a floating-point number to the nearest integer
The floor
function rounds a floating-point number down to the nearest integer.
The ceil
function rounds a floating-point number up to the nearest integer.
- All are part of the
<cmath>
library - They do not work with non-numeric values
Syntax
double num = 5.7; cout << "Round: " << round(num) << endl; cout << "Floor: " << floor(num) << endl; cout << "Ceiling: " << ceil(num) << endl;
sqrt()
Function
sqrt()
is a C++ function used to calculate the square root of a number.
- Included in the
<cmath>
header. - Does not work with complex data types.
Syntax
double result = sqrt(16.0)
abs() and fabs()
Function
abs()
and fabs()
are functions in C++ that are used to calculate the absolute value of a number.abs()
works with integers only. fabs()
works with floating point numbers.
- Included in the
<cmath>
library. - They do not work with non-numeric data types.
Syntax
int integerNum = -5; double floatingNum = -5.67; int absoluteInt = abs(integerNum); double absoluteFloat = fabs(floatingNum);
rand()
Function
rand()
is a C++ function for generating pseudo-random integers.
- Included in
<cstdlib>
library. - Doesn’t work with non-integer data types.
Syntax
int randomNum = rand();