Top 10 Most Used Inbuilt C++ functions for Competitive Programming
Last Updated :
18 Apr, 2023
In this article, we will discuss about the 10 most used inbuilt functions of C++ which will help you to save time and make code concise as well during competitive programming.
List of top 10 inbuilt functions in C++
- pow()
- sqrt()
- min()
- max()
- swap()
- gcd()
- toupper()
- tolower()
- floor()
- ceil()
1. pow( )
This function helps to find the value of a number raised to another number. It always takes to values of double data type as parameters (Also accepts int data type) and the result is of double data type.
Header file:
pow() function is defined inside the cmath header file.
#include <cmath>
Syntax:
pow(base, exponent)
The result of this function will be baseexponent
Time Complexity: O(exponent).
Below are some examples to illustrate the working of pow() method in C++:
C++
// 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 result.
double result = pow(x, y);
// Printing the result upto 2
// decimal place
cout << fixed << setprecision(2) << result << endl;
return 0;
}
2. sqrt()
This function helps to find the square root of any number. It takes floating pointer or integer data type as an argument. The result is returned after rounding it according to the required data type.
Header file:
sqrt function is defined inside cmath header file.
#include <cmath>
Syntax:
sqrt(N);
Time Complexity: θ(log(N))
Below are some examples to illustrate the working of sqrt() method in C++:
C++
// CPP Program to demonstrate errors in double sqrt()
#include <cmath>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int x = 24;
double answer;
answer = sqrt(x);
// Printing square root of 24.
cout << answer << endl;
return 0;
}
3. min():
This function helps to find the minimum between two numbers. It takes two numbers of the same data type as arguments and returns the value of the minimum.
Header file:
This function is defined in algorithm header file.
#include <algorithm>
Syntax:
min(value1, value2);
Time Complexity: O(1)
Below are some examples to illustrate the working of min() method in C++:
C++
// C++ program to demonstrate the use of std::min
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 7;
cout << std::min(a, b) << "\n";
return 0;
}
4. max()
It helps in finding the maximum between two values. This function takes two values of the same data type as arguments and returns the value of the maximum element.
Header file:
This function is defined in algorithm header file.
#include <algorithm>
Syntax:
max(value1, value2);
Time Complexity: O(1).
Below are some examples to illustrate the working of max() method in C++:
C++
// C++ program to demonstrate use of max()
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a = 112, b = 123;
// Comparing a and b
cout << std::max(a, b) << "\n";
// Returns the first one if both the numbers
// are same
cout << std::max(7, 7);
return 0;
}
5. swap()
This function is used for swapping two numbers. It takes two values of the same data type as arguments and swaps their value.
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)
Below are some examples to illustrate the working of swap() method in C++:
C++14
// 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;
}
OutputValue of a before: 10
Value of b before: 20
Value of a now: 20
Value of b now: 10
6. gcd()
This function is used to find the GCD of two numbers. It takes two values of the same data type as arguments and returns the GCD of them.
Header file:
This function is defined in algorithm header file for C++14
#include <algorithm>
#include <numeric> (for C++17)
Syntax:
__gcd(value1, value2); [for C++14]
gcd(value1, value2); [for C++17]
Time Complexity: O(log(max(value1, value2)))).
Below are some examples to illustrate the working of gcd() method in C++:
C++
// CPP program to illustrate
// gcd function of C++ STL
#include <algorithm>
#include <iostream>
// #include<numeric> for C++17
using namespace std;
int main()
{
int a = 6, b = 20;
int ans = __gcd(a, b);
// int ans = gcd(a, b) for C++17
cout << "gcd(6, 20) = " << ans << endl;
return 0;
}
7. toupper()
This function is used for converting a lowercase character to uppercase.
Header file:
This function is defined in cctype header file
#include <cctype>
Syntax:
toupper('ch'); where ch is lower case character.
Time Complexity: O(1).
Below are some examples to illustrate the working of toupper() method in C++:
C++
// C++ program to illustrate toupper() method
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
int j = 0;
char str[] = "geekforgeeks";
char ch;
while (str[j]) {
ch = str[j];
putchar(toupper(ch));
j++;
}
return 0;
}
8. tolower():
This function is used for converting an uppercase character to lowercase.
Header file:
This function is defined in cctype header file.
#include <cctype>
Syntax:
tolower(ch); where ch is an uppercase character.
Time Complexity: O(1).
Below are some examples to illustrate the working of tolower() method in C++:
C++
// C++ program to illustrate tolower() method
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
int j = 0;
char str[] = "GEEKSFORGEEKS";
char ch;
while (str[j]) {
ch = str[j];
putchar(tolower(ch));
j++;
}
return 0;
}
9. floor():
This function returns the largest possible integer value which is less than or equal to a given argument. It takes a floating number as an argument and returns an integer value.
Header file:
floor function is defined in cmath header file
#include <cmath>
Syntax:
floor(value);
Time Complexity: O(1)
Below are some examples to illustrate the working of floor() method in C++:
C++
// C++ program to demonstrate floor function
#include <cmath>
#include <iostream>
using namespace std;
// Driver function
int main()
{
// Using floor function which returns
// floor of input value
cout << "Floor is: " << floor(2.3) << "\n";
cout << "Floor is: " << floor(-2.3) << "\n";
return 0;
}
OutputFloor is: 2
Floor is: -3
10. Ceil():
This function is just the opposite of floor(), It returns the smallest possible integer value which is greater than or equal to the given argument. It takes a floating value as an argument and returns an integer value.
Header file:
ceil function is defined in cmath header file
#include <cmath>
Syntax:
ceil(value);
Time Complexity: O(1)
Below are some examples to illustrate the working of ceil() method in C++:
C++
// C++ program to demonstrate ceil function
#include <cmath>
#include <iostream>
using namespace std;
// Driver function
int main()
{
// Using ceil function which return
// floor of input value
cout << " Ceil is: " << ceil(2.3) << "\n";
cout << " Ceil is: " << ceil(-2.3) << "\n";
return 0;
}
Output Ceil is: 3
Ceil is: -2
Similar Reads
Most Critical Mistakes & Tips in Competitive Programming
The moment a beginner hears this word, an image pops up in one's mind where students are sitting in a room full of Computers and coding some out of the world stuff, We'll to be honest it's nothing too hard to grasp so we will be proposing tips that help a programmer to level up faster. So let's begi
15+ min read
C++ tricks for competitive programming (for C++ 11)
We have discussed some tricks in the below post. In this post, some more tricks are discussed. Writing C/C++ code efficiently in Competitive programming Although, practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an uppe
5 min read
Which C++ libraries are useful for competitive programming?
C++ is one of the most recommended languages in competitive programming (please refer our previous article for the reason) C++ STL contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming
3 min read
Which Python Modules are useful for competitive programming?
In the previous article, we have discussed that C++, Java and Python are three most common languages for competitive programming. In this article, we are going to focus on the most important Python modules from competitive programming and interview preparation point of view. list : Dynamic Sized Arr
3 min read
Learning the art of Competitive Programming
Learning the art of Competitive Programming How to begin with Competitive Programming?Top 10 Algorithms and Data Structures for Competitive ProgrammingHow to prepare for ACM â ICPC?How to prepare for Google Asia Pacific University (APAC) Test ?Remaining Ahead in Competitive Programming:Master in com
2 min read
Fast I/O for Competitive Programming
In competitive programming, it is important to read input as fast as possible so we save valuable time. You must have seen various problem statements saying: " Warning: Large I/O data, be careful with certain languages (though most should be OK if the algorithm is well designed)" . The key for such
4 min read
25 Essential Concepts for Competitive Programming
Competitive Programming is a mental sport which enables you to code a given problem under provided constraints. The purpose of this article is to provide an overview of the most frequent tricks used in Competitive Programming. These 25 tricks will help you master competitive programming and solve th
15 min read
Some important shortcuts in Competitive Programming
One of the best ways to save time in competitive programming is by creating snippets. Snippets are reusable pieces of code that can be used easily for program creation or repeated tasks. It not only eases the efforts put into writing any code but also saves time for debugging as you know that is sto
2 min read
Top Programming Languages For Competitive Programming
Building an application, running a server, or even implementing a game needs a programming language as the foundation. There are almost more than 700 programming languages which are the most popular ones and this number will increase day by day. But, you don't need to learn all of them. Having a goo
12 min read
Java Generics to Code Efficiently in Competitive Programming
Templates are the foundation of generic programming, which involve writing code in a way that is independent of any particular type. These powerful tools can be used for writing our code effectively. Some cool tricks that may be used in Competitive Programming are given as follows: Fast Input/Output
9 min read