General Built-in Functions
Learn to use the predefined utility functions available in C++.
We'll cover the following...
Library functions
C++ libraries provide certain utility functions that make it easier for programmers to perform routine tasks, like pow()
and sqrt()
. The commonly used other such functions are sizeof()
, round()
, reverse()
, and sort()
.
Note: We have to include respective header files that allow the use of these functions in the program.
Let’s explore some commonly used built-in functions.
Reverse order
The reverse()
function is used to access the elements of the array in reverse order. The result of this function is stored in the array itself. The syntax of calling this function is as follows:
reverse(array_name, array_name + array_length)
The beginning of the array can be accessed by simply using the array name, and in a similar fashion, the end of the array can be accessed by writing the name plus its length.
The following code will demonstrate how this function works:
#include <iostream>#include <algorithm>using namespace std;int main(){int arr[] = {0,1,2,3,4,5};int length = sizeof(arr)/sizeof(arr[0]);reverse(arr, arr + length); // Calling reverse functionfor (int i = 0; i < length; i++){cout << arr[i];}return 0;}
In the program above:
- An array variable
arr[]
is declared, and{0,1,2,3,4,5}
is