Data Structures operations using STL(Standard Template Library)
Code: Cpp
#include<iostream>
#include<vector>
#include<algorithm>// Included for Algorithmic functions such as sort,max_element,binary_search etc
#include<stdlib.h>
using namespace std;
void main()
{
int i,number,array[20],choice,srch;
cout<<"\n Enter a number of elements:";
cin>>number;
cout<<"Enter the number one by one\n";
for(i=0;i<number;i++){
cin>>array[i];
}
vector <int>::iterator loop_iterate;//loop iterator
vector <int>::iterator max_iterate;// To store maximum element
vector <int>::iterator min_iterate;// To store minimum element
vector <int> vec_first(array,array+number);//vector created for our array
while(1)
{
cout<<"\n1.Sorting\n2.Searching\n3.Max/Min Element\n4.Exit\n";
cout<<"Enter ur choice:\n";
cin>>choice;
switch(choice)
{
case 1:
sort(vec_first.begin(),vec_first.end());
cout<<"\nAfter Sorting\n";
for(loop_iterate=vec_first.begin();loop_iterate<vec_first.end();loop_iterate++){
cout<<*loop_iterate<<endl;
}
break;
case 2:
cout<<"Enter the element to be searched:\n";
cin>>srch;
//Binary search works well for sorted array
//so u have to sort the array first and then apply binary search algorithm
sort(vec_first.begin(),vec_first.end());
if(binary_search(vec_first.begin(),vec_first.end(),srch))
cout<<"Element found";
else
cout<<"Element not found";
break;
case 3:
max_iterate=max_element(vec_first.begin(),vec_first.end());
cout <<"\n Maximum element present in the array is :"<<*max_iterate<<endl;
min_iterate=min_element(vec_first.begin(),vec_first.end());
cout <<"\n Minimum element present in the array is :"<<*min_iterate<<endl;
break;
case 4:
exit(0);
}
} //end of while loop
}


