目录

algorithm::is_heap()

描述 (Description)

C ++函数std::algorithm::is_heap()测试给定序列是否为max heap。 它使用运算符“进行比较。

声明 (Declaration)

以下是std :: algorithm :: is_heap()函数形式std :: algorithm头的声明。

C++11

template <class RandomAccessIterator>
bool is_heap(RandomAccessIterator first, RandomAccessIterator last);

参数 (Parameters)

  • first - 随机访问迭代器到初始位置。

  • last - 随机访问迭代器到最终位置。

返回值

如果给定序列是最大堆,则返回true,否则返回false。

异常 (Exceptions)

如果元素比较或迭代器上的操作抛出异常,则抛出异常。

请注意,无效参数会导致未定义的行为。

时间复杂

线性。

例子 (Example)

以下示例显示了std :: algorithm :: is_heap()函数的用法。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
   vector<int> v = {3, 5, 2, 1, 4};
   bool result;
   result = is_heap(v.begin(), v.end());
   if (result == false)
      cout << "Given sequence is not a max heap." << endl;
   v = {5, 4, 3, 2, 1};
   result = is_heap(v.begin(), v.end());
   if (result == true)
      cout << "Given sequence is a max heap." << endl;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Given sequence is not a max heap.
Given sequence is a max heap.
↑回到顶部↑
WIKI教程 @2018