Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Tenable updates Vulnerability Priority Rating scoring method to flag fewer vulnerabilities as critical

      July 24, 2025

      Google adds updated workspace templates in Firebase Studio that leverage new Agent mode

      July 24, 2025

      AI and its impact on the developer experience, or ‘where is the joy?’

      July 23, 2025

      Google launches OSS Rebuild tool to improve trust in open source packages

      July 23, 2025

      EcoFlow’s new portable battery stations are lighter and more powerful (DC plug included)

      July 24, 2025

      7 ways Linux can save you money

      July 24, 2025

      My favorite Kindle tablet just got a kids model, and it makes so much sense

      July 24, 2025

      You can turn your Google Photos into video clips now – here’s how

      July 24, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Blade Service Injection: Direct Service Access in Laravel Templates

      July 24, 2025
      Recent

      Blade Service Injection: Direct Service Access in Laravel Templates

      July 24, 2025

      This Week in Laravel: NativePHP Mobile and AI Guidelines from Spatie

      July 24, 2025

      Retrieve the Currently Executing Closure in PHP 8.5

      July 24, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      FOSS Weekly #25.30: AUR Poisoned, Linux Rising, PPA Explained, New Open Source Grammar Checker and More

      July 24, 2025
      Recent

      FOSS Weekly #25.30: AUR Poisoned, Linux Rising, PPA Explained, New Open Source Grammar Checker and More

      July 24, 2025

      How to Open Control Panel in Windows 11

      July 24, 2025

      How to Shut Down Windows 11

      July 24, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Helpful Built-in Functions in C++ that All Devs Should Know

    Helpful Built-in Functions in C++ that All Devs Should Know

    July 22, 2025

    Built-in functions in C++ are those functions that are part of the C++ standard libraries. These functions are designed to provide common and essential functionality that is often required in programming.

    In this article, we will look at some of the most commonly used built-in functions in C++ so you can start using them in your code.

    What we’ll cover:

    1. The sqrt() Function

    2. The pow() Function

    3. The sort() Function

    4. The find() Function

    5. The binarysearch() Function

    6. The max() Function

    7. The min() Function

    8. The swap() Function

    9. The toupper() Function

    10. The tolower() Function

    The sqrt() Function

    You use the sqrt() function to determine the square root of the value of type double. It is defined inside the <cmath> header file.

    Syntax:

    sqrt (n)
    

    Parameter: This function takes only one parameter of type double which is a number we want to find the square root of.

    Return Type: The square root of the value of type double.

    Example Code

    Let’s look at an example so you can see how this function works:

    // C++ program to see the use of sqrt() function
    #include <cmath>     
    #include <iostream>  
    
    using namespace std;  
    int main()
    {
        double x = 100;
    
        double answer;
    
        // Use the sqrt() function to calculate the square root of the number
        answer = sqrt(x);
    
        // Print the result 
        cout << answer << endl;
    
        return 0;
    }
    

    Output:

    10

    The pow() Function

    You use the pow() function to find the value of the given number raised to some power. This function is also defined inside the <cmath> header file.

    Syntax:

    double pow(double x, double y);
    

    Parameters:

    • x: The base number.

    • y: The exponential power.

    Return Type: value of x raised to the power y.

    Example Code:

    Let’s look at an example to see how this works:

    // C++ program to see the use of the pow() function
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    int main()
    {
     // Declare an integer variable 'base' 
        int base = 5;
    
    // Declare an integer variable 'exponent' 
        int exponent = 3;
    
    // pow(5, 3) means 5^3 which is 5*5*5 = 125
    // Use the pow() function to calculate base raised to the power of exponent
        int answer = pow(base, exponent);
    
    // output the result
        cout << answer << endl;
    }
    

    Output:

    125

    The sort() Function

    The sort() function is part of STL’s <algorithm> header. It is a function template that you can use to sort the random access containers, such as vectors, arrays, and so on.

    Syntax:

    sort (arr , arr + n, comparator)
    

    Parameters:

    • arr: The pointer or iterator to the first element of the array.

    • arr + n: The pointer to the imaginary element next to the last element of the array.

    • comparator: The unary predicate function that is used to sort the value in some specific order. The default value of this sorts the array in ascending order.

    Return Value: This function does not return any value.

    Example Code:

    Let’s look at an example:

    #include <iostream>     
    #include <algorithm>    // Header file that includes the sort() function
    
    using namespace std;    
    
    int main()
    {
        // Declare and initialize an integer array with unsorted elements
        int arr[] = { 13, 15, 12, 14, 11, 16, 18, 17 };
    
        // Calculate the number of elements in the array
        int n = sizeof(arr) / sizeof(arr[0]);
    
        // Use the built-in sort() function from the algorithm library
        sort(arr, arr + n);
    
        // Print the sorted array using a loop
        for (int i = 0; i < n; ++i)
            cout << arr[i] << " ";  
    
        return 0;
    }
    

    Output:

    11 12 13 14 15 16 17 18

    The find() Function

    The find() function is also part of the STL <algorithm> library. You use this function to find a value in the given range. You can use it with both sorted and unsorted datasets as it implements a linear search algorithm.

    Syntax:

    find(startIterator, endIterator, key)
    

    Parameters:

    • startIterator: Iterates to the beginning of the range.

    • endIterator: Iterates to the end of the range.

    • key: The value to be searched.

    Return Value: If the element is found, then the iterator is set to the element. Otherwise, it iterates to the end.

    Example Code:

    Let’s look at an example to better understand how it works:

    // C++ program to see the the use of the find() function
    
    #include <algorithm>   // Required for the find() function
    #include <iostream>    
    #include <vector>      
    
    using namespace std;   
    
    int main()
    {
        // Initialize a vector 
        vector<int> dataset{ 12, 28, 16, 7, 33, 43 };
    
        // Use the find() function to search for the value 7
        auto index = find(dataset.begin(), dataset.end(), 7);
    
        // Check if the element was found
        if (index != dataset.end()) {
            // If found, print the position (index) by subtracting the starting iterator
            cout << "The element is found at the "
                 << index - dataset.begin() << "nd index";
        }
        else {
            // If not found
            cout << "Element not found";
       }
        return 0;
    }
    

    Output:

    The element is found at the 3rd index

    The binary_search() Function

    The binary_search() function is also used to find an element in the range – but this function implements binary search instead of linear search as compared to the find() function. It’s also faster than the find() function, but you can only use it on sorted datasets with random access. It’s defined inside the <algorithm> header file.

    Syntax:

    binary_search (starting_pointer , ending_pointer , target);
    

    Parameters:

    • starting_pointer: Pointer to the start of the range.

    • ending_pointer: Pointer to the element after the end of the range.

    • target: Value to be searched in the dataset.

    Return Value:

    • Returns true if the target is found.

    • Else return false.

    Example Code:

    Let’s check out an example to see how it works:

    // C++ program for the binary_search() function
    
    #include <algorithm>   
    #include <iostream>    
    #include <vector>      
    
    using namespace std;   
    
    int main()
    {
        // Initialize a sorted vector of integers
        vector<int> arr = { 56, 57, 58, 59, 60, 61, 62 };
    
        // binary_search() works only on sorted containers
        if (binary_search(arr.begin(), arr.end(), 62)) {
            // If found, print that the element is present
            cout << 62 << " is present in the vector.";
        }
        else {
            // If not found, print that the element is not present
            cout << 16 << " is not present in the vector";
        }
    
        cout << endl;
    }
    

    Output:

    62 is present in the vector.

    The max() Function

    You can use the std::max() function to compare two numbers and find the bigger one between them. It’s also defined inside the <algorithm> header file.

    Syntax:

    max (a , b)
    

    Parameters:

    • a: First number

    • b: Second number

    Return Value:

    • This function returns the larger number between the two numbers a and b.

    • If the two numbers are equal, it returns the first number.

    Example Code:

    Here’s an example:

    // max() function
    
    #include <algorithm>  
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare two integer variables
        int a = 8 ;
        int b = 10 ;
    
        // Use the max() function to find the larger number between a and b
        int maximum = max(a, b);
    
        // Display the result with a meaningful message
        cout << "The maximum of " << a << " and " << b << " is: " << maximum << endl;
    
        return 0;
    }
    

    Output:

    The maximum of 8 and 10 is: 10

    The min() Function

    You can use the std::min() function to compare two numbers and find the smaller of the two. It’s also defined inside the <algorithm> header file.

    Syntax:

    min (a , b)
    

    Parameters:

    • a: First number

    • b: Second number

    Return Value:

    • This function returns the smaller number between the two numbers a and b.

    • If the two numbers are equal, it returns the first number.

    Example Code:

    Here’s an example:

    // use of the min() function
    
    #include <algorithm>  // For the built-in min() function
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare two integer variables to store user input
        int a = 4 ;
        int b = 8 ;
    
        // Use the min() function to find the smaller 
        int smallest = min(a, b);
    
        // Display the result 
        cout << "The smaller number between " << a << " and " << b << " is: " << smallest << endl;
    
        return 0;
    }
    

    Output:

    The smaller number between 4 and 8 is: 4

    The swap() Function

    The std::swap() function lets you swap two values. It’s defined inside <algorithm> header file.

    Syntax:

    swap(a , b);
    

    Parameters:

    • a: First number

    • b: Second number

    Return Value: This function does not return any value.

    Example:

    Here’s how it works:

    //  use of the swap() function
    
    #include <algorithm>  // For the built-in swap() function
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        int firstNumber = 8 ;
        int secondNumber = 9 ;
    
    
        // Use the built-in swap() function to exchange values
        swap(firstNumber, secondNumber);
    
        // Display values after swapping
        cout << "After the swap:" << endl;
        cout << firstNumber << " " << secondNumber << endl;
    
        return 0;
    }
    

    Output:

    After the swap:

    9 8

    The tolower() Function

    You can use the tolower() function to convert a given alphabet character to lowercase. It’s defined inside the <cctype> header.

    Syntax:

    tolower (c);
    

    Parameter(s):

    • c: The character to be converted.

    Return Value:

    • Lowercase of the character c.

    • Returns c if c is not a letter.

    Example Code:

    Here’s how it works:

    // C++ program
    
    // use of tolower() function
    
    #include <cctype>     
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare and initialize a string with uppercase characters
        string str = "FRECODECAMP";
    
        for (auto& a : str) {
            a = tolower(a);
        }
    
        // Print the modified string 
        cout << str;
    
        return 0;
    }
    

    Output:

    freecodecamp

    The toupper() Function

    You can use the toupper() function to convert the given alphabet character to uppercase. It’s defined inside the <cctype> header.

    Syntax:

    toupper (c);
    

    Parameters:

    • c: The character to be converted.

    Return Value

    • Uppercase of the character c.

    • Returns c if c is not a letter.

    Example Code:

    Here’s how it works:

    // use of toupper() function
    
    #include <cctype>     
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare and initialize a string 
        string str = "freecodecamp";
    
        for (auto& a : str) {
            a = toupper(a);
        }
    
        // Output the converted uppercase string
        cout << str;
    
        return 0;
    }
    

    Output:

    FREECODECAMP

    Conclusion

    Inbuilt functions are helpful tools in competitive programming and in common programming tasks. These help in improving code readability and enhance the efficiency of code. In the above article, we discussed some very useful common inbuilt functions. Some common inbuilt functions are max(), min(), sort(), and sqrt(), etc. By using these inbuilt libraries, we can reduce boilerplate code and speed up the process of software development. These help in writing more concise, reliable, and maintainable C++ programs.

    If you enjoyed this article, you can check out more of my work here:
    Ayush Mishra’s Author Profile on TutorialsPoint

    And I’ve written some other tutorials about math and programming:

    How to Find the Area of a Square using Python?

    How to Calculate the Area of a Circle using C++?

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleVPS vs PaaS: How to Choose a Hosting Solution
    Next Article Data Structure and Algorithm Patterns for LeetCode Interviews

    Related Posts

    Development

    Blade Service Injection: Direct Service Access in Laravel Templates

    July 24, 2025
    Development

    This Week in Laravel: NativePHP Mobile and AI Guidelines from Spatie

    July 24, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-5440 – Linksys RE6500/RE6250/RE6300/RE6350/RE7000/RE9000 NTP Os Command Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    LWiAI Podcast #213 – Midjourney video, Gemini 2.5 Flash-Lite, LiveCodeBench Pro

    Artificial Intelligence

    CVE-2025-4517 – Tarfile Filter Arbitrary File Write

    Common Vulnerabilities and Exposures (CVEs)

    Aviatrix Cloud Controller Authentication Vulnerability Let Attackers Execute Remote Code

    Security

    Highlights

    CVE-2025-6717 – WordPress B1.lt Plugin SQL Injection Vulnerability

    July 18, 2025

    CVE ID : CVE-2025-6717

    Published : July 18, 2025, 6:15 a.m. | 4 hours, 42 minutes ago

    Description : The B1.lt plugin for WordPress is vulnerable to SQL Injection via the ‘id’ parameter in all versions up to, and including, 2.2.56 due to insufficient escaping on the user supplied parameter and lack of sufficient preparation on the existing SQL query. This makes it possible for authenticated attackers, with Subscriber-level access and above, to append additional SQL queries into already existing queries that can be used to extract sensitive information from the database.

    Severity: 6.5 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    The biggest miss in gaming handhelds just got hit with a major sale, so is it worth buying now?

    April 14, 2025

    At long last The Division 2 Battle for Brooklyn DLC is getting a proper reveal, and it’s happening soon

    April 16, 2025

    CVE-2025-30377 – Microsoft Office Use After Free Remote Code Execution Vulnerability

    May 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.