Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 06-03-2010
Member
 
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 );
Reply With Quote
  #3  
Old 06-03-2010
Member
 
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
  #4  
Old 06-03-2010
Member
 
Join Date: May 2008
Posts: 2,293
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
Reply With Quote
  #5  
Old 06-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search



All times are GMT +5.5. The time now is 07:07 PM.