The following code of lines can help's you to learn the nth_element() function of the C++ language easily. The following code demonstrates you how the nth_element() function can be used in the C++ programming :
Code:
#pragma warning(disable: 4786)
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std ;
int main()
{
const int VCTR_SZ = 8 ;
typedef vector<int> IntVector ;
typedef IntVector::iterator IntVctrIt ;
IntVector Nos(VCTR_SZ) ;
IntVctrIt start, end, it ;
Nos[0] = 4 ;
Nos[1] = 10;
Nos[2] = 70 ;
Nos[3] = 30 ;
Nos[4] = 10;
Nos[5] = 69 ;
Nos[6] = 96 ;
Nos[7] = 100;
start = Nos.begin();
end = Nos.end();
cout << "Before calling nth_element\n" << endl ;
cout << "Nos { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
nth_element(start, start+4, end) ;
cout << "After calling nth_element\n" << endl ;
cout << "Nos { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ;
}
The above code of lines can produce the following Output :
Code:
Before calling nth_element
Nos { 4 10 70 30 10 69 96 100 }
After calling nth_element
Nos { 4 10 10 30 69 70 96 100 }
Bookmarks