How to create std :: set of structures

I need to create stl :: set structures. I write

stl::set <Point> mySet; //Point - name of structure

But then I try to add an instance of the structure to mySet

Point myPoint;
mySet.insert(myPoint);

There are several compilation errors (bug C2784, bug C2676). Can anyone give some advice?

1> C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ include \ xfunctional (125): error C2784: bool std :: operator <(const std :: vector <_Ty, _Ax> &; const std: : vector <_Ty, _Ax> &): could not cast the argument to the pattern "const std :: vector <_Ty, _Ax> &" from "const Point"

1> C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ VC \ include \ xfunctional (125): error C2676: binary "<": "const Point" does not define this operator or conversion to a type acceptable for the integrated operator

+4
1

std::set , . . , .

std::set - . std::less<Key> , Key - , ( Point). , operator <, . , , (std::less<Point> ), :

Point pt1(args);
Point pt2(args);

if (pt1 < pt2)  // <<=== this operation
    dosomething();

:

operator <

operator < Point. pt1 < pt2 , std::less<Point> . , x, y, :

struct Point
{
    int x,y;

    // compare for order.     
    bool operator <(const Point& pt) const
    {
        return (x < pt.x) || ((!(pt.x < x)) && (y < pt.y));
    }
};

, std::less<Point>. , , , .

struct CmpPoint
{
    bool operator()(const Point& lhs, const Point& rhs) const
    {
        return (lhs.x < rhs.x) || ((!(rhs.x < lhs.x)) && (lhs.y < rhs.y));
    }
};

std::set :

std::set<Point,CmpPoint> mySet;

-, : Point, .


operator <

, operator <. -. std::less<Point> .

bool operator <(const Point& lhs, const Point& rhs)
{
    return (lhs.x < rhs.x) || ((!(rhs.x < lhs.x)) && (lhs.y < rhs.y));
}

, -, , . : operator <, std::less<Point>. , , .


; operator <. , Point . , . , .

+7

Source: https://habr.com/ru/post/1666770/


All Articles