pop_heap

Elimina il massimo elemento dall'inizio di un heap al successivo (- ultima posizione nell'intervallo e quindi costituisce una nuova heap gli elementi rimanenti.

template<class RandomAccessIterator>
   void pop_heap(
      RandomAccessIterator _First, 
      RandomAccessIterator _Last
   );
template<class RandomAccessIterator, class BinaryPredicate>
   void pop_heap(
      RandomAccessIterator _First, 
      RandomAccessIterator _Last,
      BinaryPredicate _Comp
   );

Parametri

  • _First
    Un iteratore di accesso casuale destinato alla posizione del primo elemento nell'heap.

  • _Last
    Un iteratore di accesso casuale destinato alla posizione una dopo l'elemento finale nell'heap.

  • _Comp
    Oggetto definito dall'utente di funzione predicativa in cui viene definito il concetto cui è minore di un'altra.Un predicato binario accetta due argomenti e restituisce true una volta soddisfatti e false una volta non soddisfatta.

Note

L'algoritmo pop_heap è l'opposto dell'operazione eseguita dall'algoritmo push_heap, in cui un elemento al successivo all'ultima posizione di un intervallo viene aggiunto a un heap costituita da elementi precedenti nell'intervallo, nel caso in cui l'elemento aggiunto all'heap è maggiore di tutti gli elementi già nell'heap.

Gli heap sono due proprietà:

  • Il primo elemento è sempre il più grande.

  • Gli elementi possono essere aggiunti o rimossi nel tempo logaritmico.

Gli heap sono modo ideale per implementare le code di priorità e utilizzate nell'implementazione dell'adattatore standard classe di priority_queuedella libreria di modelli.

l'intervallo fatto riferimento deve essere valido; tutti i puntatori devono essere dereferenceable e all'interno della sequenza dell'ultima posizione è raggiungibile da prima dall'aumento.

L'intervallo all'elemento appena aggiunto alla fine deve essere un heap.

Complessità è logaritmica, richiedendo al massimo i confronti del registro*_Last (– _First*).

Esempio

// alg_pop_heap.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

int main( )  {
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter1, Iter2;

   int i;
   for ( i = 1 ; i <= 9 ; i++ )
      v1.push_back( i );

   // Make v1 a heap with default less than ordering
   random_shuffle( v1.begin( ), v1.end( ) );
   make_heap ( v1.begin( ), v1.end( ) );
   cout << "The heaped version of vector v1 is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Add an element to the back of the heap
   v1.push_back( 10 );
   push_heap( v1.begin( ), v1.end( ) );
   cout << "The reheaped v1 with 10 added is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Remove the largest element from the heap
   pop_heap( v1.begin( ), v1.end( ) );
   cout << "The heap v1 with 10 removed is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl << endl;

   // Make v1 a heap with greater-than ordering with a 0 element
   make_heap ( v1.begin( ), v1.end( ), greater<int>( ) );
   v1.push_back( 0 );
   push_heap( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "The 'greater than' reheaped v1 puts the smallest "
        << "element first:\n ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Application of pop_heap to remove the smallest element
   pop_heap( v1.begin( ), v1.end( ), greater<int>( ) );
   cout << "The 'greater than' heaped v1 with the smallest element\n "
        << "removed from the heap is: ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;
}

Esempio di output

The heaped version of vector v1 is ( 9 5 8 4 1 6 7 2 3 ).
The reheaped v1 with 10 added is ( 10 9 8 4 5 6 7 2 3 1 ).
The heap v1 with 10 removed is ( 9 5 8 4 1 6 7 2 3 10 ).

The 'greater than' reheaped v1 puts the smallest element first:
 ( 0 1 6 3 2 8 7 4 9 10 5 ).
The 'greater than' heaped v1 with the smallest element
 removed from the heap is: ( 1 2 6 3 5 8 7 4 9 10 0 ).

Requisiti

intestazione: <algorithm>

Spazio dei nomi: deviazione standard

Vedere anche

Riferimenti

heap

Libreria di modelli standard