priority queue and structs

May 14, 2011 at 8:17pm
hello
i have a pr. queue and a struct
my struct is:
1
2
3
4
5
6
struct Call
{
int CallNum;
long CallTime;
int CallLength;
};


and I use
1
2
priority_queue<Call> qu; //I also get an error msg here. if I use "queue<Call> qu;" then no problem
Call client[i]; //i is between 1-10 


I push my data into the queue like this:
 
qu.push(client[i]);

The main problem is when i wanna pop from queue i want it to pop the data which's CallLength is shortest.

And here is my algorithm:

http://img861.imageshack.us/img861/1161/18222051.png

Help pls
Last edited on May 15, 2011 at 3:46pm
May 14, 2011 at 9:02pm
Are you including the proper headers?
May 14, 2011 at 9:06pm
sure...
1
2
3
4
5
6
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <queue> 
May 14, 2011 at 10:14pm
anyone?? :\
May 15, 2011 at 12:44am
..............
May 15, 2011 at 1:03am
Could you post the error message?
May 15, 2011 at 5:50am
Let me guess
error: no match for 'operator<' in '__x < __y'


Edit: keep in mind that the order is higher priority first (maximum element)
Last edited on May 15, 2011 at 5:52am
May 15, 2011 at 12:21pm
Here is the error logs:
1
2
3
4
5
6
7
8
9
10

C:\main.cpp||In function 'int main()':|
C:\main.cpp|88|error: 'class std::priority_queue<Call, std::vector<Call, std::allocator<Call> >, std::less<Call> >' has no member named 'front'|
c:\..\bits\stl_function.h||In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Call]':|
c:\..\bits\stl_heap.h|303|instantiated from 'void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Call*, std::vector<Call, std::allocator<Call> > >, _Distance = int, _Tp = Call, _Compare = std::less<Call>]'|
c:\..\bits\stl_heap.h|434|instantiated from 'void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Call*, std::vector<Call, std::allocator<Call> > >, _Compare = std::less<Call>]'|
c:\..\bits\stl_queue.h|401|instantiated from 'std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = Call, _Sequence = std::vector<Call, std::allocator<Call> >, _Compare = std::less<Call>]'|
C:\main.cpp|39|instantiated from here|
c:\..\bits\stl_function.h|230|error: no match for 'operator<' in '__x < __y'|
||=== Build finished: 2 errors ===|


And here is the relevant lines:
1
2
Line 88: mu[counter]=qu.front();
Line 39: priority_queue<Call> qu;


So.. What can I do now? Thankss
May 15, 2011 at 3:41pm
It is not front() but top()

error: no match for 'operator<' in '__x < __y'
Provide a bool operator<(const Call&, const Call&); definition
(you could also use a comparator class)
May 15, 2011 at 3:50pm
1
2
3
4
5
struct compare_cost
{
    bool operator() ( const Call& a, const  Call& b) const
    { return a.cost < b.cost ; }
};


I found sth like that in daniweb but i dont know how to implement this into my code. can you help me about this pls?

btw, here is an outline of my algorithm for help:

http://img861.imageshack.us/img861/1161/18222051.png
May 15, 2011 at 4:08pm
Why I can't see the image?

So a class comparator then.
priority_queue<Call, vector<Call>, compare_cost> qu; For some reason the comparator is the third template parameter
What is the problem?
May 15, 2011 at 4:57pm
I dont know but the image is visible, i can see.

and all codes u advice me is not include "CallLength" variable. cos i want to pop from queue which struct's CallLength is shortest.

Here is another image:

http://img831.imageshack.us/img831/3128/clienty.png
May 15, 2011 at 5:47pm
this is what I see http://imageshack.us/img/blocked_login.jpg

i want to pop from queue which struct's CallLength is shortest.
Then compare the CallLength in compare_cost.
However the top of the priority queue is the biggest element (highest priority).
May 16, 2011 at 11:53am
http://i52.tinypic.com/30arq53.png

http://i52.tinypic.com/2yoycd2.png

I uploaded images into another server. I hope u can see now..
May 16, 2011 at 12:42pm
closed account (zwA4jE8b)
have you tried using a queue of Call*, I have noticed that usually when storing structs in a queue, vector, or array, it is good to store a pointer to that object, not the object itself.
May 16, 2011 at 2:33pm
@CreativeMFS no i have not. actullay im very new about C++. I have lots of lack about syntax...

i just wanna know that:
"How can I define a priority queue that its priority must be struct's CallLength property?" (ascending order)

If somebody can give an outline for this...? :\
May 16, 2011 at 3:03pm
output:
1 200 150
4 150 320
3 100 450
2 300 950


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <queue>

using namespace std;

struct Call
{
  Call( int callNum, long callTime, int callLength ) :
    CallNum( callNum ), CallTime( callTime ), CallLength( callLength ) { }

  int CallNum;
  long CallTime;
  int CallLength;
};

bool operator>( const Call& lhs, const Call& rhs ) {
  return lhs.CallLength > rhs.CallLength;
}

ostream& operator<<( ostream& os, const Call& c ) {
  os << c.CallNum << " " << c.CallTime << " " << c.CallLength;
  return os;
}

main()
{
  priority_queue< Call, vector<Call>, greater<Call> > q; 
  q.push( Call( 1, 200, 150 ));
  q.push( Call( 2, 300, 950 ));
  q.push( Call( 3, 100, 450 ));
  q.push( Call( 4, 150, 320 ));

  unsigned i=0, n=q.size();
  for ( i=0; i<n; ++i ) {
    cout << q.top() << endl;
    q.pop();
  }
}
Last edited on May 16, 2011 at 3:04pm
May 16, 2011 at 8:44pm
@kfmfe4 mate thank you very much... It works pretty well.
A last question
how i can print to screen just callTime for ex?
q.top(callTime) or something else? thanks a lot again:)
May 16, 2011 at 11:54pm
think about how a priority_queue works

if it's sorting by CallLength, how can it search by callTime?

you will need another data struct if you want to do a quick search by some other field - in fact, if you choose to go that route, I suggest that you follow CreativeMFS's suggestion and store pointers to your objects rather than the objects themselves, in order to maintain data consistency and speed

- data consistency because with one set of objects, you don't have problems caused by two sets of data going out of sync

- speed? think about how all STL objects work - by copy symantics - that means every time you insert/sort, an STL object has to copy-construct its objects when moving things around - it will always be cheaper to copy pointers than to copy-construct objects
May 17, 2011 at 12:15pm
buddy, i see what you want to say but i really dont know c++ syntax. if u help me pls? :\
Topic archived. No new replies allowed.