In this article, you will discuss the inbuilt functions in C++ with their different functions and examples. Before discussing the inbuilt functions, you must know about the functions in C++.
A function is a section of code that only executes when it is called. Parameters are referred to the data that you supply to a function.
Functions are used to carry out certain tasks and are crucial for code reuse. You can use the code numerous times by defining it only once.
I. String Functions in C++
There are several string functions in C++. Some string functions in C++ are as follows:
1. strcat
Strcat function is used to include the cstring header file in the program. Two character arrays can be entered into the strcat method . The second array is joined to the end of the first array by concatenation.
Syntax:
It has the following syntax:
strcat(char_array1, char_array2);
The strcat function only takes character arrays as parameters, which is crucial to remember. String objects cannot be used in the function.
Example:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str_1[50] = "Radha ";
char str_2[] = "Krishna.";
strcat(str_1, str_2);
cout<< str_1 <<endl;
return 0;
}
Output:
Radha Krishna.
Explanation:
In this example, first, the code serves two character arrays, str1 , and str2 , with data. After that, the str2 data is appended to the end of str1 by using the strcat function from the cstring header . Finally, it uses the cout command to print the combined string. This program will result in "Radha Krishna" .
2. length
As its name implies, the length function is used to return the length of a string.
Example:
#include <iostream>
using namespace std;
int main()
{
string str_1 = "The fruit name is Apple";
cout<< "The length of the string is: " << str_1.length() <<endl;
return 0;
}
Output:
The length of the string is: 23
Explanation:
In this example, the code creates a string called str, which determines its length using the length function and then prints the length and a message. The program will output the following when it is run:
"The fruit name is Apple" is a string of 23 characters long.
3. capacity
The size of the allotted space for the string as of the current time is returned by the capacity function . The dimension can match or exceed the string's dimension . You can allot more space than the string's maximum length to handle additional characters in the string efficiently.
Example:
#include <iostream>
using namespace std;
int main()
{
string str_1 = "Seetha Ram";
cout<< "The length of the string is: " << str_1.length() <<endl;
cout<< "The capacity of the string is: " << str_1.capacity() <<endl;
return 0;
}
Output:
The length of the string is: 10
The capacity of the string is: 15
Explanation:
In this example, the code fills the string str_1 with the words "Seetha Ram" when it is first created. After that, it uses the member functions length and capacity to print out the string's length and capacity.
The string "Seetha Ram" has a length of 10 characters and a capacity of 11 characters, allowing for potential future growth. These two numbers represent the memory set aside for the string.
4. resize
The resize function is a function that is used to change the size of a string.
Example:
#include <iostream>
using namespace std;
int main()
{
string str_1 = "God is great.";
cout<< "The original string is: " << str_1 <<endl;
// Only the first eight characters of the string will be retained.
str_1.resize(8);
cout<< "The string after using resize is: " << str_1 <<endl;
return 0;
}
Output:
The original string is: God is great.
The string after using resize is: God is g
Explanation:
In this example, the string "God is great." is initialized as str_1 by the programmer. After that, the original string is printed, and the string is shrunk to fit only the first eight characters, and finally, the shrunk string is printed.
The string "God is great." is reduced in length after scaling to only the first eight letters, becoming "God is g".
5. begin
The begin function provides an iterator that leads to the string's starting place.
Example:
#include <iostream>
using namespace std;
int main()
{
string str_1 = "Keep Learning";
// declaring an iterator
string::iterator it;
it = str_1.begin();
cout<< * it << " ";
it++;
cout<< * it;
return 0;
}
Output:
Explanation:
In this example, the code fills the string str_1 with the phrase "Keep Learning" at the beginning. After that, an iterator is declared and initialized with a pointer to the start of the string. The program outputs the first character that the iterator points to, followed by a space that moves on to the next character and then outputs that character.
This is the case because the iterator successively points to the letters "K" and "e" in the text "Keep Learning" .
6. end
The end method provides an iterator thacpp tutorials to the string's end . The null character will always be where the iterator points.
Example:
#include <iostream>
using namespace std;
int main()
{
string str = "Taj mahal is the symbol of love";
// declaring an iterator
string::iterator it;
it = str.end();
cout<< * it; // nothing will print because the iterator points to \0
it--;
cout<< * it;
it--;
cout<< * it;
return 0;
}
Output:
Explanation:
The initialization code takes the string str with the phrase "The Taj Mahal is a symbol of love" . After that, the iterator is initialized and declared to point to the string's final null character (0) . Following a null character output of nothing, the program uses an iterator to go backward and output the characters 'e' and 'v' .
This is the case because the iterator progressively points in reverse order to the characters "e" and "v" .
7. begin
The keyword rbegin means "the reverse beginning". It serves as a reference for the string's final character.
Example:
#include <iostream>
using namespace std;
int main()
{
string str = "Charminar";
// declaring a reverse iterator
string::reverse_iteratorrit;
rit = str.rbegin();
cout<< * rit;
rit++;
cout<< * rit;
rit++;
cout<< * rit;
return 0;
}
Output:
Explanation:
In this example, the initialization of the string "Charminar" in the str variable is performed by the code. Following that, a reverse iterator named rit is declared and initialized to refer to the string's final character. As the reverse iterator is increased, the program outputs the letters "r", "a" , and "n" .
This is because the reverse iterator progressively points to the letters "r", "a", and "n" .
8. rend
The rend function is typically used with bidirectional containers like vectors, arrays , and others that provide reverse iteration through reverse iterators . However, utilizing its iterators, std::string only permits forward iteration.
Example:
#include <iostream>
#include <string>
int main ()
{
std::string str_1 ("start living");
for (std::string::reverse_iteratorrit=str_1.rbegin(); rit!=str_1.rend(); ++rit)
std::cout<< *rit;
return 0;
}
Output:
gnivil trats
Explanation:
In this example, the string "start living" is the text that the code initializes in the str_1 variable. After that, it iterates through the characters in the string in reverse order using a for loop and a reverse iterator . Reverse order is used by the program to produce each character.
The letters in the string "start living" displayed in the opposite order are the ones that make up this output.
9. copy
The copy method copies a sub-string from one string (character array) to another string (character array) specified in the function's arguments. It requires three inputs (at least two), including the target character array , the length of the copied text , and the starting place inside the string.
Example:
#include <iostream>
using namespace std;
int main()
{
string str_1 = "copy function is used for";
char str_2[10];
// copying nine chars starting from index 5
str_1.copy(str_2, 9, 5);
// The last character of a char array should be \0
str_2[9] = '\0';
cout<< "The copied string is: " << str_2;
return 0;
}
Output:
The copied string is: function
Explanation:
The initialization code sets the string "copy function is used for" as the value of the variable str1 . After that, it copies a section of str1 (from index 5 to 9 characters forward) to a character array called str2 , using the copy function . The copied string is produced after the program inserts a null terminator to str2 to make it a genuine C-style string.
This result is the same as the characters "function " that were copied from str_1 .
10. swap
The swap function is used to swap the two strings.
Example:
#include <iostream>
using namespace std;
int main()
{
string fruit_1 = "Banana";
string fruit_2 = "Sapota";
cout<< "Before swapping - fruit 1 is: " << fruit_1 <<endl;
cout<< "Before swapping - fruit 2 is: " << fruit_2 <<endl<<endl;
// swapping
fruit_1.swap(fruit_2);
cout<< "After swapping - fruit 1 is: " << fruit_1 <<endl;
cout<< "After swapping - fruit 2 is: " << fruit_2 <<endl;
return 0;
}
Output:
Before swapping - fruit 1 is: Banana
Before swapping - fruit 2 is: Sapota
After swapping - fruit 1 is: Sapota
After swapping - fruit 2 is: Banana
Explanation:
In this example, the two string variables fruit1 and fruit2 are initialized by the code to contain "Banana" and "Sapota" accordingly. After that, it gives an example of how to use the swap function to switch the data in these two strings. The strings' original and swapped contents are output by the program.
II. Mathematical functions in C++
There are several mathematical functions in C++. Some mathematical functions in C++ are as follows:
1. abs
The argument's absolute value is returned by the abs function .
Example:
#include <iostream>
#include <cmath>
int main()
{
std::cout<< std::abs(-7.8) << "\n";
}
Output:
2. ceil
The whole number that follows the argument and is greater than or equal to it is returned by the ceil function .
Example:
#include <iostream>
#include <cmath>
int main()
{
double num_1 = 13.5458;
double result;
result = std::ceil(num_1);
std::cout<< "The result is " << result << "!\n";
}
Output:
The result is 14!
3. floor
The first whole number that is less than or equal to the parameter is returned by the floor method .
Example:
#include <iostream>
#include <cmath>
int main()
{
double num_1 = 15.33576;
double result;
result = std::floor(num_1);
std::cout<< "The result is " << result << "!\n";
}
Output:
The result is 15!
4. max
The fmax method is used to find the greater of two inputs.
Example:
#include <iostream>
#include <cmath>
int main()
{
double num_1 = -43.253;
double num_2 = -35.462;
double result;
result = fmax(num_1, num_2);
std::cout<< "The larger value between " << num_1 << " and " << num_2 << " is " << result << "\n";
}
Output:
The larger value between -43.253 and -35.462 is -35.462
5. fmin
The fmin function is used to find the smaller parameter from two inputs.
Example:
#include <iostream>
#include <cmath>
int main()
{
double num_1 = -36.786;
double num_2 = -23.231;
double result;
result = fmin(num_1, num_2);
std::cout<< "The smaller value between " << num_1 << " and " << num_2 << " is " << result << "\n";
}
Output:
The smaller value between -36.786 and -23.231 is -36.786
6. pow
The outcome of raising a base value by an exponent is returned by the pow function .
Example:
#include <iostream>
#include <cmath>
int main()
{
double base = 5;
double exponent = 3;
double result = pow(base, exponent);
std::cout<< base << " raised to the power of " << exponent << " equals " << result << ".\n";
}
Output:
5 raised to the power of 3 equals 125.
7. sqrt
The square root of the argument is returned by the sqrt function .
Example:
#include <iostream>
#include <cmath>
int main()
{
double num_1 = 166;
double result;
result = std::sqrt(num_1);
std::cout<< "The square root of " << num_1 << " is " << result << "\n";
}
Output:
The square root of 166 is 12.8841
8. round
The round function is used to provide the nearest integer to the parameter, with halfway cases adjusted away from the final zero.
Example:
#include <iostream>
#include <cmath>
int main()
{
double num_1 = 25.53;
double result;
result = std::round(num_1);
std::cout<< "The result is " << result << "\n";
}
Output:
The result is 26
III. Input/Output Functions in C++
1. cin:
The Cin function is used to read input from the standard input, which is typically the keyboard.
2. cout:
The cout function is used to write output to the standard output, which is often the console.
Example:
#include <iostream>
using namespace std;
int main()
{
int num_1;
cout<< "Enter a number: ";
cin>> num_1;
cout<< "The entered number is : " << num_1 <<endl;
return 0;
}
Output:
Enter a number: 5
The entered number is: 5
IV. lgorithm Functions in C++
1. std::sort:
The std::sort function is used to sort the content of a container.
2. std::find:
The std:: find is used to hunt down a specific element inside a container.
Example:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> numbers = {4, 1, 10, 8, 5, 9, 3, 6};
sort(numbers.begin(), numbers.end());
cout<< "Sorted numbers: ";
for (int num: numbers)
{
cout<<num<< " ";
}
cout<<endl;
if (find(numbers.begin(), numbers.end(), 6) != numbers.end()) {
cout<< "Number 5 found in the vector." <<endl;
}
return 0;
}
Output:
Sorted numbers: 1 3 4 5 6 8 9 10
Number 5 is found in the vector.
V. Container Functions
1. std::vector:
The dynamic array std::vector offers routines for element manipulation and dynamic resizing.
2. std::map and std::set:
Associative containers that hold unique values and key-value pairs, respectively, are std::map and std::set .
Example:
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
vector<int> numbers = {11,84,24,35,49};
cout<< "The size of the vector is: " <<numbers.size() <<endl;
map<string, int> ages;
ages["Ram"] = 30;
ages["Seetha"] = 27;
cout<< "Ram's age: " << ages["Ram"] <<endl;
return 0;
}
Output:
The size of the vector is: 5
Ram's age: 30