How to use headers for common algorithms in C++

In C++, headers or header files store the predefined functions. They contain the definition/declaration of functions, libraries, variables, classes, etc. Using headers, we don’t need to create numerous functions and algorithms from scratch.

Include the header

The syntax to include/import any library or function in C++ is given below:

//general sytax
#include <libraryName>
//or
#include <libraryName.h>
Syntax to include header

We can include any library in C++ by using the include keyword with a hash symbol # and specifying the library’s name in angle brackets.

Include the <algorithm> library

In this Answer, we’ll go through the <algorithm> library available in C++. To use the built-in algorithms, we need to include the <algorithm> header/library. The algorithm library is a part of the standard template library Provides general-purpose classes and functions (STL), which contains useful algorithms such as sorting, searching, etc. The syntax is given below to include the <algorithm> library:

#include <algorithm>

We’re going to use the <algorithm> header to simplify the usage of:

  • Searching algorithms

  • Sorting algorithms

Searching algorithms

Searching algorithms determine whether specific data exists or not. They’re also helpful in finding the location of data (if it exists). There are a lot of search algorithms available in the <algorithm> library.

The find() function

The find() function is a part of the <algorithm> library/header. It searches for an element and returns its first occurrence.

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int val = 20;
int array_1[] = {13, 17, 3, 29, 5};
int arr_size = sizeof(array_1) / sizeof(array_1[0]);
auto it = find(array_1, array_1 + arr_size, val);
if(*it == val)
cout<<"Found\n";
else
cout<<"Not found";
return 0;
}

Explanation

  • Line 1-3: Include the required libraries.

  • Line 9: Use find() function to search an element stored in a val variable.

  • Lines 10-13: Check whether the value exists by comparing the iterator value with the actual value.

The binary_search() function

The binary_search() function is also a part of <algorithm> library. It searches for an element in an ordered list/array.

#include <iostream>
using namespace std;
#include <algorithm>
int main() {
int array_1[] = {1000, 1200, 1500, 2000, 3000, 3500};
int arr_size = sizeof(array_1) / sizeof(array_1[0]);
bool found = binary_search(array_1, arr_size + array_1, 3000);
if (found)
cout<<"Found\n";
else
cout<<"Not found";
return 0;
}

Explanation

  • Line 1–3: Include the required libraries.

  • Line 9: Use the binary_search() function to search an element stored in the array arr.

  • Lines 11–14: Check if the value exists.

Sorting algorithms

The purpose of the sorting algorithm is to sort the given collection of data. Like search algorithms, sorting algorithms/functions can also be used with the help of the <algorithm> library, and sort() is one such function.

The sort() function

The sort() function takes two arguments, the starting and ending index of an array/list. It also accepts a third parameter, which specifies the sorting criteria.

#include <iostream>
using namespace std;
#include <algorithm>
int main()
{
int array_1[] = {13, 17, 3, 29, 5};
int arr_size = sizeof(array_1) / sizeof(array_1[0]);
sort(array_1, arr_size + array_1);
cout<<"The array after sort function: ";
for(auto m=0; m<arr_size; m++)
{
cout<<array_1[m]<<" ";
}
return 0;
}

Explanation

  • Line 1–3: Include the required libraries.

  • Line 9: Find the size of the array_1 array.

  • Line 10: Use the sort() function to sort the array_1 array.

  • Lines 12–15: Print the sorted array.

Other algorithms

The <algorithm> library also has numerous other algorithms available. Some of these algorithms are given below:

  • count()

  • move()

  • transform()

  • swap()

  • accumulate()

In conclusion, we can see that it’s possible to include headers/libraries in our code and use the functions available in that library without implementing them from scratch.

Copyright ©2024 Educative, Inc. All rights reserved