473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

stl priority_queue organization?

Ok, so I've just written my first program that uses the stl priority_queue. Everything went pretty well, except I have no idea how to get my stuff to "pop" out in the order I want! I know that i automatically organizes its contents from smaller to larger, but I am storing instances of a structure, and it seems to just toss them back out in the order they were entered. HOW can manipulate the order in which it returns my structure instances??

Any help would be greatly appreciated. Thanks everyone.
Apr 5 '07 #1
4 2506
JosAH
11,448 Expert 8TB
Ok, so I've just written my first program that uses the stl priority_queue. Everything went pretty well, except I have no idea how to get my stuff to "pop" out in the order I want! I know that i automatically organizes its contents from smaller to larger, but I am storing instances of a structure, and it seems to just toss them back out in the order they were entered. HOW can manipulate the order in which it returns my structure instances??

Any help would be greatly appreciated. Thanks everyone.
What exactly did you do? a) did you supply a comparison object to your
priority_queue? or b) did you overload the < operator in your struct?

kind regards,

Jos
Apr 5 '07 #2
Haha, I didn't do either one of those. I have no idea what that even means. But that sounds like a pretty good idea! Do you mind showing me how? (if that can be done without expending too much of your time, of course.)
Apr 5 '07 #3
JosAH
11,448 Expert 8TB
Haha, I didn't do either one of those. I have no idea what that even means. But that sounds like a pretty good idea! Do you mind showing me how? (if that can be done without expending too much of your time, of course.)
Well you have to have a notion of which struct has higher priority than another
struct. The one with the highest priority will come out of the heap first. If A < B
is true than struct A has higher priority than struct B.

All you have to do is overload the operator< for your struct. Something like this:
Expand|Select|Wrap|Line Numbers
  1. boolean operator<(struct& B) {
  2.    // this struct is A, so implement A < B
  3. }
  4.  
kind regards,

Jos
Apr 5 '07 #4
Well you have to have a notion of which struct has higher priority than another
struct. The one with the highest priority will come out of the heap first. If A < B
is true than struct A has higher priority than struct B.

All you have to do is overload the operator< for your struct. Something like this:
Expand|Select|Wrap|Line Numbers
  1. boolean operator<(struct& B) {
  2.    // this struct is A, so implement A < B
  3. }
  4.  
kind regards,

Jos

I am trying very hard to understand. Haha, I really am. I only have one struct. But I am storing in my priority_queue many different instances of that struct. Each instance contains the information about a given appointment (the purpose of this program is to keep and appointment book). So my struc looks something like this:

Expand|Select|Wrap|Line Numbers
  1. struct book {
  2.               string lastName, firstName, date, hours, minutes, ampm;
  3.               int idTag;
  4. };
  5.  
So when i run the program, it asks for information reguarding any number of patients (or whoever is making appointments to see me) I enter their first name, last name, the date and time of the appointment, then the program pushes that instance into my priority_queue, and I start entering information about the next appointment. Now, when I start to pop, I want it to spit out the appointments starting with the soonest, and moving down to list to the most distant one. This is why I included the int idTag in my struct. Each appointment has an "id number" so that they can be ordered so, if I have an appointment on November 5th, at 3:30pm, that instance's idTag will be 11051530.

Basically, what I can't figure out how to do is get my priority_queue to sort and pop my instances according one variable within each instance (i want it to look at each instance's idTag to decide what order they should be popped in). If i am going about this totally the wrong way, I'll be happy to recieve any correction! I just feel like I'm pretty close... maybe. heh.

Thanks for your patience,
mistersteve
Apr 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
3
by: Tino | last post by:
In using std::priority_queue, I'm concerned about the expense of memory allocation and copying as the priority_queue grows large. std::vector has reserve() to address this concern, though there...
3
by: zl2k | last post by:
hi, all Here is what I want to do: to wrap my self defined class in a shared_ptr and then insert it into the priority_queue. The priority_queue should pump the least element of the self defined...
9
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt...
18
by: J.M. | last post by:
I would like to use a data structure (e.g. from the STL) that always allows me to retrieve the largest element. (I want to push in elements, and remove the largest, push in further elements, etc.)...
6
by: Eric Lilja | last post by:
Hello, I have a the following priority_queue: priority_queue<pair<int, string pq; AFAICT, priority_queues doesn't support iterators. My question: is there a way to print its contents without...
8
by: thomas | last post by:
priority_queue usually uses the greater<intpredicate function. But as you know, we don't always use priority_queue<int>. Actually we may need the "priority_queue<pair<int,int>,...
24
by: Joe, G.I. | last post by:
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes and I put them on a priority_queue, but I don't know how to get them in priority. The priority is the event w/ the smallest...
5
card
by: card | last post by:
I was wondering if anyone has a definitive answer on whether the STL priority_queue is dynamic? What I mean by this is best illustrated by a simple example. Suppose I have a priority queue of class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.