Sep 26, 2012 at 10:25am UTC
Hi all, i am doing the following.
struct Adjacent
{
string name1;
int distance1,fare1;
};
inline bool operator<(const Adjacent &m, const Adjacent &n)
{
return (m.distance1 < n.distance1) && (m.fare1 < n.fare1);
}
I want to sort the two queue's one by fare and other by distance at same time or in same program. May be i need to return boolean operator( if i am right ) for each operation. I tried some combination but dint work any.
in the case above it displays the result according to fare sort.
Someone please help me for this ASAP.. Thanks alot.
Sep 26, 2012 at 11:21am UTC
You need to pass a sort predicate to each container, rather than using a global operator< for your type.
Sep 26, 2012 at 8:14pm UTC
could you please give me an example how to use sort predicate for each container .
Sep 26, 2012 at 9:07pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
struct FairLess
{
bool operator ()(const Adjacent &m, const Adjacent &n){
return m.fare1 < n.fare1;
}
};
struct DistanceLess
{
bool operator ()(const Adjacent &m, const Adjacent &n){
return m.distance1 < n.distance1;
}
};
queue<Adjacent> myQueue;
std::sort(myQueue.begin(),myQueue.end(),FairLess); //Sorted by fair
Did not compile any of this code
Last edited on Sep 26, 2012 at 9:08pm UTC
Sep 26, 2012 at 10:11pm UTC
Great HELP Folks !!!
I tried second approach it worked smoothly ... Thanks a ton .. I am done with the assignment :)
With the first one I was facing error..like Fairless not declared in this scope while passing in sort method.
....
Sep 27, 2012 at 12:39am UTC
Could any one please post some link where I can read about the functionality of the same . !
I just used it but not clear how it is working.