vs2005 c++ heap使用push_head()异常invalid heap(bug)

来源:互联网 发布:mac批量安装字体 编辑:程序博客网 时间:2024/05/01 15:55

最近想使用STL的heap,我的vs2005 Team Edition For Software Developers版本是version 8.0.50727.42,.Net Framework Version 2.0.50727 .

然后看到微软官网的一段使用Heap的代码

 

  1. // alg_push_heap.cpp
  2. // compile with: /EHsc
  3. #include <vector>
  4. #include <algorithm>
  5. #include <functional>
  6. #include <iostream>
  7. int main( ) {
  8.    using namespace std;
  9.    vector <int> v1, v2;
  10.    vector <int>::iterator Iter1, Iter2;
  11.    int i;
  12.    for ( i = 1 ; i <= 9 ; i++ )
  13.       v1.push_back( i );
  14.    random_shuffle( v1.begin( ), v1.end( ) );
  15.    cout << "Vector v1 is ( " ;
  16.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  17.       cout << *Iter1 << " ";
  18.    cout << ")." << endl;
  19.    // Make v1 a heap with default less than ordering
  20.    make_heap ( v1.begin( ), v1.end( ) );
  21.    cout << "The heaped version of vector v1 is ( " ;
  22.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  23.       cout << *Iter1 << " ";
  24.    cout << ")." << endl;
  25.    // Add an element to the heap
  26.    v1.push_back( 10 );
  27.    cout << "The heap v1 with 10 pushed back is ( " ;
  28.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  29.       cout << *Iter1 << " ";
  30.    cout << ")." << endl;
  31.    push_heap( v1.begin( ), v1.end( ) );
  32.    cout << "The reheaped v1 with 10 added is ( " ;
  33.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  34.       cout << *Iter1 << " ";
  35.    cout << ")." << endl << endl;
  36.    // Make v1 a heap with greater than ordering
  37.    make_heap ( v1.begin( ), v1.end( ), greater<int>( ) );
  38.    cout << "The greater-than heaped version of v1 is/n ( " ;
  39.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  40.       cout << *Iter1 << " ";
  41.    cout << ")." << endl;
  42.    v1.push_back(0);
  43.    cout << "The greater-than heap v1 with 11 pushed back is/n ( " ;
  44.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  45.       cout << *Iter1 << " ";
  46.    cout << ")." << endl;
  47.    push_heap( v1.begin( ), v1.end( ), greater<int>( ) );
  48.    cout << "The greater than reheaped v1 with 11 added is/n ( " ;
  49.    for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  50.       cout << *Iter1 << " ";
  51.    cout << ")." << endl;
  52. }

发现竟然在push_heap()那里抛了异常。(invalid Heap)

然后无奈去了www.cplusplus.com也下了一段代码:

 

  1. // range heap example
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. int main () {
  7.   int myints[] = {10,20,30,5,15};
  8.   vector<int> v(myints,myints+5);
  9.   vector<int>::iterator it;
  10.   make_heap (v.begin(),v.end());
  11.   cout << "initial max heap   : " << v.front() << endl;
  12.   pop_heap (v.begin(),v.end()); v.pop_back();
  13.   cout << "max heap after pop : " << v.front() << endl;
  14.   v.push_back(99); push_heap (v.begin(),v.end());
  15.   cout << "max heap after push: " << v.front() << endl;
  16.   sort_heap (v.begin(),v.end());
  17.   cout << "final sorted range :";
  18.   for (unsigned i=0; i<v.size(); i++) cout << " " << v[i];
  19.   cout << endl;
  20.   return 0;
  21. }

这段代码没有抛异常,运行完好。仔细检查一番,发现可能是vector数组中的元素没有增多的缘故。于是将

 pop_heap (v.begin(),v.end()); v.pop_back();改为

 pop_heap (v.begin(),v.end()); 这样vector元素就在总数上多了一。

然后运行..相同的错误。

不知道vs2008版本上有没有相同的错误。