ITeye博客

C++ STL学习(3)

zhava · 2013-04-02 09:41 · 编程语言 · Markdown

make_heap(first_it,last_it,comp)将 区间类的元素是否构成一个堆. 是返回true ,否则返回 false.

  1. bool is_heap(iterator start, iterator end, StrictWeakOrdering cmp); ->判断迭代器 区间类的元素在cmp条件下是否构成一个堆. 是返回true ,否则返回 false.

make_heap() :

原型如下 :

  1. void make_heap( random_access_iterator start, random_access_iterator end );

void make_heap( random_access_iterator start, random_access_iterator end, StrictWeakOrdering cmp ); ->以 迭代器 区间内的元素生成一个堆. 默认使用 元素类型 的 < 操作符 进行判断堆的类型, 因此生成的是大顶堆 . ->当使用了 版本2时, 系统使用 用户定义的 cmp 函数来构建一个堆 ->值得注意的是, make_heap 改变了 迭代器所指向的 容器 的值.

pop_heap() :

原型如下 :

  1. void pop_heap( random_access_iterator start, random_access_iterator end );
  2. void pop_heap( random_access_iterator start, random_access_iterator end, StrictWeakOrdering cmp ); ->pop_heap() 并不是真的把最大(最小)的元素从堆中弹出来. 而是重新排序堆. 它把首元素和末元素交换,然后将 = 4 ; Numbers = 10; Numbers = 70 ; Numbers = 10 ; Numbers = 30 ; Numbers = 69 ; Numbers = 96 ; Numbers = 100; // print content of Numbers cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ; // convert Numbers into a heap make_heap(Numbers.begin(), Numbers.end()) ; cout << "After calling make_heap\n" << endl ; // print content of Numbers cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ; //注意:该部分排序后将破坏堆的结构! // sort the heapified sequence Numbers sort_heap(Numbers.begin(), Numbers.end()) ; cout << "After calling sort_heap\n" << endl ; // print content of Numbers cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ;     //若此前调用了sort_heap,为了保持堆的性质,在调用push_heap之前必须调用该行,否则程序将崩溃。此处原文未调用,故原文测试程序有invalid heap错误。 // you need to call make_heap to re-assert the // heap property make_heap(Numbers.begin(), Numbers.end()) ; //insert an element in the heap Numbers.push_back(7) ; push_heap(Numbers.begin(), Numbers.end()) ; // you need to call make_heap to re-assert the // heap property make_heap(Numbers.begin(), Numbers.end()) ; cout << "After calling push_heap and make_heap\n" << endl ; // print content of Numbers cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ; // remove the root element from the heap Numbers pop_heap(Numbers.begin(), Numbers.end()) ; cout << "After calling pop_heap\n" << endl ; // print content of Numbers cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ; }

若利用STL中的堆函数来实现堆排序,则代码十分简洁。以下是代码: cout << "堆排序example:\n"; cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ; make_heap(Numbers.begin(), Numbers.end()); for(it = Numbers.end(); it!=Numbers.begin(); it--) pop_heap(Numbers.begin(), it); cout << "after 堆排序:\n"; cout << "Numbers { " ; for(it = Numbers.begin(); it != Numbers.end(); it++) cout << *it << " " ; cout << " }\n" << endl ;

事实上,sort_heap就是堆排序的一种实现。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。