I want to understand what parameters are needed in c ++ in the function push_heap (). If you can explain everything in simple language.
- two random access iterators and a comparator if the standard comparator does not fit - AR Hovsepyan
|
1 answer
On the cppreference site there is an example - https://en.cppreference.com/w/cpp/algorithm/push_heap
In short, you just need two random iterators at the beginning and end of the container in which you need to make the average. The first elements are the finished hip, and the very last element is the "inserted" one, which is not in its place yet. And this function will put it "in place."
Example, copied.
#include <iostream> #include <algorithm> #include <vector> int main() { // исходный вектор std::vector<int> v { 3, 1, 4, 1, 5, 9 }; // сделали хип std::make_heap(v.begin(), v.end()); // распечатали std::cout << "v: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; // добавили элемент для "добавления" v.push_back(6); // снова вывели, что бы показать std::cout << "before push_heap: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; // исправили, переместив последний элемент на свое место. std::push_heap(v.begin(), v.end()); // и снова вывели std::cout << "after push_heap: "; for (auto i : v) std::cout << i << ' '; std::cout << '\n'; } - not just two iterators at the beginning and end of the container. And if the container does not support a random access iterator? .. For example, std :: list is not suitable, its iterators cannot be an argument for creating a heap - AR Hovsepyan
|