Solved

Priority Queues

Posted on 2006-11-01
6
404 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?
0
Comment
Question by:MissProgrammer
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 17851235
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
0
 

Author Comment

by:MissProgrammer
ID: 17852787
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 17852915
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());
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:MissProgrammer
ID: 17852938
Will it be the lowest value be on top of the priority queue or do I need some type of compare function?
0
 
LVL 86

Expert Comment

by:jkr
ID: 17853105
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".
0
 

Author Comment

by:MissProgrammer
ID: 17853176
Thanks that really helped
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now