Reputation: 1
I am trying to create a set of a structure but my code keeps getting errors, and I can't find anything online.
struct smove {
int src;
int dst;
};
int main()
{
smove moov;
moov.dst = 1;
moov.src = 2;
set<struct smove> moovs = {moov};
return 0;
}
Upvotes: 0
Views: 267
Reputation: 385104
Set's value type needs to be less-than comparable. That's how the container knows how elements relate to one another and in what order (including ensuring no duplicates).
Short story, make an operator<
for smove
.
The long story is, well, longer, because that operator is required to work in a certain way, but you can read up on that. For now, here's a simple example that uses std::tie
to get a legal ordering quickly:
#include <set>
#include <tuple>
struct smove
{
int src;
int dst;
};
bool operator<(const smove& lhs, const smove& rhs)
{
return std::tie(lhs.src, lhs.dst) < std::tie(rhs.src, rhs.dst);
}
int main()
{
smove moov;
moov.dst = 1;
moov.src = 2;
std::set<smove> moovs = {moov};
}
Upvotes: 4