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

MissProgrammer
on
Medium Priority
431 Views
Last Modified: 2011-09-20
How can  I turn a vector into a min-heap, then into a priority queue, Can I have an example?
Comment
Watch Question

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

See also http://www.sgi.com/tech/stl/priority_queue.html

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

Ask the Experts

Author

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
jkr
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());

Author

Commented:
Will it be the lowest value be on top of the priority queue or do I need some type of compare function?
jkr
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".

Author

Commented:
Thanks that really helped
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.