-2

How c++ set/map checks the equality of keys ?

for example in this example :

struct A
{
    int id , val;

    A( int _val = 0 , int _id = 0 ) 
    { val = _val , id = _id; }

    bool friend operator < ( const A &x , const A &y )
    {
        return x.val < y.val;
    }
};
set< A > s;

because we haven't written the == operator ?

bluemmb
  • 85
  • 1
  • 9
  • 2
    Possible duplicate of [std::set with user defined type, how to ensure no duplicates](http://stackoverflow.com/questions/1114856/stdset-with-user-defined-type-how-to-ensure-no-duplicates) – LogicStuff Aug 16 '16 at 11:25
  • Related: [set documentation](http://en.cppreference.com/w/cpp/container/set), [map documentation](http://en.cppreference.com/w/cpp/container/map). – molbdnilo Aug 16 '16 at 11:38

2 Answers2

6

it checks if (!(x < y) && !(y < x))

David
  • 1,510
  • 14
  • 20
Andrey Nekrasov
  • 456
  • 2
  • 12
1

operator== is not used by std::set. Elements a and b are considered equal iff !(a < b) && !(b < a)

Note: A set is probably inappropriate if you define equality in a different sense than ordering. Equality in set essentially means the two element will have the same place in sorted sequence of items.

Shravan40
  • 8,922
  • 6
  • 28
  • 48