1

When using C++ STL, we often need to compare the elements in a container, such as set, sort, priority_queue. My question is, when i define a new type of struct (or class, doesn't matter) without specifying any comparator or operator overloading, and somehow compare two struct objects, how will C++ compare them by default? E.g. by the value of address, or consider the whole struct as a big number, or use the lower four bytes? Or something else?

Neo
  • 1,031
  • 2
  • 11
  • 27
  • 1
    Why don’t you try it? Granted, trying something is often not a good way to get an authoritative answer in C++ (due to undefined and implementation-defined behaviour) but in this case it works just fine. – Konrad Rudolph Jul 19 '20 at 10:19
  • Related/dupe: [Why don't C++ compilers define operator== and operator!=?](https://stackoverflow.com/q/217911/10871073). – Adrian Mole Jul 19 '20 at 10:24
  • @KonradRudolph Well, I tried, but I still couldn't see the logic how C++ sort the structs. – Neo Jul 20 '20 at 07:19
  • 1
    @Neo That’s confusing: you should be getting a descriptive *compilation error*. Please share your code. – Konrad Rudolph Jul 20 '20 at 08:46

0 Answers0