473,545 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stl priority_queue organization?

4 New Member
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 2508
JosAH
11,448 Recognized Expert MVP
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
mistersteve
4 New Member
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 Recognized Expert MVP
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
mistersteve
4 New Member
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
3111
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<> a superior alternative. Why factors am I not considering here? Why in the general case would std::vector<> be best? Thanks, Dave
3
4458
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 doesn't seem to be anything like this for priority_queue. Also, I don't know anything about how priority_queue is implemented (specifically, MSVC++...
3
3999
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 class first. To make it simple, here is the toy code. myClass.h ------------------------------------------ #ifndef MYCLASS_H #define MYCLASS_H
9
28507
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 find methods for these which suprises me as both should be easily solvable with access to the underlying vector. Did I just miss these methods?...
18
5513
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.) It seems a priority_queue from the STL would work fine. However at some point, I am finished (even though the queue is not empty) and want to throw...
6
11180
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 emptying it? Right now I'm using the following code: while (!pq.empty()) { cout << setw(3) << pq.top().first << ": " << pq.top().second <<
8
4154
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>, vector<pair<int,int, cmphp;" thing. My question is how should I write the "cmp" function? I tried this one:
24
2548
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 timestamp. // just a wrapper around priority_queue pq = new MyPriorityQueue(); // I generate 10 events w/ a random timestamp for (int i = 0; i <...
5
3520
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 called Position with a comparator class called PositionCompare as follows: priority_queue<Position, vector<Position>, PositionCompare> _pQ; ...
0
7484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.