Javatpoint Logo
Javatpoint Logo

Inbuilt functions in C++

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:

The strcat() function only takes character arrays as parameters, which is crucial to remember. String objects cannot be used in the function.

Example:

Output:

Radha Krishna.

Explanation:

In this example, first, the code serves two character arrays, str_1, and str_2, with data. After that, the str_2 data is appended to the end of str_1 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:

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:

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:

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:

Output:

K e

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 that points to the string's end. The null character will always be where the iterator points.

Example:

Output:

ev

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:

Output:

ran

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:

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:

Output:

The copied string is:  function

Explanation:

The initialization code sets the string "copy function is used for" as the value of the variable str_1. After that, it copies a section of str_1 (from index 5 to 9 characters forward) to a character array called str_2, using the copy() function. The copied string is produced after the program inserts a null terminator to str_2 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:

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 fruit_1 and fruit_2 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:

Output:

7.8

2. ceil()

The whole number that follows the argument and is greater than or equal to it is returned by the ceil() function.

Example:

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:

Output:

The result is 15!

4. max()

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

Example:

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:

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:

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:

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:

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:

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:

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:

Output:

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA