最近在研究C++标准看到make_heap的范例用法。
连结: http://en.cppreference.com/w/cpp/algorithm/make_heap
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v { 3, 1, 4, 1, 5, 9 };
std::cout << "initially, v: ";
for (auto i : v) std::cout << i << ' ';
std::cout << '\n';
std::make_heap(v.begin(), v.end());
std::cout << "after make_heap, v: ";
for (auto i : v) std::cout << i << ' ';
std::cout << '\n';
std::pop_heap(v.begin(), v.end());
auto largest = v.back();
v.pop_back();
std::cout << "largest element: " << largest << '\n';
std::cout << "after removing the largest element, v: ";
for (auto i : v) std::cout << i << ' ';
std::cout << '\n';
}
网站直接Run Code运行结果:
initially, v: 3 1 4 1 5 9
after make_heap, v: 9 5 4 1 1 3
largest element: 9
after removing the largest element, v: 5 3 4 1 1
觉得很奇怪的地方是,范例中排序的结果怪怪的。
after make_heap, v: 9 5 4 1 1 3 应该是 9 5 4 3 1 1才对吧?
自己又用VC2015运行一次,发现结果一样
是我哪边误解了吗??
恳请解答~