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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
double x = 6.1, y = 4.8;
double result = pow (x, y);
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++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int x = 24;
double answer;
answer = sqrt (x);
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++
#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++
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a = 112, b = 123;
cout << std::max(a, b) << "\n" ;
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
#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(a, b);
cout << "Value of a now: " << a << endl;
cout << "Value of b now: " << b << endl;
return 0;
}
|
Output
Value 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++
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a = 6, b = 20;
int ans = __gcd(a, b);
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++
#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++
#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++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << "Floor is: " << floor (2.3) << "\n" ;
cout << "Floor is: " << floor (-2.3) << "\n" ;
return 0;
}
|
Output
Floor 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++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
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
Points to focus on while doing Competitive Programming
Competitive Programming is vital for oneâs development in the coding field. This article is going to discuss some basics points one should keep in mind while competing. Make a list of functions to perform tasks that are encountered frequently in questions and add them to your code in the form of a t
5 min read
getchar_unlocked() â Faster Input in C/C++ For Competitive Programming
getchar_unlocked() is similar to getchar() with the exception that it is not thread-safe. This function can be securely used in a multi-threaded program if and only if they are invoked when the invoking thread possesses the (FILE*) object, as is the situation after calling flockfile() or ftrylockfil
2 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
Must do Math for Competitive Programming
Competitive Programming (CP) doesnât typically require one to know high-level calculus or some rocket science. But there are some concepts and tricks which are sufficient most of the time. You can definitely start competitive coding without any mathematical background, but maths becomes essential as
15+ 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
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
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