Functions in C++: Built-in

Mannan Ul Haq
0

C++ is a powerful programming language that offers a wide range of built-in functions to simplify programming tasks and provide ready-to-use functionality. These built-in functions are part of the standard library and cover various areas such as input/output, mathematical operations, string manipulation, and more. In this response, we will explore some of the important and commonly used built-in functions in C++.


1. `<cctype>`:

This library provides functions for character classification and manipulation.

Function Purpose Example
isalnum() Checks if a character is alphanumeric. isalnum('A') returns true.
isalpha() Checks if a character is alphabetic. isalpha('7') returns false.
isdigit() Checks if a character is a decimal digit. isdigit('3') returns true.
islower() Checks if a character is lowercase. islower('x') returns true.
isupper() Checks if a character is uppercase. isupper('Z') returns true.
isspace() Checks if a character is whitespace. isspace(' ') returns true.
tolower() Converts a character to lowercase. tolower('H') returns 'h'.
toupper() Converts a character to uppercase. toupper('e') returns 'E'.

2. `<cmath>`:

This library provides mathematical functions and constants.

Function Purpose Example
sqrt() Calculates the square root of a number. sqrt(25) returns 5.
pow() Calculates the power of a number. pow(2, 3) returns 8.
abs() Returns the absolute value of a number. abs(-10) returns 10.
sin() Calculates the sine of an angle (in radians). sin(Math.PI / 2) returns 1.
cos() Calculates the cosine of an angle (in radians). cos(0) returns 1.
tan() Calculates the tangent of an angle (in radians). tan(Math.PI / 4) returns 1.

3. `<cstring>`:

This library provides functions for string manipulation and memory operations.

Function Purpose Example
strlen() Returns the length of a C-style string. strlen("Hello") returns 5.
strcpy() Copies a C-style string to another. char dest[10]; strcpy(dest, "Hello");
strcat() Concatenates two C-style strings. char str1[10] = "Hello"; char str2[10] = "World"; strcat(str1, str2);
strcmp() Compares two C-style strings lexicographically. strcmp("apple", "banana") returns a negative value.

4. `<string>`:

This library provides the `string` class for more convenient string manipulation compared to C-style strings.

Member Function Purpose Example
length() Returns the length of the string. string str = "Hello"; str.length() returns 5.
substr() Returns a substring of the string. string str = "Hello World"; str.substr(6, 5) returns "World".
find() Finds the first occurrence of a substring within the string. string str = "Hello World"; str.find("World") returns 6.
replace() Replaces a portion of the string with another string. string str = "Hello World"; str.replace(6, 5, "Universe") replaces "World" with "Universe".
operator+ Concatenates two strings. string str1 = "Hello"; string str2 = "World"; string result = str1 + str2;
operator== Compares two strings for equality. string str1 = "Hello"; string str2 = "World"; if (str1 == str2) ...
size() Returns the size of the string (same as length()). string str = "Hello"; str.size() returns 5.

5. `<cstdlib>`:

This library provides general-purpose functions.

Function/Macro Purpose Example
atoi() Converts a string to an integer. int num = atoi("123");
atof() Converts a string to a floating-point number. float num = atof("3.14");
rand() Generates a random integer. int randomNum = rand();
srand() Seeds the random number generator. srand(time(NULL));
exit() Terminates the program. exit(0);

6. `<ctime>`:

This library provides functions for working with time and dates.

Function/Structure Purpose Example
time() Obtains the current calendar time. time_t currentTime = time(NULL);
ctime() Converts a time value to a string representation. time_t currentTime = time(NULL); char* timeString = ctime(&currentTime);
difftime() Calculates the difference in seconds between two time values. time_t startTime = time(NULL); // Perform some operations... time_t endTime = time(NULL); double duration = difftime(endTime, startTime);

7. `<algorithm>`:

This library provides a collection of algorithms to operate on containers and sequences.

Function Purpose Example
sort() Sorts the elements of a container in ascending order. vector numbers = {5, 2, 1, 3, 4}; sort(numbers.begin(), numbers.end());
reverse() Reverses the order of elements in a container. string str = "Hello"; reverse(str.begin(), str.end());
find() Finds the first occurrence of a value in a container. vector numbers = {1, 2, 3, 4, 5}; auto it = find(numbers.begin(), numbers.end(), 3);
count() Counts the occurrences of a value in a container. vector numbers = {1, 2, 2, 3, 2}; int count = count(numbers.begin(), numbers.end(), 2);
max_element() Returns an iterator pointing to the maximum element in a container. vector numbers = {5, 2, 1, 3, 4}; auto max = max_element(numbers.begin(), numbers.end());
min_element() Returns an iterator pointing to the minimum element in a container. vector numbers = {5, 2, 1, 3, 4}; auto min = min_element(numbers.begin(), numbers.end());
binary_search() Checks if a value exists in a sorted range. vector numbers = {1, 2, 3, 4, 5}; bool exists = binary_search(numbers.begin(), numbers.end(), 3);



Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !