I am trying to implement all the functions STLmyself.
In the function, make_heapI need to swap two objects. Now, since I want it to be STL, how can I not change the list of arguments.
Therefore, I cannot declare the variable objectthat it points to first.
I also can not use swap without temporary variables
swap (RandomAccessIterator a,RandomAccessIterator b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
because the operators +and -cannot be overloaded for this object, which are indicated by two pointers.
Here is my code:
template <class RandomAccessIterator>
void make_heap (RandomAccessIterator first,RandomAccessIterator last)
{
int num_ele = (last - first) + 1;
for (int i=num_ele-1;i>=1;i--)
{
if (first[i] < first[(i-1)/2])
{
swap ();
}
}
}
PS: suppose that they objectsare comparable when <Operator.
source
share