 Home >>C++ Standard Template Library Tutorial(STL) >C++ Algorithm

# C++ Algorithm

### C++ Algorithm Library

In order to excel in the field of competitive programming having only knowledge about containers of STL is not enough and the programmers should be aware about the things that STL has to offer.

As a most popular library, STL possess a vast ocean of the algorithms that are generally for all the < algorithm > library functions.

Here is the list of the most widely used algorithms on vectors and that are known as the most useful in the Competitive Programming as depicted below:

### Non-Manipulating Algorithms

Function Description
sort(first_iterator, last_iterator) This algorithm is used to sort the provided vector.
reverse(first_iterator, last_iterator) This algorithm is used to reverse a vector.
*max_element (first_iterator, last_iterator) This algorithm is used to find the maximum element of a vector.
*min_element (first_iterator, last_iterator) This algorithm is used to find the minimum element of a vector.
accumulate(first_iterator, last_iterator, initial value of sum) This algorithm is used to do the summation of vector elements.
count(first_iterator, last_iterator,x) This algorithm is used to count the occurrences of x in vector.
find(first_iterator, last_iterator, x) This algorithm generally point towards the last address of vector ((name_of_vector).end()) if element is not present in vector.
binary_search(first_iterator, last_iterator, x) This algorithm is generally used to test whether x exists in sorted vector or not.
lower_bound(first_iterator, last_iterator, x) This algorithm is used to return an iterator pointing to the first element in the range [first,last) which has a value not less than 'x'.
upper_bound(first_iterator, last_iterator, x) This algorithm is used to return an iterator pointing to the first element in the range [first,last) which has a value greater than 'x'.

### Some Manipulating Algorithms

Function Description
arr.erase(position to be deleted) This algorithm is used to erase the selected element in vector and shifts and resizes the vector elements accordingly.
arr.erase(unique(arr.begin(),arr.end()),arr.end()) This algorithm is used to erase the duplicate occurrences in sorted vector in a single line.
next_permutation(first_iterator, last_iterator) This algorithm is used to modify the vector to its next permutation.
prev_permutation(first_iterator, last_iterator) This algorithm is used to modify the vector to its previous permutation.
distance(first_iterator,desired_position) This algorithm is used to return the distance of desired position from the first iterator.This function is very useful while finding the index.

### Example of C++ Algorithm

```#include
#include
#include
using namespace std;
int main()
{
//intitalize vector array
int arr[] = {10,5,20,30,40,10,50,60};
int num = sizeof(arr)/sizeof(arr);
vector vect(arr, arr+num);

// Sort the given array
sort(vect.begin(), vect.end());

//it  Returns the first Position of 10
auto f = lower_bound(vect.begin(), vect.end(), 20);

//It  Returns the last Position of 10
auto l = upper_bound(vect.begin(), vect.end(), 20);

cout << "Here is the  lower bound position : ";
cout << f-vect.begin() << endl;

cout << "Here is the upper bound position: ";
cout << l-vect.begin() << endl;

return 0;
}
```
Output :
Here is the lower bound position : 3
Here is the upper bound position: 4