Skip to main content
gamedev.net
🔒 Locked

Issue with set comparators

Started by Premandrake Nov 29, 2002 at 2:23 PM 5 replies 1.2k views
Original Post
Premandrake
Premandrake
Okay, first the issue is I want to use a set with a custom comparator, which is normally fine. Normally something like this would work fine:
  
struct CompFunc
{
  bool operator()(const int *p1,const int *p2)
  {
    // Do stuff with p1 and p2

  }
};

std::set<int*,CompFunc> tada;
  
However, what I''m looking to do is inside my comparison function I need to use data from a class to do the comparison. That proved to be more complicated that I thought. I ended up doing something like this:
  
struct CompFunc
{
  bool operator()(const int *p1,const int *p2)
  {
    // Do stuff with p1 and p2

  }
  static int dataNeeded;
};

int CompFunc::dataNeeded;

CompFunc::dataNeeded = value;
std::set<int*,CompFunc> tada;
  
This feels wrong to me, but I''m having a tough time coming up with a nice encapsulated way to do this. Any suggestions?
Premandrake
Premandrake
Ah! Thank you very much, that solves my problems very well .
sark
sark
quote:
Original post by Kylotan


    
struct CompFunc
{
CompFunc(int data) : dataNeeded(data) {}

bool operator()(const int *p1,const int *p2)
{
// Do stuff with p1 and p2

}

private:
int dataNeeded;
};

CompFunc comparison(value);
std::set<int*,comparison> tada;



That wont work. "comparison" is a variable which is only known at runtime - std::set requires a type for its comparison functor.
joanusdmentia
joanusdmentia
well, if you know what your value will be at compile-time you could just use a template parameter:


  
template<unsigned int dataNeeded>
struct CompFunc
{
bool operator()(const int *p1,const int *p2)
{
// Do stuff with p1 and p2

}
};

std::set< int*,CompFunc<value> > tada;
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
Premandrake
Premandrake
Indeed, well done Sark, you can also tell I didn't test that out . I gave up on it actually as I was unable to arrive at a solution that worked across threads, that has now been remedied though.

Joanusdmentia, unfortunately the values are not known at compile time (and are not integral types also), so template parameters do not solve the problem, thanks for the suggestion though!

Thanks again Kylotan, I've actually implemented that now and it works great.

Edit: wow, bad spelling again...

[edited by - premandrake on December 2, 2002 10:37:47 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.