Perform the heapsort with push_heap and pop_heap : vector push pop heap « vector « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » vector » vector push pop heap 
16.20.1.Perform the heapsort with push_heap and pop_heap
#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
   int a10 3102748195};

   std::vector< int > va, a + 10 )// copy of a
   std::vector< int > v2;
   std::ostream_iterator< int > outputcout, " " );

   for int i = 0; i < 10; i++ )
   {
      v2.push_backa] );
      std::push_heapv2.begin(), v2.end() );
      cout << "\nv2 after push_heap(a[" << i << "]): ";
      std::copyv2.begin(), v2.end(), output );
   }

   for int j = 0; j < v2.size(); j++ )
   {
      cout << "\nv2 after " << v2<< " popped from heap\n";
      std::pop_heapv2.begin(), v2.end() - j );
      std::copyv2.begin(), v2.end(), output );
   }
   cout << endl;
   return 0;
}
v2 after push_heap(a[0]): 3
v2 after push_heap(a[1]): 10 3
v2 after push_heap(a[2]): 10 3 2
v2 after push_heap(a[3]): 10 7 2 3
v2 after push_heap(a[4]): 10 7 2 3 4
v2 after push_heap(a[5]): 10 7 8 3 4 2
v2 after push_heap(a[6]): 10 7 8 3 4 2 1
v2 after push_heap(a[7]): 10 9 8 7 4 2 1 3
v2 after push_heap(a[8]): 10 9 8 7 4 2 1 3 5
v2 after push_heap(a[9]): 10 9 8 7 6 2 1 3 5 4
v2 after 10 popped from heap
9 7 8 5 6 2 1 3 4 10
v2 after 9 popped from heap
8 7 4 5 6 2 1 3 9 10
v2 after 8 popped from heap
7 6 4 5 3 2 1 8 9 10
v2 after 7 popped from heap
6 5 4 1 3 2 7 8 9 10
v2 after 6 popped from heap
5 3 4 1 2 6 7 8 9 10
v2 after 5 popped from heap
4 3 2 1 5 6 7 8 9 10
v2 after 4 popped from heap
3 1 2 4 5 6 7 8 9 10
v2 after 3 popped from heap
2 1 3 4 5 6 7 8 9 10
v2 after 2 popped from heap
1 2 3 4 5 6 7 8 9 10
v2 after 1 popped from heap
1 2 3 4 5 6 7 8 9 10
16.20.vector push pop heap
16.20.1.Perform the heapsort with push_heap and pop_heap
16.20.2.Using pop_back to Erase the Last Element
16.20.3.pop_back from a vector
16.20.4.Demonstrating the STL vector push_back functions
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.