TechArena Community What is the nth_element() function
 User Name Remember Me? Password

#1
03-03-2010
 Member Join Date: Dec 2009 Posts: 27
What is the nth_element() function

I am interested in doing the programming. Thus, I had just started for learning the C++ programming language. I can also have the knowledge of the basic working of the functions. Hence, I want to know what is the nth_element() function of the C++ language. I also want to know how can I used the nth_element() function in the C++ programming. Can anyone knows about the nth_element() function of C++ language.
#2
03-03-2010
 Member Join Date: May 2008 Posts: 2,383
Re: What is the nth_element() function

The nth_element() function of the C++ language can re-arranges the elements in the given specified range [fst,lst), in a manner that an element at the resulting nth position can be an element that could be in that position in a sequence that can be sorted with all the elements that can starting it being smaller and all the elements followed by it greater than it. Neither the elements preceding it nor the elements following it can granted that can be ordered. The elements that can be compared using operator "<" for the first version and comp for the second version.
__________________
The FIFA Manager 2009 PC Game
#3
03-03-2010
 Member Join Date: Feb 2008 Posts: 1,848
nth_element() function

The nth_element() function can be used in the C++ language to arranged the elements in the sorted order that can be in the given specified range. The algorithm nth_element() function can be linear on average where M can be the size of the given range [strt, fnsh).The following can be the syntax of the nth_element() function in the C++ language :
Code:
```template <class RndmAcsItrtr>
void nth_element ( RndmAcsItrtr fst, RndmAcsItrtr nth,
RndmAcsItrtr lst );```
#4
03-03-2010
 Member Join Date: Jan 2008 Posts: 1,515
The nth_element() function

The following can be the parameters of the nth_element() function of the C++ language :
1. nth : This parameter of the nth_element() function can contains the random-Access iterator that can be used to point to the place in the given range [fst,lst) that can have an element that can be sorted.
2. fst, lst : These parameter of the nth_element() function can contains random-Access iterators to the final and starting positions of the given sequence to be used. The range used can be [fst,lst) which can contains all the elements that can be in between the first and last.
#5
03-03-2010
 Member Join Date: Apr 2008 Posts: 1,948
Re: What is the nth_element() function

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  }```
#6
03-03-2010
 Member Join Date: May 2008 Posts: 2,008
What is the nth_element() function

The nth_element() algorithm can arranges a collection again according to either operator<() or the function object comp. After the algorithm can applied the following can hold :
1. All the elements that can be prior to the nth position could also precede that position in an given ordered sequence.
2. All elements following the nth position could also follow that position in an given ordered sequence.
3. The element that could be in the nth position if the given sequence were completely sorted can be in the nth position.

 Tags: