将const对象放入std :: set

我知道STL容器中的对象有“可分配和可复制构造”的要求。例如,您不能将具有const成员的对象放入向量中。

#include <vector>
    
    // /home/doriad/Test/Test.cxx:3:8: error: non-static const member ‘const int MyClass::x’, can’t use default assignment operator
    
    // struct MyClass
    // {
    //   int const x;
    //   MyClass(int x): x(x) {}
    // };
    // 
    // int main()
    // {
    //   std::vector<MyClass> vec;
    //   vec.push_back(MyClass(3));
    //   return 0;
    // }

但是,我注意到它可以与std :: set一起使用:

#include <set>
    
    // Attempt 1
    struct MyClass
    {
      int const x;
      MyClass(int x): x(x) {}
      bool operator< (const MyClass &other) const;
    };
    
    bool MyClass::operator<(const MyClass &other) const
    {
      if(this->x < other.x)
      {
        return true;
      }
      else if (other.x < this->x)
      {
        return false;
      }
    
    }
        
    int main()
    {
      std::set<MyClass> container;
      container.insert(MyClass(3));
      return 0;
    }

这仅适用于某些编译器吗?还是可以这样做?

谢谢,

大卫