目录

algorithm::is_heap_until()

描述 (Description)

C ++函数std::algorithm::is_heap_until()查找序列中违反最大堆条件的第一个元素。 它使用二进制函数进行比较。

声明 (Declaration)

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

C++11

template <class RandomAccessIterator>
template <class RandomAccessIterator, class Compare>
RandomAccessIterator is_heap_until(RandomAccessIterator first,
   RandomAccessIterator last Compare comp);

参数 (Parameters)

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

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

  • comp - 一个二进制函数,它接受两个参数并返回bool。

返回值

返回违反最大堆条件的第一个元素的迭代器。 如果整个序列是有效的最大堆,那么它返回最后一个

异常 (Exceptions)

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

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

时间复杂

线性。

例子 (Example)

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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool ignore_case(char a, char b) {
   return (tolower(a) == tolower(b));
}
int main(void) {
   vector<char> v = {'E', 'd', 'C', 'b', 'A'};
   auto result = is_heap_until(v.begin(), v.end());
   cout << char(*result) << " is the first element which "
        << "violates the max heap." << endl;
   v = {5, 4, 3, 2, 1};
   result = is_heap_until(v.begin(), v.end(), ignore_case);
   if (result == end(v))
      cout << "Entire range is valid heap." << endl;
   return 0;
}

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

d is the first element which violates the max heap.
Entire range is valid heap.
↑回到顶部↑
WIKI教程 @2018