C++ std::make_heap with std::array

C++ std::make_heap with std::array

std::make_heap is defined inside the header file <algorithm> .

std::make_heap has two versions,

default (1) template <class RandomAccessIterator> void make_heap (RandomAccessIterator first, RandomAccessIterator last); custom (2) template <class RandomAccessIterator, class Compare> void make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp );

Rearranges the elements in the range [first,last) in such a way that they form a heap.
A heap is a way to organize the elements of a range that allows for fast retrieval of the element with the highest value at any moment (with pop_heap), even repeatedly, while allowing for fast insertion of new elements (with push_heap).

The element with the highest value is always pointed by first. The order of the other elements depends on the particular implementation, but it is consistent throughout all heap-related functions of this header.

The elements are compared using operator< (for the first version), or comp (for the second): The element with the highest value is an element for which this would return false when compared to every other element in the range.

first, last
       Random-access iterators to the initial and final positions of the sequence to be transformed into a heap. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable.

        Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool.
        The value returned indicates whether the element passed as first argument is considered less than the second in the specific strict weak ordering it defines.
        The function shall not modify any of its arguments.
        This can either be a function pointer or a function object.

Return value:

Container used here: std::array, is a container that encapsulates fixed size arrays.

#include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <array> // std::array int main () { std::array<int, 5> v{50, 20, 30, 10, 40}; std::make_heap (v.begin(),v.end()); //heap created std::sort_heap (v.begin(),v.end()); //sort created heap std::cout << "sorted heap :"; for (int i=0; i<v.size(); i++) std::cout << ' ' << v[i]; return 0; }

++ -std=c++11 -o main *.cpp

sorted heap : 10 20 30 40 50

More Articles of Nagendra Bhat:

Name Views Likes
C++ std::swap with std::array 547 20
C++ std::max_element with std::multiset 918 19
C++ std::min_element with std::list 621 17
C++ std::swap with std::vector 604 20
C++ program to diagonal sum of a binary tree 437 20
C++ std::max_element with std::list 733 18
C++ std::swap with std::deque 467 17
C++ Program to remove duplicate elements from std::list of char pointers 530 27
C++ std::iter_swap with std::list 659 17
C++ std::max_elemnt with std::set 411 14
C++ std::min_element with std::vector 683 17
C++ std::is_nothrow_move_assignable 412 13
C++ std::min_element with std::deque 1134 22
C++ std::is_nothrow_copy_constructible 542 25
std::min_element with std::multiset 1015 18
C++ std::make_heap with std::deque 1462 27
C++ std::max_element with std::array 538 20
C++ std::iter_swap with std::vector 479 16
C++ min_element with std::set 999 14
std::min_element with std::multiset 499 19
C++ std::iter_swap with std::deque 662 24
C++ std::max_element with std::forward_list 489 24
C++ std::min_element with std::array 586 22
C++ std::make_heap with std::vector 777 13
C++ std::make_heap with std::array 1382 21
C++ std::min_element with std::forward_list 456 19
C++ std::is_nothrow_default_constructible 520 24
C++ std::iter_swap with std::array 531 15
C++ std::max_element with std::vector 627 19
C++ std::swap with std::list 547 25
C++ program to convert a given binary tree to doubly linked list 443 14
C++ program to replace each node in binary tree with the sum of its inorder predecessor and successor 993 19
C++ thread std::thread::detach 700 19
C++ std::max_element with std::deque 5225 14