In this article, you will find out about stl calculation library in c++.

Are you an aggressive developer or enthusiastic software engineer then you should think about STL calculation library.

It contains a rich arrangement of calculations. On the off chance that you practice this well, you can nail any programming meeting.

Every single inbuilt calculation executed in this library were actualized in the best intricacy way. So utilizing those is superior to composing own code. It additionally spares time and code lucidness.

C++ STL Algorithm Library

There are a significant number of those calculations. We will see some of them which for the most part utilized.

To work with these we first need to incorporate calculation library. That is #include

In every single past article we found out about compartments, presently we can apply these calculations on those holders.

Since the fundamental aim of calculation is to recover data or to know some of the properties, these won’t do any adjustments on holder sizes or compartment stockpiling.

They simply use iterators to apply on holders.

min (a, b): This gives least estimation of an and b. Here conditions are both an and b must be same information type.

On the off chance that if an and b are equivalent it gives same worth.

max (a,b) : This gives most extreme estimation of an and b. Both an and b ought to be same information type.

sort (first_iterator , last_iterator): Sorts the components of a holder between given vectors.

Above sort arranging capacity used to sort holders as it were. Since they just have iterators. Be that as it may, for ordinary exhibits.

We need to utilize sort (array_begin, array_end):

Model program to appear above calculations:

```
//Standard library algorithms
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
cout << "min(10,3) gives " << min(10,3) << "\n";
cout << "min(5,5) gives " << min(5,5) << "\n";
cout << "min ('A', 'B') gives" << min ('A' , 'B') << "\n";
cout << endl << "max(10,3) gives " << max(10,3) << "\n";
cout << "max(5,5) gives " << max(5,5) << "\n";
cout << "max ('A', 'B') gives" << max ('A' , 'B') << "\n";
vector <int> v;
vector <int> :: iterator it;
for(int i=0; i<5;i++) v.push_back(10-i);
cout << endl << "Elements of vector before sort " << endl;
for(it= v.begin(); it!=v.end();it++) cout << *it << " ";
cout << endl;
cout << "Performing sort(v.begin(), v.end()) " << endl;
sort(v.begin(), v.end());
cout << "After above function vector elements are " << endl;
for(it= v.begin(); it!=v.end(); it++) cout << *it << " " ;
cout << endl;
int ar[5];
for(int i=0; i<5 ;i++) ar[i]= 15-i;
cout << endl << "Array elements are " << endl;
for(int i=0; i<5; i++) cout << ar[i] << " " ;
cout << endl;
cout << "Performing sort(ar+0, ar+5) " << endl;
sort(ar+0, ar+5);
cout << "After above operation array elements are " << endl;
for(int i=0;i<5; i++) cout << ar[i] << " ";
cout << endl;
return 0;
}
```

**Output**

```
min(10,3) gives 3
min(5,5) gives 5
min (‘A’, ‘B’) givesA
max(10,3) gives 10
max(5,5) gives 5
max (‘A’, ‘B’) givesB
Elements of vector before sort
10 9 8 7 6
Performing sort(v.begin(), v.end())
After above function vector elements are
6 7 8 9 10
Array elements are
15 14 13 12 11
Performing sort(ar+0, ar+5)
After above operation array elements are
11 12 13 14 15
```

discover(): This will discover given component present in the compartment or not. Information parameters for this are range and component.

The range contains two qualities, start of the range and completion of the range.

find() on clusters will be find(ar_base_address, ar_end, component);

So also find() on other stl holders will be find(iterator_to_1st, iterator_to_last, component);

One more thing we have to think about this find() is, whenever given component is available it focuses that component position, else it focuses to end of the compartment.

This we can use for checking reason.

tally (): This is same method for discover() calculation. Be that as it may, this profits number of times given component present in the given scope of components.

Same like find(), for exhibits we have to give the base address as range and for other STL holders like vector, dequeue, list, and so on give iterator positions as a range.

