MissProgrammer
asked on
Priority Queues
How can I turn a vector into a min-heap, then into a priority queue, Can I have an example?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Then sort (http://www.sgi.com/tech/stl/sort.html) the vector first:
vector<int> v;
//... fill vector
sort(v.begin(),v.size());
priority_queue<int> q(v.begin(),v.end());
vector<int> v;
//... fill vector
sort(v.begin(),v.size());
priority_queue<int> q(v.begin(),v.end());
ASKER
Will it be the lowest value be on top of the priority queue or do I need some type of compare function?
Yes, see check the 'sort' example on http://www.sgi.com/tech/stl/sort.html
int A[] = {1, 4, 2, 8, 5, 7};
const int N = sizeof(A) / sizeof(int);
sort(A, A + N);
copy(A, A + N, ostream_iterator<int>(cout , " "));
// The output is " 1 2 4 5 7 8".
int A[] = {1, 4, 2, 8, 5, 7};
const int N = sizeof(A) / sizeof(int);
sort(A, A + N);
copy(A, A + N, ostream_iterator<int>(cout
// The output is " 1 2 4 5 7 8".
ASKER
Thanks that really helped
ASKER