Results 1 to 5 of 5

Thread: Use of the sort_heap() in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    31

    Use of the sort_heap() in C++

    I has just started for learning the C++ programming language. I think that the programming in C++ can be easier as compared to C language. I can have the idea about the C++ functions. So, I would like to know about the sort_heap() function of the C++ language. I also would like to know the Use of the sort_heap() function in the C++ coding. Can anyone knows about the sort_heap() function of C++ language.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    The sort_heap() in C++

    The sort_heap() function can be used in the C++ language to rearrange an elements in the given specified range of the heap as [fst,lst) in such a manner that they can form a range that can be sorted. The comparisons can be performed using less than operator "<" for the first version and cmp variable for the second version. The following can be the syntax of the sort_heap() in the C++ language :
    Code:
    template <class RndmAcsItrtr>
      void sort_heap ( RndmAcsItrtr fst, RndmAcsItrtr lst );

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Use of the sort_heap() in C++

    The following can be the parameters of the sort_heap() function in the C++ language :
    1. cmp : This parameter of the sort_heap() function of the C++ language can holds an object of the comparison function that can taking the two values of the similar type than that those can consisted in the given specified range.
    2. fst, lst : These parameters of the sort_heap() function of the C++ language can holds the random-Access iterators to the final and starting positions of the given specified range of the heap that can be sorted.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    The sort_heap()

    I can suggest you that to carefully go through the following code of lines that can demonstrates you how to use the sort_heap() function in the C++ programming as follows :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
      int mt[] = {10,20,30,5,15};
      vector<int> z(mt,mt+5);
      vector<int>::iterator x;
      make_heap (z.begin(),z.end());
      cout << "initial max heap   : " << z.front() << endl;
      pop_heap (z.begin(),z.end()); z.pop_back();
      cout << "max heap after pop : " << z.front() << endl;
      z.push_back(99); push_heap (z.begin(),z.end());
      cout << "max heap after push: " << z.front() << endl;
      sort_heap (z.begin(),z.end());
      cout << "final sorted range :";
      for (unsigned j=0; j<z.size(); j++) cout << " " << z[j];
      cout << endl;
      return 0;
    }
    Output:
    Code:
    initial max heap   : 30
    max heap after pop : 20
    max heap after push: 99
    final sorted range : 5 10 15 20 99

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Use of the sort_heap() in C++

    The sort_heap() function can converts the heap that can be defined by the range [strt,nd) into a given range that can be sorted. If the weak sequencing comparison function object can be given then the sort_heap() function can used instead of the less than operator to compare an elements. The sort_heap() function can have two properties as follows :
    1. The first element can always be the largest element.
    2. Elements can be deleted or inserted in logarithmic time.

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,713,560,648.20104 seconds with 16 queries