I'm trying to insert a struct into a set within an ITK class.
The struct is defined within the class as follows:
struct PixType {
IndexType Index;
PixelType Value;
bool operator <(const PixType&rhs) {
return (double)Value < (double)rhs.Value;
}
};
Where IndexType and PixelType are the index and value for pixels in an image:
typedef typename TImage::IndexType IndexType;
typedef typename TImage::PixelType PixelType;
However, when I create my set and try to insert an element...
// Create set
std::set< PixType > MySet;
// Create something to insert into set
PixType LeftPixel;
LeftPixel.Index = qualIt.GetIndex( left );
LeftPixel.Value = qualIt.GetPixel( left );
// Try to insert into set
MySet.insert( LeftPixel ); // error: invalid operands to binary expression
...I get an invalid operands to binary expression error. I have also tried inlineing the < function outside of the struct, but I then get an error that the function cannot be defined here (within the method of the class).
In case it helps, here is a bigger picture of what I am trying to do. I would like to store a list of pixels in an image (both their indices and their values). I would like to be able to quickly find the pixel with the minimum value, and retrieve the index of that pixel. After some reading, it seemed that a set containing a struct that held index and value and which could be compared based on the value, would be the most efficient option. I have previously gotten the functionality I need by maintaining vectors of indices and values, and using find and min_element, but I have run into efficiency problems as the vectors get too large.
The operator needs to be constant, so that it can be applied to the constant elements of a set:
bool operator <(const PixType&rhs) const
^^^^^
Alternatively, you could implement it as a non-member:
bool operator <(const PixType&lhs, const PixType&rhs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With