Friday, 30 June 2017

STL Algorithm

Algorithm Used in Vector

(i) Non-Manipulating Algorithms:
          1. sort(first_iterator,last_iterator)-To sort a given vector.
          2. reverse(first_iterator,last_iterator)-To reverse a given vector.
          3. *max_element(first_iterator,last_iterator)-To find the maximum element of a vector
          4. *min_element(first_iterator,last_iterator)-To find the minimum element of a vector 
          5. accumulate(first_iterator,last_iterator.initial value of sum)-Does the summation of vector elements.
          6. count(first_iterator,last_iterator,x)-Count the occurrence in vector.
          7. find(first_iterator,last_iterator,x)-Points the last address of the vector if the element is not found

Sample Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
vector<int>v;
int i,a,n,w;
cin>>n; //Number of element in the vector
for(i=0;i<n;i++){
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
cout<<"After sorting in Ascending order:\n";
for(auto x:v)cout<<x<<' ';cout<<endl;
reverse(v.begin(),v.end());
cout<<"\nAfter reversing:\n";
for(auto x:v)cout<<x<<' ';cout<<endl;
cout<<"\nMaximum element in the vector:\n";
cout<<*max_element(v.begin(),v.end())<<endl;
cout<<"\nMinimum element in the vector:\n";
cout<<*min_element(v.begin(),v.end())<<endl;
cout<<"\nSummation of the elements in the vector:\n";
cout<<accumulate(v.begin(),v.end(),0)<<endl;
cout<<"\nOccurrence of the a number\n";
cout<<count(v.begin(),v.end(),2)<<endl;
cout<<"\nFinding the position of a number in the vector\n";
if(find(v.begin(),v.end(),2)!=v.end()){
cout<<"Element found at\n";
auto q=find(v.begin(),v.end(),2);
cout<<q-v.begin()<<endl;
}
else cout<<"Element not found\n";
return 0;
}
Input:
7
1 3 2 4 5 2 2
Output:
After sorting in Ascending order:
1 2 2 2 3 4 5
After reversing:
5 4 3 2 2 2 1
Maximum element in the vector:
5
Minimum element in the vector:
1
Summation of the elements in the vector:
19
Occurrence of the a number
3
Finding the position of a number in the vector
Element found at
3
       
          8. binary_search(first_iterator,last_iterator,x)-Tests whether x exists in the sorted vector or not!
          9. lower_bound(first_iterator,last_iterator,x)- The leftmost position in which 'x' can be inserted keeping the vector sorted.
          10. upper_bound(first_iterator,last_iterator,x)- The rightmost position in which 'x' can be inserted keeping the vector sorted.

Sample Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
vector<int>v;
int n,i,a;
cin>>n;
for(i=0;i<n;i++){
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
auto low=lower_bound(v.begin(),v.end(),20);
auto up=upper_bound(v.begin(),v.end(),20);
cout<<"The lower bound is at position: "<<low-v.begin()<<endl;
cout<<"The upper bound is at position: "<<up-v.begin()<<endl;
return 0;
}
Input:
8
5 10 15 20 20 23 42 45
Output:
The lower bound is at position: 3
The upper bound is at position: 5
view raw upperlower.cpp hosted with ❤ by GitHub
       
 (ii) Manipulating Algorithms:
           1. vect.erase(position to be deleted)-This erases the selected element and shifts and resizes the vector.
           2. vect.erase(unique(vect.begin(),vect.end()),vect.end())-This erases thye duplicate occurrence in the sorted vector.
           3. next_permutation(first_iterator,last_iterator)-This modified vector to it's next permutation.
           4. prev_permutation(first_iterator,last_iterator)-This modified vector to it's previous permutation.

Sample Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
vector<int>vect;
int n,i,a;
cin>>n;
for(i=0;i<n;i++){
cin>>a;
vect.push_back(a);
}
sort(vect.begin(),vect.end());
cout<<"After sorting in Ascending order\n";
for(auto x:vect) cout<<x<<' '; cout<<endl;
vect.erase(vect.begin()+1);
cout<<"After removing 1th element\n";
for(auto x:vect)cout<<x<<' '; cout<<endl;
vect.erase(unique(vect.begin(),vect.end()),vect.end());
cout<<"After removing duplicate elements\n";
for(auto x:vect)cout<<x<<' '; cout<<endl;
next_permutation(vect.begin(),vect.end());
cout<<"After performing next permutation:\n";
for(auto x:vect) cout<<x<<' '; cout<<endl;
prev_permutation(vect.begin(),vect.end());
cout<<"After performing prev permutation:\n";
for(auto x:vect) cout<<x<<' '; cout<<endl;
return 0;
}
Input:
7
4 3 1 2 7 1 1
Output:
After sorting in Ascending order
1 1 1 2 3 4 7
After removing 1th element
1 1 2 3 4 7
After removing duplicate elements
1 2 3 4 7
After performing next permutation:
1 2 3 7 4
After performing prev permutation:
1 2 3 4 7

Updating..............

No comments:

Post a Comment