Sorting Vector In C++
Sorting a vector in C++ can be done by using std::sort(). It is defined
in<algorithm> header. To get a stable sort std::stable_sort is used. It
is exactly like sort() but maintains the relative order of equal elements.
Fig:Sorting Vector In C++ |
Program:
#include<iostream.h>
#include<vector.h>
#include<algorithm.h>
#include<conio.h>
void main()
{
vector<int> v={10,9,8,6,7,2,5,1};
cout<<”elements before sorting”<<endl;
for(const auto &i:v)
cout<<i <<endl;
cout<<”elements after sorting”<<endl;
sort(v.begin(),v.end());
for(const auto &i:v)
cout<<i <<endl;
getch();
}
Output:
Elements before sorting:
10
No comments:
Post a Comment