Reputation: 3245
Question about make_heap
function.
When making a heap in stl
using make_heap
it takes v.begin()
and v.end()
// [front, end)
but what if I don't want to add 10 in to the heap..
I thought I could accomplish this by using:
make_heap(v.begin(),v.end()-1);
But this isn't giving me any different output.. I tried using make_heap(v.begin(),v.end()-1);
instead of make_heap(v.begin(),v.end());
But output is the same...
What's happening here?..
Code #1:
#include <algorithm>
#include <vector>
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> v(myints,myints+10);
std::make_heap (v.begin(),v.end());
std::cout << "range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];
std::cout << '\n';
return 0;
}
Code #2:
#include <algorithm>
#include <vector>
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> v(myints,myints+10);
std::make_heap (v.begin(),v.end()-1);
std::cout << "range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];
std::cout << '\n';
return 0;
}
Both giving me same output.. (?)
Upvotes: 0
Views: 132
Reputation: 7962
This is not an answer per say, but I was unable to reproduce your results: I obtain two different outputs.
Program:
#include <algorithm>
#include <vector>
#include <iostream>
void code1()
{
int myints[] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> v(myints,myints+10);
std::make_heap (v.begin(),v.end());
std::cout << "range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];
std::cout << '\n';
}
void code2()
{
int myints[] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> v(myints,myints+10);
std::make_heap (v.begin(),v.end()-1);
std::cout << "range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];
std::cout << '\n';
}
// ------------------------------------------------------------------------
int main()
{
code1();
code2();
}
Compilation & Output:
john@---:/tmp$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
john@---:/tmp$ g++ -o so2 so2.cpp
john@---:/tmp$ ./so2
range : 10 9 7 8 5 6 3 1 4 2
range : 9 8 7 4 5 6 3 2 1 10
john@---:/tmp$
Upvotes: 1