equivalent(): This is utilized to think about the scope of successions. We will give some range as a parameter to think about other range. It gives genuine if all components equivalent in the range else returns bogus.

For instance, if give a vector and a cluster ranges, equivalent gives genuine if and just if an ith component of vector and exhibit are the same. Where I am between a given range.

Model program to appear above calculations:

```
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int ar[5];
for(int i=0;i<5; i++) ar[i]= i*i;
cout << "Array contains 0, 1, 4, 9, 16" << endl;
cout << "Performing find(ar+0, ar+5, 44) " << endl;
int *position= find(ar+0, ar+5, 44);
if(position == ar+5){
cout << "Element not found in the array " << endl;
}
else{
cout << "Element found in the array " << endl;
}
vector <int> vec;
vector <int> :: iterator it;
for(int i=0; i<5; i++) vec.push_back(i*i);
cout << endl << "Vector contains elements 0, 1, 4, 9, 16" << endl;
cout << "Performing find (vec.begin(), vec.end(), 4) " << endl;
it = find (vec.begin(), vec.end(), 4);
if(it == vec.end()){
cout << "Element not found in the vector " << endl;
}
else{
cout << "Element found in the vector " << endl;
}
ar[0]= vec[0]= 2;
ar[1]= vec[1]= 3;
ar[2]= vec[2]= 2;
ar[3]= vec[3]= 2;
ar[4]= vec[4]= 5;
cout << endl << "Array and vector contains elements 2, 3, 2, 2, 3" << endl;
cout << "Performing count (ar+0, ar+5, 2) on array " << endl;
int cnt= count(ar+0, ar+5, 2);
cout << "2 present " << cnt << " times in the array" << endl;
cout << "Performing count (vec.begin(), vec.end(), 3) on vector " << endl;
cnt= count(vec.begin(), vec.end(), 3);
cout << "3 present " << cnt << " times in the vector " << endl;
cout << endl << "elements of array are " << endl;
for(int i=0; i<5; i++) cout << ar[i] << " " ;
cout << endl;
cout << "elements in the vector are " << endl;
for(it=vec.begin(); it!=vec.end(); it++) cout << *it << " ";
cout << endl;
cout << "Now performing equal(vec.begin(), vec.end(), ar) " << endl;
bool chk = equal(vec.begin(), vec.end(), ar);
if(chk)
cout << "Both sequences are equal "<< endl;
else
cout << "Givens sequences are not equal " << endl;
return 0;
}
```

**Output**

```
Array contains 0, 1, 4, 9, 16
Performing find(ar+0, ar+5, 44)
Element not found in the array
Vector contains elements 0, 1, 4, 9, 16
Performing find (vec.begin(), vec.end(), 4)
Element found in the vector
Array and vector contains elements 2, 3, 2, 2, 3
Performing count (ar+0, ar+5, 2) on array
2 present 3 times in the array
Performing count (vec.begin(), vec.end(), 3) on vector
3 present 1 times in the vector
elements of array are
2 3 2 2 5
elements in the vector are
2 3 2 2 5
Now performing equal(vec.begin(), vec.end(), ar)
Both sequences are equal
```

reverse(start_, end_): This calculation turn around the components in the given range.

accumulate(start_, end_, initial_sum ): Accumulate has utilized to entirety all components in the given run. It adds components to introductory aggregate which additionally we indicate as a parameter to this collect capacity.

To utilize this gathering work we ought to incorporate numeric library. i.e #include

separation( start_, position): This capacity used to discover how much good ways from the given iterator position to the given position. Position may be anything determined by the client.

For instance, the position might be the least component, of a most extreme component of the vector/compartment.

Model program to appear above capacities:

