Inbuilt Functions in C++
In this article, you will learn about the inbuilt functions in C++ with their examples and functions.
Inbuilt Functions:
Built-in functions and standard library functions are other names for inbuilt functions in C++. Once you include the correct header files, you can access these functions, which are part of the C++ Standard Library. They handle a wide variety of tasks and processes, making it simpler for programmers to carry out typical tasks without having to develop them from scratch.
Following are some categories of inbuilt C++ functions that are often used, along with samples of functions from each category:
1. Input/Output (iostream):
There are several Input/Output functions that are used in C++. Some of them are as follows:
- cin: It is used to read the user input.
- cut: It sends information to the console.
- getline(): It reads a single line of text.
Example:
Let's take a program to illustrate the above functions:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "You entered: " << num << endl;
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp1.png)
2. Math (cmath/cmath.h):
There are several math functions that are used in C++. Some of them are as follows:
- sqrt(): It determines a number's square root.
- pow(): It determines a number's power.
- Trigonometric functions : sin(), cos(), and tan().
Example:
Let's take a program to illustrate the above functions:
#include <cmath>
#include<bits/stdc++.h>
using namespace std;
int main() {
double x = 16.0;
double squareRoot = sqrt(x);
cout << "Square root of " << x << " is " << squareRoot << endl;
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp2.png)
3. String (string):
There are several string functions that are used in C++. Some of them are as follows:
- length(): It returns the length of the string.
- substr(): It obtains a substring from a string.
- find(): It locates a substring's location within a string.
Example:
Let's take a program to illustrate the above functions:
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Hello, world!";
int length = text.length();
string substring = text.substr(7, 5);
size_t position = text.find("world");
cout << "Length: " << length << endl;
cout << "Substring: " << substring << endl;
cout << "Position of 'world': " << position << endl;
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp3.png)
4. Containers(Map, list, and vector):
There are several container functions that are used in C++. Some of them are as follows:
- push_back(): It pushes a vector's last element to the end.
- insert(): It inserts a specified element into a container.
- erase(): It erases a component's element.
Example:
Let's take a program to illustrate the above functions:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {1, 2, 3};
numbers.push_back(4);
numbers.insert(numbers.begin() + 1, 5);
numbers.erase(numbers.begin() + 2);
for (int num: numbers) {
cout << num << " ";
}
cout << endl;
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp4.png)
5. File Handling (fstream):
There are several file handling functions that are used in C++. Some of them are as follows:
- Ifstream: It is used to read from files when handling files (fstream).
- ofstream: It is a writing tool for files.
- fstream: It is a reading and writing stream.
Example:
Let's take a program to illustrate the above functions:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream outputFile("output.txt");
if (outputFile.is_open()) {
outputFile << "Hello, File Handling!";
outputFile.close();
cout << "Data written to file." << endl;
} else {
cout << "Unable to open the file." << endl;
}
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp5.png)
6. Pow():
- This function aids in determining the value of one number multiplied by another.
- It always accepts parameters of the double data type (but it also accepts int values), and the output is also of the double data type.
Header file:
- The cmath header file contains a definition of the pow() function.
#include <cmath>
Syntax:
pow(exponent, base)
- This function's output is the base exponent.
- O(exponent) is the time complexity.
Example:
Let's take a program to illustrate the pow() function:
// CPP program to illustrate
// power function
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = 6.1, y = 4.8;
// Storing the answer in the result.
double result = pow(x, y);
// Printing the result upto 2
// decimal place
cout << fixed << setprecision(2) << result << endl;
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp6.png)
7. swap():
- This function is used to swap two numbers. It swaps the values of two parameters that are two instances of the same data type.
Header file:
- #include <algorithm> (until C++11)
- #include <utility> (since C++11)
- #include <string_view> (since C++17)
Syntax:
swap(value1, value2);
Time Complexity: O(1)
Example:
Let's take a program to illustrate the swap() function:
// C++ program for illustration
// of swap() function
#include <iostream>
#include <utility>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << "Value of a before: " << a << endl;
cout << "Value of b before: " << b <<endl;
// swap values of the variables
swap(a, b);
cout << "Value of a now: " << a << endl;
cout << "Value of b now: " << b << endl;
return 0;
}
Output:
![Inbuilt Functions in C++](https://static.tutorialandexample.com/cpp/inbuilt-functions-in-cpp7.png)
Conclusion:
- These are only a few of the numerous built-in functions that C++ offers.
- A large variety of functions are available in the C++ Standard Library to aid programmers in completing a variety of tasks quickly.
- Include the relevant header files and adhere to the documentation's recommended syntax to utilize these functions.