Results 1 to 6 of 6

Thread: What is the nth_element() function

  1. #1
    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. #2
    Join Date
    May 2008
    Posts
    2,389

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    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. #4
    Join Date
    Jan 2008
    Posts
    1,521

    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. #5
    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. #6
    Join Date
    May 2008
    Posts
    2,012

    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.

Similar Threads

  1. c++ equivalent function to the c-function 'sprintf
    By Dilbert in forum Software Development
    Replies: 6
    Last Post: 13-12-2011, 04:03 PM
  2. c# function equivalent to gettime function in javascript
    By Omaar in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:44 PM
  3. Replies: 5
    Last Post: 27-02-2010, 07:52 PM
  4. How does abstract function differs from virtual function?
    By Maddox G in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 11:32 AM
  5. Function keys don't function under windows XP
    By GunFighter in forum Hardware Peripherals
    Replies: 3
    Last Post: 08-04-2009, 11:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,665,373.19049 seconds with 17 queries