```
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric> // This is for accumulate function
using namespace std;
int main(){
vector <int> vec;
vector <int> :: iterator it;
for(int i=0; i<5; i++) vec.push_back(i+5);
cout << "Vector contains elements " ;
for(it=vec.begin(); it!= vec.end(); it++) cout << *it << " ";
cout << endl;
cout << "Performing reverse(vec.begin(), vec.end()) " << endl;
reverse(vec.begin(), vec.end());
cout << "Now vector elements are " ;
for(it=vec.begin(); it!= vec.end(); it++) cout << *it << " ";
cout << endl;
cout << endl << "Performing accumulate (vec.begin(), vec.end(), 0) " << endl;
int total= accumulate(vec.begin(), vec.end(), 0);
cout << "Total of vector elements using accumulate function is " << total << endl;
cout << endl << "Finding distance from starting of vector to maximum element of the vector " << endl;
cout << "For that performing distance(vec.begin(), max(vec.begin(), vec.end()) " << endl;
int dist= distance (vec.begin(), max(vec.begin(), vec.end() ));
cout << "The distance to maximum element from starting of the iterator is " << dist << endl;
return 0;
}
```

**Output**

```
Vector contains elements 5 6 7 8 9
Performing reverse(vec.begin(), vec.end())
Now vector elements are 9 8 7 6 5
Performing accumulate (vec.begin(), vec.end(), 0)
Total of vector elements using accumulate function is 35
Finding distance from starting of vector to maximum element of the vector
For that performing distance(vec.begin(), max(vec.begin(), vec.end())
The distance to maximum element from starting of the iterator is 5
```

next_permuatation(start_ , end_ ): As I said the beginning of this article, STL is a companion to aggressive software engineer, producing stages is one of the fundamental undertakings in certain inquiries.

By utilizing this we can produce the next change of given succession between the components given a determined range.

prev_permutation (start_, end_ ): This gives a past change of the succession.

Model program to appear above capacities:

```
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
vector <int> vec;
vector <int> :: iterator it;
for(int i=1; i<=5; i++) vec.push_back(i);
cout << "Elements in the vector are " ;
for(it = vec.begin(); it!= vec.end(); it++) cout << *it << " ";
cout << endl;
cout << "next_permutation (vec.begin(), vec.end()) gives " << endl;
next_permutation(vec.begin(), vec.end());
for(it = vec.begin(); it!= vec.end(); it++) cout << *it << " ";
cout << endl;
cout << "prev_permutation (vec.begin(), vec.end()) gives " << endl;
prev_permutation(vec.begin(), vec.end());
for(it = vec.begin(); it!= vec.end(); it++) cout << *it << " ";
cout << endl;
return 0;
}
```

**Output**

```
Elements in the vector are 1 2 3 4 5
next_permutation (vec.begin(), vec.end()) gives
1 2 3 5 4
prev_permutation (vec.begin(), vec.end()) gives
1 2 3 4 5
```

lower_bound(start_, end_ , ele ): Returns an iterator indicating the main component in the range which has a component at the very least ele.

upper_bound(start_, end_ ,ele ): Returns an iterator indicating the principal component in the given range which has component more noteworthy than ele.

These two capacities utilized in performing paired inquiry activity:

Model program to appear above capacities:

```
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int ar[5]= {10, 20, 30, 40, 50};
vector <int> vec (ar, ar+5) ;
vector <int> :: iterator it;
sort(vec.begin(), vec.end()); // Sorting must need for lower_bound, upper_bound and binary search
cout << "Elements in the vector are " ;
for(it = vec.begin(); it!= vec.end(); it++) cout << *it << " ";
cout << endl;
it = lower_bound(vec.begin(), vec.end(), 25);
cout << "Lower bound for 25 is at position " << it- vec.begin() << endl;
it = upper_bound(vec.begin(), vec.end(), 35);
cout << "upper bound for 35 is at position " << it- vec.begin() << endl;
return 0;
}
```

**Output**

```
Elements in the vector are 10 20 30 40 50
Lower bound for 25 is at position 2
upper bound for 35 is at position 3
```

Remark beneath in the event that you have any questions identified with above C++ stl calculation library.