Inbuilt Functions In C++ - C++ Programming Tutorial
C++ Course / Functions / Inbuilt Functions In C++

Inbuilt Functions In C++

BLUF: Mastering Inbuilt Functions In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Inbuilt Functions In C++

C++ is renowned for its efficiency. Learn how Inbuilt Functions In C++ enables low-level control and high-performance computing in the tutorial below.

In this guide, we will explore the built-in functions in C++ along with their various functionalities and illustrative examples. Prior to delving into the specifics of built-in functions, it is essential to have a solid understanding of functions in C++.

A function is a block of code that runs only upon being invoked. Parameters are the values passed into a function.

Functions are essential for performing specific operations and play a key role in reusing code. They enable you to define a set of instructions once and utilize it multiple times throughout your program.

I. String Functions in C++

There exist numerous string methods within C++. A selection of string functions in C++ includes:

1. strcat

The strcat function is employed to add the cstring header file to the program. It allows for the combination of two character arrays, with the second array being appended to the end of the first array through concatenation.

Syntax:

It has the following syntax:

Example

strcat(char_array1, char_array2);

The strcat function exclusively accepts character arrays as arguments, a key point to keep in mind. It is essential to note that string objects are not compatible with this function.

Example:

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:

Output

Radha Krishna.

Explanation:

In this instance, initially, the program populates two arrays of characters, str1 and str2, with respective information. Subsequently, the information stored in str2 is concatenated to the end of str1 utilizing the strcat function found in the cstring header. Ultimately, the cout statement is employed to display the merged string. The outcome of this program will be the string "Radha Krishna".

2. length

As suggested by its name, the length function is utilized to retrieve the size of a string.

Example:

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:

Output

The length of the string is: 23

Explanation:

In this instance, the code generates a string named str, calculates its length by employing the length method, and subsequently displays the length along with a corresponding message. Upon execution, the program will produce the subsequent output:

The string "Apple" represents a fruit name and consists of 23 characters.

3. capacity

The capacity function returns the current size of the allocated space for the string. This size can be equal to or larger than the string's actual size. Allocating extra space beyond the maximum string length allows for efficient handling of additional characters in the string.

Example:

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:

Output

The length of the string is: 10
The capacity of the string is: 15

Explanation:

In this instance, the code populates the string str_1 with the phrase "Seetha Ram" upon its initialization. Subsequently, it employs the methods length and capacity to display the size and storage capacity of the string.

The text "Seetha Ram" consists of 10 characters and has a capacity of 11 characters, providing room for potential expansion. These figures indicate the allocated memory for the string.

4. resize

The resize method is utilized to modify the length of a string.

Example:

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:

Output

The original string is: God is great.
The string after using resize is: God is g

Explanation:

In this instance, the programmer assigns the phrase "God is great." to a variable named str_1. Following this assignment, the original string is displayed, then the string is truncated to accommodate only the initial eight characters, and lastly, the truncated string is shown.

The text "God is great." shortens to "God is g" when limited to the initial eight characters through scaling.

5. begin

The begin method offers an iterator that points to the initial position of the string.

Example:

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 instance, the code populates the string str_1 with the text "Keep Learning" at the start. Subsequently, an iterator is defined and set to point to the beginning of the string. The script displays the initial character pointed to by the iterator, adds a space, proceeds to the subsequent character, and then displays that character.

This situation arises as the iterator sequentially references the characters "K" and "e" within the phrase "Keep Learning".

6. end

The end function offers an iterator that points to the end of the string in C++ tutorials. The iterator will always point to the null character.

Example:

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 starts by accepting the string "The Taj Mahal is a symbol of love". Next, the iterator is set to point to the final null character (0) in the string. After encountering the null character, the program iterates backward using the iterator to display the characters 'e' and 'v'.

This situation occurs because the iterator sequentially references the characters "e" and "v" in reverse order.

7. begin

The function rbegin signifies "reverse begin" and acts as a pointer to the last character of the string.

Example:

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 instance, the code initializes the string "Charminar" within the variable str. Subsequently, a reverse iterator named rit is defined and set to point to the last character of the string. By incrementing the reverse iterator, the system displays the characters "r", "a", and "n".

This occurs as the backward iterator sequentially references the characters "r", "a", and "n".

8. rend

The rend function is commonly employed with bidirectional data structures such as vectors, arrays, and similar containers that support reverse iteration using reverse iterators. Nevertheless, when it comes to std::string, its iterators only allow forward iteration.

Example:

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:

Output

gnivil trats

Explanation:

In this instance, the phrase "commence existing" is the content that the script assigns to the str_1 variable. Following this assignment, it loops through the characters within the string in a reversed sequence utilizing a for loop and a reverse iterator. The program leverages the reverse order to display each individual character.

The characters in the phrase "commence existing" shown in reverse order are the ones forming this result.

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:

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:

Output

The copied string is:  function

Explanation:

The setup code assigns the phrase "copy function is utilized for" to the variable str1. Following this, a segment of str1 (starting from the 5th index and extending 9 characters) is duplicated to a char array named str2, employing the copy method. The duplicated string is generated by appending a null terminator to str2, converting it into a proper C-style string.

This outcome matches the string "function " that was duplicated from str_1.

10. swap

The swap method is employed to exchange the positions of two strings.

Example:

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:

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 illustration, the code initializes the string variables fruit1 and fruit2 with the values "Banana" and "Sapota" respectively. Subsequently, it demonstrates the utilization of the swap function to interchange the data within these two strings. The program then displays both the original contents and the exchanged contents of the strings.

II. Mathematical functions in C++

There exist numerous mathematical functions available in C++. A selection of mathematical functions in C++ includes:

1. abs

The abs function returns the absolute value of the argument.

Example:

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:

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:

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:

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:

Output

The result is 15!

4. max

The fmax method is used to find the greater of two inputs.

Example:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

Output

The size of the vector is: 5
Ram's age: 30

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience