[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/algorithm/is_heap [Back]  [Original]

std::is_heap cppreference.com
cppreference.com

std::is_heap

cppreference.com

<metanoindex/>

 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
(C++20)
, ranges::copy, ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
( )
(C++11)
/
(C++11)
(C++17)

(C++17)
(C++17)
(C++17)
C
 
<tbody> </tbody>
template< class RandomIt > bool is_heap( RandomIt first, RandomIt last );
(1)
template< class RandomIt, class Compare > bool is_heap( RandomIt first, RandomIt last, Compare comp );
(2)
, [first, last).
:
Checks if the elements in range [first, last) are a heap.
Google.
. .
is_heap operator< , comp.
:
The first version of is_heap uses operator< to compare elements, whereas the second uses the given comparison function comp.
Google.
. .

first, last
:
the range of elements to examine
Google.
. .
comp (.. , Compare), true, "", .

:

bool cmp(const Type1 &a, const Type2 &b);

noexcept ( C++11) . const&, . ( const) Type1 Type2 ( , Type1& , Type1, Type1 ( C++11)). Type1 Type2 , RandomIt .

-
RandomIt RandomAccessIterator.

true ' , false .
:
true if the range is heap, false otherwise.
Google.
. .

first last
:
Linear in the distance between first and last
Google.
. .

' [f,l), :
:
A heap is a range of elements [f,l) that has the following properties:
Google.
. .
  • *f
    :
    *f is the largest element in the range
    Google.
    . .
  • std::push_heap()
    :
    a new element can be added using std::push_heap()
    Google.
    . .
  • std::pop_heap()
    :
    the first element can be removed using std::pop_heap()
    Google.
    . .
, .
:
The actual arrangement of the elements is implementation defined.
Google.
. .

#include <iostream>
#include <algorithm>

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';

    if (!std::is_heap(v.begin(), v.end())) {
        std::cout << "making heap...\n";
        std::make_heap(v.begin(), v.end());
    }

    std::cout << "after make_heap, v: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';
}

:

initially, v: 3 1 4 1 5 9
making heap...
after make_heap, v: 9 5 4 1 1 3

.

,
( ) []

Web Proxy Viewer  |  New URL  |  Original Page