Useful Inbuilt Functions in C++
Last Updated :
16 Oct, 2023
In-built functions in C++ are those functions that are part of C++ standard libraries. The purpose of inbuilt functions is to provide common and essential functionality that is frequently required in the programming. In this article, we will look at some of the commonly used inbuilt functions in C++.
1. sqrt() Function
The sqrt() function is used to determine the square root of the value of type double. It is defined inside <cmath> header file.
Syntax
sqrt (n)
Parameter
- This function takes only one parameter of type double which is a number we want to find the square root of.
Return Type
- The square root of the value of type double.
Example
C++
// C++ program to illustrate the sqrt() function
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
double x = 24;
double answer;
answer = sqrt(x);
cout << answer << endl;
return 0;
}
2. pow() Function
The pow() function is used to find the value of the given number raised to some power. This function is also defined inside <cmath> header file.
Syntax
double pow(double x, double y);
Parameters
- x: The base number.
- y: The exponential power.
Return Type
- value of x raised to the power y
Example
C++
// C++ program to illustrate the pow() function
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int base = 3;
int exponent = 2;
int answer = pow(base, exponent);
cout << answer << endl;
}
3. sort() Function
The sort() function is part of STL's <algorithm> header. It is a function template that is used to sort the random access containers such as vectors, arrays, etc.
Syntax
sort (arr , arr + n, comparator)
Parameters
- arr: The pointer or iterator to the first element of the array.
- arr + n: The pointer to the imaginary element next to the last element of the array.
- comparator: The unary predicate function that is used to sort the value in some specific order. The default value of this sorts the array in ascending order.
Return Value
- This function does not return any value.
Example
C++
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[] = { 4, 6, 2 , 8, 7 , 1 , 3};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n);
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
4. find() Function
The find() function is also the part of STL <algorithm> library. This function is used to find some value in the given range. It can be used with both sorted and unsorted datasets as it implements.
Syntax
find(startIterator, endIterator, key)
Parameter
- startIterator: Iterator to the beginning of the range.
- endIterator: Iterator to the end of the range.
- key: The value to be searched.
Return Value
- If the element is found, then the iterator to the element. Otherwise, iterator to the end.
Example
C++
// C++ program to illustrate the find() function
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> dataset{ 1, 89, 0, 7, 33, 45 };
// finding 0 in the above vector
auto index = find(dataset.begin(), dataset.end(), 0);
if (index != dataset.end()) {
cout << "The element found at "
<< index - dataset.begin() << " index";
}
else {
cout << "Element not found";
}
return 0;
}
OutputThe element found at 2 index
5. binary_search() Function
The binary_search() function is also used to find an element in the range but this function implements the binary search instead of the linear search as compared to the find function. It is faster than the find() function but it can only be implemented on sorted datasets with random access. It is defined inside the <algorithm> header file.
Syntax
binary_search (starting_pointer , ending_pointer , target);
Parameters
- starting_pointer: Pointer to the start of the range.
- ending_pointer: Pointer to the element after the end of the range.
- target: Value to be searched in the dataset.
Return Value
- Returns true if the target is found.
- Else return false.
Example
C++
// C++ program to illustrate the binary_search() function
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> arr = { 10, 11, 12, 13, 14 };
// checking if the value 16 is present or not
if (binary_search(arr.begin(), arr.end(), 16)) {
cout << 16 << " is present.";
}
else {
cout << 16 << " is not present";
}
cout << endl;
}
6 . max() Function
The std::max() function is used to compare the two numbers and find the maximum among them. It is also defined inside the <algorithm> header file.
Syntax
max (a , b)
Parameters
- a: First number
- b: Second number
Return Value
- This function returns the larger number among the two numbers a and b.
- If the two numbers are equal, it returns the first number.
Example
C++
// C++ program to illustrate the max() function
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
cout << max(7, 6);
return 0;
}
7 . min() Function
The std::min() function is used to compare the two numbers and find the minimum among them. It is also defined inside the <algorithm> header file.
Syntax
min (a , b)
Parameters
- a: First number
- b: Second number
Return Value
- This function returns the smaller number among the two numbers a and b.
- If the two numbers are equal, it returns the first number.
Example
C++
// C++ program to illustrate the min() function
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
cout << min(3, 4);
return 0;
}
8. swap() Function
The std::swap() function provides the basic functionality to swap two values. It is defined inside <algorithm> header file.
Syntax
swap(a , b);
Parameters
- a: First number
- b: Second number
Return Value
- This function does not return any value.
Example
C++
// C++ program to illustrate the swap() function
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a = 3, b = 4;
cout << "Before swap: " << endl;
cout << "a: " << a << " b: " << b;
cout << endl;
// using swap
swap(a, b);
cout << "After swap: " << endl;
cout << "a: " << a << " b: " << b;
return 0;
}
OutputBefore swap:
a: 3 b: 4
After swap:
a: 4 b: 3
9. tolower() Function
The tolower() function is used to convert the given alphabet character to the lowercase. It is defined inside the <cctype> header.
Syntax
tolower (c);
Parameters
- c: The alphabet to be converted.
Return Value
- Lowercase of the character c.
- Returns c if c is not an alphabet.
Example
C++
// C++ program to illustrate the use of tolower()
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
string str = "GFG";
// using tolower() for each character
for (auto& a : str) {
a = tolower(a);
}
cout << str;
return 0;
}
10. toupper() Function
The toupper() function is used to convert the given alphabet character to uppercase. It is defined inside the <cctype> header.
Syntax
toupper (c);
Parameters
- c: The alphabet to be converted.
Return Value
- Uppercase of the character c.
- Returns c if c is not an alphabet.
Example
C++
// C++ program to illustrate the use of toupper()
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
string str = "geeksforgeeks";
// using toupper() for each character
for (auto& a : str) {
a = toupper(a);
}
cout << str;
return 0;
}
Similar Reads
List in C++ - Some Useful Functions Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, the List has slow traversal, but once a position has been found, insertion and deletion is quick. List Useful Functions: 1. emplace(position, value): This function is used to insert an element at the sp
7 min read
Inline Functions in C++ In C++, inline functions provide a way to optimize the performance of the program by reducing the overhead related to a function call. When a function is specified as inline the whole code of the inline function is inserted or substituted at the point of its call during the compilation instead of us
6 min read
Function Pointer in C++ Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
Virtual Function in C++ A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object a
6 min read
std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
Functions in C++ A function is a building block of C++ programs that contains a set of statements which are executed when the functions is called. It can take some input data, performs the given task, and return some result. A function can be called from anywhere in the program and any number of times increasing the
9 min read
log() Function in C++ The std::log() in C++ is a built-in function that is used to calculate the natural logarithm (base e) of a given number. The number can be of any data type i.e. int, double, float, long long. It is defined inside the <cmath> header file.In this article we will learn about how to use std::log()
1 min read
norm() function in C++ with Examples The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax: template<class T> T norm (const complex<T>& z); Parameter: z: It represents the given complex number. Return: It returns the squared magni
1 min read
Function Pointers and Callbacks in C++ A callback is a function that is passed as an argument to another function. In C++, we cannot directly use the function name to pass them as a callback. We use function pointers to pass callbacks to other functions, store them in data structures, and invoke them later. In this article, we will discu
2 min read
std::is_function template in C++ with Examples The std::is_function of C++ STL is used to check whether the given type T is function or not. It returns the boolean value either true or false. Below is the syntax for the same: Header File: #include<type_traits> Syntax: template <class T> struct is_function; Parameter: The template std
2 min read