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
nearbyint() function in C++
The nearbyint() function is defined in cmath header file. This function round off the given value to a nearby integral value by using the current rounding method. The current rounding method is described by fegetround. Syntax: float nearbyint(float x);or,double nearbyint(double x);or,long double nea
2 min read
real() function in C++
The real() function is defined in the complex header file. The real() function is used to find the real part of the complex number. Syntax: template<class T> T real(const complex<T>& z); Parameter: This method takes a mandatory parameter z: which represents the given complex number.
2 min read
Higher Order Functions in C++
Higher-order functions are functions that take functions as an argument. It is used in functional languages which is not used in C++ are, although this is slowly changing since C++11 gave us lambdas and 'std::function'... and frequently people don't realize that 'std::function' is not a tool that fi
3 min read
valarray size() function in C++
The size() function is defined in valarray header file. This function is used to find the size of valarray and returns the size of valarray. Syntax: size_t size() const; Parameter: This function doesn't takes any parameter. Returns: This function returns the number of element in valarray. Below prog
1 min read
Immediate Functions in C++
In this article, we will discuss the immediate function used in C++. Immediate Function: In C++20, an immediate function is a function where every call to the function either directly or indirectly produces a compile-time constant expression.These functions are declared by using a consteval keyword
4 min read
Overloading function templates in C++
Template: A template is a tool that reduces the efforts in writing the same code as templates can be used at those places.A template function can be overloaded either by a non-template function or using an ordinary function template. Function Overloading: In function overloading, the function may ha
3 min read
erase_if() Function in C++
The std::erase_if() is a utility introduced in C++20 that is used to remove elements from containers based on a specified condition. This function erases all elements that satisfy a given predicate from standard containers like std::vector, std::deque, std::list, std::forward_list, std::string, and
3 min read
Function Pointer to Member Function in C++
In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a functio
3 min read
C++ Mathematical Functions
C++ being a superset of C, supports a large number of useful mathematical functions. These functions are available in standard C++ to support various mathematical calculations. Instead of focusing on implementation, these functions can be directly used to simplify code and programs. In order to use
5 min read
C++ Function Practice Problems
Functions are the basic building block of the program. They are the block of code that performs a specific task. Function can be executed from anywhere in the program any number of times. They increase the modularity and reusability of the code. Practicing problems is one of the most effective ways
2 min read