# Help with STL List

This topic is 3695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I can't seem to my list working properly. It runs but gives me an error saying "invalid operator<" when it hits the last line in Visual Studio. The first two inserts work fine. All I really want is to be able to store unique config classes into the list for easy searching. Any help is appreciated! Thanks!
class config {
public:
int data;
bool operator<(const config &r) const {
return (!(this->data == r.data));
}
};

queue<config> nextMoves;
set<config> uniqueConfig;
pair<set<config>::iterator,bool> ret;

ret = uniqueConfig.insert(nextMoves.front());
ret = uniqueConfig.insert(nextMoves.front());
nextMoves.pop();
ret = uniqueConfig.insert(nextMoves.front());

##### Share on other sites
This should fix it:

class config {public:     int data;      	bool operator<(const config &r) const {		return this->data < r.data;	}};

The error was self-explanatory: the operator< was invalid because it needed to respect strict weak ordering.

##### Share on other sites
Why would you write operator< like that [smile]

It is more effort than the more natural:
bool operator<(const config &r) const{    return (this->data < r.data);}

##### Share on other sites
Quote:
 Original post by rip-offWhy would you write operator< like that [smile]It is more effort than the more natural:*** Source Snippet Removed ***

I figured it needed to use the < to compare and since all I really want is to use it to check unique values, so I thought it would've been be ok to change it to compare values instead.

So I guess my actual question is how would I use the set only for its ability to check for duplicates when using my config class?

I'm trying to make it as modular as possible since the config class will change while the code below that, stays the same. Could I just redefine the == operate to what my <operator was? or is there a better alternative besides the set?

Thanks!

##### Share on other sites
Quote:
 Original post by cloud_171I figured it needed to use the < to compare

It does.

Quote:
 and since all I really want is to use it to check unique values, so I thought it would've been be ok to change it to compare values instead.

No. It asks for an operator< for a reason. If all it needed was "are these things equal", then you would be asked to implement an operator that actually has that meaning: i.e. operator!=.

What's going on is that the set stores the values internally in a sorted order, typically in a binary search tree. This is why it's able to answer the question "is this value in the set?" quickly: it just traverses one branch of the tree, instead of checking every value in order.

Quote:
 So I guess my actual question is how would I use the set only for its ability to check for duplicates when using my config class?

That's kind of like asking "how do I read a book without looking at the pictures?". Just don't look at the pictures.

Quote:
 I'm trying to make it as modular as possible since the config class will change while the code below that, stays the same. Could I just redefine the == operate to what my

No. The objects have to be compared and ordered. That's how the container works.

I don't understand why you would resist this. As was already pointed out to you, the correct implementation is easier to write than what you had. It will, per comparison, run just as fast, too. Plus it will actually work.

I can't even begin to understand what you're thinking about "modularity" here.