We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Priority Queues

on
Medium Priority
431 Views
How can  I turn a vector into a min-heap, then into a priority queue, Can I have an example?
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Top Expert 2012
Commented:
What do you mean by a 'min-heap'? In general, you can do that like

vector<int> v;

//... fill vector

priority_queue<int> q(v.begin(),v.end());

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Min-heap means that I have to store the vector in a heap that has the lowest number first,  I want to execute the character with the lowest time in my programm
CERTIFIED EXPERT
Top Expert 2012

Commented:
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());

Commented:
Will it be the lowest value be on top of the priority queue or do I need some type of compare function?
CERTIFIED EXPERT
Top Expert 2012

Commented:
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".

Commented:
Thanks that really helped
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile