C ++: errors when inserting struct into set

I am trying to paste struct

in set

in an ITK class.

The structure is defined internally class

as follows:

struct PixType {

    IndexType Index;
    PixelType Value;

    bool operator <(const PixType&rhs) {
        return (double)Value < (double)rhs.Value;
    }

};

      

Where IndexType

and PixelType

is the index and value for the pixels in the 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 am getting an error invalid operands to binary expression

. I also tried a inline

function <

outside of the structure, but then I get an error that the function cannot be defined here (inside a class method).

In case it helps, here is a more detailed picture of what I am trying to do. I would like to store a list of pixels in the image (both their indices and their values). I would like to be able to quickly find the pixel with the minimum value and get the index of that pixel. After some reading, it seemed that the most efficient option would be a set containing a structure containing an index and a value and which can be compared by value. I previously got the functionality I needed by maintaining vector

indices and values ​​and using find

and min_element

, but I ran into performance issues as vectors get too large.

+3


source to share


1 answer


The operator must be constant so that it can be applied to constant elements of the set:

bool operator <(const PixType&rhs) const
                                   ^^^^^

      



Alternatively, you can implement it as a non-member:

bool operator <(const PixType&lhs, const PixType&rhs)

      

+5


source







All Articles