Ritu Aggarwal
Ritu Aggarwal

Reputation: 23

I have a set of user defined dataType and using that set variable in a structure. I want to compare the values of set. How will I do that

using setIp = set<ipv4address>;

struct Test {

setIp IP1;
setIp IP2;

}

IP1 and IP2 can have more than 1 ipaddress. I want to use find function on setIp and for that need operator <. How would I overload it.

Upvotes: 0

Views: 33

Answers (1)

John Zwinck
John Zwinck

Reputation: 249163

You can do this:

bool operator<(const ipv4address& left, const ipv4address& right) {
    return left.TODO < right.TODO;
}

The TODO is there because I don't know what the members of your ipv4address are, but I'm sure you can figure that part out. If your comparison needs to consider multiple members (for example if you store ipv4address as four separate octets), use std::tie:

bool operator<(const ipv4address& left, const ipv4address& right) {
    return std::tie(left.o1, left.o2, left.o3, left.o4)
         < std::tie(right.o1, right.o2, right.o3, right.o4);
}

That means "if left.o1 is less than right.o1, left is less than right" and so on.

Either way, the operator needs to be declared before you declare your std::set<ipv4address>.

Upvotes: 1

Related Questions