C++ Functions Flashcards

1
Q

toupper() & tolower()

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

stoi()

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

min () and max()

A

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 and string.

Syntax

float a = 3.5;
float b = 2.0;
float result = std::min(a, b); // result will be 2.0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

setprecision()

A

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 and scientific 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.
		
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

.length () and .size()

A

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;
    
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

pow()

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

round(), floor(), and ceil()

A

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;
		
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

sqrt()

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

abs() and fabs()

A

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);
		
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

rand()

A

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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly