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
5 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
raise() function in C++
csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored. Syntax: int raise ( int signal_ ) Parameter: The f
3 min read
list size() function in C++ STL
The list::size() is a built-in function in C++ STL that is used to find the number of elements present in a list container. That is, it is used to find the size of the list container.Syntax: list_name.size(); Time Complexity - Linear O(1) as per c++11 standard. Possible because the implementation ma
1 min read
logb() function in C++ STL
The logb() is a builtin function in C++ STL which returns the logarithm of |x|, using FLT_RADIX as base for the logarithm. In general, the value of FLT_RADIX is 2, so logb() is equivalent to log2()(for positive values only). Syntax: logb(val) Parameter: The function accepts a single mandatory parame
2 min read
String Functions in C++
A string is referred to as an array of characters. In C++, a stream/sequence of characters is stored in a char array. C++ includes the std::string class that is used to represent strings. It is one of the most fundamental datatypes in C++ and it comes with a huge set of inbuilt functions. In this ar
8 min read
Inbuilt function for calculating LCM in C++
Many times while we do programming, we need to calculate the Least Common Multiple (LCM) between two numbers. We have already discussed how to find LCM in this post. In place of defining and then using a function for calculating lcm , we can simply use an inbuilt function of boost library of C++ , b
2 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