C ++ built -in functions are functions that are part of C ++ standard libraries. These functions are designed to provide general and necessary functionality that are often required in programming.
In this article, we will look at some of the commonly used built -in functions in C ++ so that you can start using them in your code.
We will cover what:
sqrt()
Ceremony
You use sqrt()
Function to determine the square root of double price. Has been inside the description of
Header file
Syntax:
sqrt (n)
Parameter: The function takes only one parameter type double that is a number we want to find a square root.
Type of return: Square root of double price.
Example code
Let’s see an example so you can see how this function works:
#include
#include
using namespace std;
int main()
{
double x = 100;
double answer;
answer = sqrt(x);
cout << answer << endl;
return 0;
}
Output:
10
pow()
Ceremony
You use pow()
Function to find the value of the number raised in some power. Also described within this function
Header file
Syntax:
double pow(double x, double y);
Parameters:
Type of return: Value X Raised in power y.
Example code:
Let’s see an example to see how it works:
#include
#include
using namespace std;
int main()
{
int base = 5;
int exponent = 3;
int answer = pow(base, exponent);
cout << answer << endl;
}
Output:
125
sort()
Ceremony
sort()
Function is part of STL
Header This is a function template that you can use for random access containers, such as vector, rows, and so on.
Syntax:
sort (arr , arr + n, comparator)
Parameters:
Arr: Pointer or Attenders towards the first element of the array.
Arr + n: Referring to the next fantasy element of the last element of the array.
Compare: The union forecast function that is used to configure the value in a certain order. Its default price sets the array in the climbing sequence.
Return value: This function does not return any price.
Example code:
Let’s see an example:
#include
#include // Header file that includes the sort() function
using namespace std;
int main()
{
int arr() = { 13, 15, 12, 14, 11, 16, 18, 17 };
int n = sizeof(arr) / sizeof(arr(0));
sort(arr, arr + n);
for (int i = 0; i < n; ++i)
cout << arr(i) << " ";
return 0;
}
Output:
11 12 13 14 15 16 17 18
find()
Ceremony
find()
Function is also part of STL
The library you use to find a value in the range given to this function. You can use it with both sorted and unmanned datases as it enforces linear search algorithm.
Syntax:
find(startIterator, endIterator, key)
Parameters:
Starter: Renks at the beginning of the range.
Andreter: It reaches the end of the range.
Key: The price will be searched.
Return value: If the element is found, then the Atterator is set on the element. Otherwise, it reaches the end.
Example code:
Let’s see an example to better understand how it works:
#include // Required for the find() function
#include
#include
using namespace std;
int main()
{
vector<int> dataset{ 12, 28, 16, 7, 33, 43 };
auto index = find(dataset.begin(), dataset.end(), 7);
if (index != dataset.end()) {
cout << "The element is found at the "
<< index - dataset.begin() << "nd index";
}
else {
cout << "Element not found";
}
return 0;
}
Output:
The element is found in the third index
binary_search()
Ceremony
binary_search()
The function is also used to find factor in the range – but this function enforces binary search instead of a linear search than that find()
The function is faster than that too find()
Function, but you can only use it on configured datasters with access. Has been inside the description of
Header file
Syntax:
binary_search (starting_pointer , ending_pointer , target);
Parameters:
Starting_point: Pointing to the start of the range.
End_point: Pointing to the element after the end of the limit.
Target: Will be searched in the dataset.
Return value:
Example code:
Let’s see an example to see how it works:
#include
#include
#include
using namespace std;
int main()
{
vector<int> arr = { 56, 57, 58, 59, 60, 61, 62 };
if (binary_search(arr.begin(), arr.end(), 62)) {
cout << 62 << " is present in the vector.";
}
else {
cout << 16 << " is not present in the vector";
}
cout << endl;
}
Output:
62 is in vector.
max()
Ceremony
You can use std::max()
Function to compare two numbers and find a bigger between them. It has also been appreciated within
Header file
Syntax:
max (a , b)
Parameters:
A: The first number
b: The second number
Return value:
This function returns a large number between the two numbers A And B
If both numbers are equal, it returns the first number.
Example code:
Here is an example:
#include
#include
using namespace std;
int main()
{
int a = 8 ;
int b = 10 ;
int maximum = max(a, b);
cout << "The maximum of " << a << " and " << b << " is: " << maximum << endl;
return 0;
}
Output:
Max is 8 and 10: 10
min()
Ceremony
You can use std::min()
Function to compare two numbers and find small of both. It has also been appreciated within
Header file
Syntax:
min (a , b)
Parameters:
A: The first number
b: The second number
Return value:
This function returns small numbers between two numbers A And B
If both numbers are equal, it returns the first number.
Example code:
Here is an example:
#include // For the built-in min() function
#include
using namespace std;
int main()
{
int a = 4 ;
int b = 8 ;
int smallest = min(a, b);
cout << "The smaller number between " << a << " and " << b << " is: " << smallest << endl;
return 0;
}
Output:
The smaller number between 4 and 8 is: 4
swap()
Ceremony
std::swap()
The function allows you to change two values. Its description has been made in
Header file
Syntax:
swap(a , b);
Parameters:
A: The first number
b: The second number
Return value: This function does not return any price.
Example:
How does it work:
#include // For the built-in swap() function
#include
using namespace std;
int main()
{
int firstNumber = 8 ;
int secondNumber = 9 ;
swap(firstNumber, secondNumber);
cout << "After the swap:" << endl;
cout << firstNumber << " " << secondNumber << endl;
return 0;
}
Output:
After the exchange:
9 8
tolower()
Ceremony
You can use tolower()
Function to convert a given alphabet character into a lower case. Has been inside the description of
Header
Syntax:
tolower (c);
Parameter (Languages):
- C: The character should be changed.
Return value:
Example code:
How does it work:
#include
#include
using namespace std;
int main()
{
string str = "FRECODECAMP";
for (auto& a : str) {
a = tolower(a);
}
cout << str;
return 0;
}
Output:
Free Codecamp
toupper()
Ceremony
You can use toupper()
Function to turn the alphabet character into a large. Has been inside the description of
Header
Syntax:
toupper (c);
Parameters:
- C: The character should be changed.
Returning value
Example code:
How does it work:
#include
#include
using namespace std;
int main()
{
string str = "freecodecamp";
for (auto& a : str) {
a = toupper(a);
}
cout << str;
return 0;
}
Output:
Free Codecamp
Conclusion
These bullet functions are helpful tools in competitive programming and joint programming works. These codes help improve reading ability and enhance the code performance. In the aforementioned article, we discussed some of the most useful common intestines. There are some ordinary interstit functions max()
For, for, for,. min()
For, for, for,. sort()
And sqrt()
Etc. Using these bullet libraries, we can reduce the boiler plate code and accelerate the software development process. They help write more comprehensive, reliable, and maintaining C ++ programs.
If you enjoyed this article, you can see my more work here:
Awash Mishra’s Author Profile at Tutorial Point
And I have written some other lessons about math and programming: