10

I am trying to insert a object Point2D into a Point2D set but i am not able to do it, it seems the set works for int and char but not for objects.

I need help to know how to insert objects into the set ??? Assuming i want to sort them by ascending order of x value

class Point2D
{
public:
    Point2D(int,int);
    int getX();
    int getY();

    void setX(int);
    void setY(int);

    double getScalarValue();

protected:
    int x;
    int y;
    double distFrOrigin;
    void setDistFrOrigin();
};


int main()
{
    Point2D abc(2,3);

    set<Point2D> P2D;
    P2D.insert(abc); // i am getting error here, i don't know why
}
4
  • 1
    You need to write operator< for Point2D Commented Nov 9, 2013 at 2:21
  • can you elaborate further , sorry i am quite new to c++ Commented Nov 9, 2013 at 2:23
  • A set is sorted. If it can't sort your objects then you can't put them in a set. The sorting is done using operator< or a user specified predicate. Commented Nov 9, 2013 at 2:23
  • Next time please provide the error messages you get. Commented Nov 9, 2013 at 2:31

3 Answers 3

22

You need to implement the operator< overload for your class. For instance, in your class, you can do:

friend bool operator< (const Point2D &left, const Point2D &right);

Then, outside your class:

bool operator< (const Point2D &left, const Point2D &right)
{
    return left.x < right.x;
}

Edit: As suggested by Retired Ninja, you can also implement this as a regular member-function within your class:

bool operator< (const Point2D &right) const
{
    return x < right.x;
}
Sign up to request clarification or add additional context in comments.

11 Comments

I seriously doubt that this functor's logic is sufficient.
The original poster said all he/she needed to do was sort based on x though. When std::set sorts uses this function, isn't that what it will do?
No need to make it a free function, it can just as easily be a member. Those references should also be const.
What the OP thinks he needs to do is irrelevant. The set's key type must satisfy the Strict Weak Ordering and a comparison function that considers only x ignores all the other members. Is it strictly valid? Off the top of my head, okay yes probably. Is it really what the OP wants? Doubtful. Is it the proper solution? No.
How does this work with object pointers, as in set<Point2D*>?
|
3

std::set<T> requires that std::less<T> is known for the value type T. This is so that it can order its elements, which is fundamental to how it works internally.

Fix this by defining bool operator<(const Point2D&, const Point2D&), with any logic you choose as long as it satisfies the Strict Weak Ordering.

This is a requirement of the type, but once you've done this you're good to go.

Comments

2

I believe a better way for C++ 11 or newer to define the order is to use custom functor, since std::set support that.

We can see set is defined in header <set> like this:

template<
    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>
> class set;

Hence, to compare based on x for example:

struct Point2DCmp
{
    bool operator() (Point2D& p1, Point2D& p2)
    {
        return p1.getX() < p2.getX();
    }
}

set<Point2D, Point2DCmp> P2D;

1 Comment

Why is it better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.