将元素插入到std :: set会导致多个错误

mah*_*ood 2 c++ set

考虑以下课程

#include <set>
#include <vector>
using namespace std;
class foo {
public:
  struct spatial {
    bool block;
    char status;  // H, M, Z
  };

  typedef pair< int, vector<spatial> > way;  // tag + spatial vector
  typedef set< way > one_set;


  void bar() 
  {
     way theWay;
     theWay.first = 10;

     one_set theSet;
     one_set::iterator sit = theSet.end();
     if (theSet.size() == 16) {
        sit = theSet.begin();
     }
     theSet.insert(sit, theWay);
  }
};
Run Code Online (Sandbox Code Playgroud)

对于插入函数,我收到这些错误,我不知道这是什么意思

error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const foo::spatial'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility

error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const foo::spatial'   c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144

error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const foo::spatial'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144

error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const foo::spatial'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144

error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const foo::spatial'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144    

error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const foo::spatial'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144

error C2676: binary '<' : 'const foo::spatial' does not define this operator or a conversion to a type acceptable to the predefined operator    c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 3144
Run Code Online (Sandbox Code Playgroud)

坚持到这一点.感谢任何帮助.

Csq*_*Csq 8

您的way类型必须已operator<定义为在std::set.中使用.

std::pairoperator<如果这两种类型都有operator<.intoperator<,这是确定的,但vector<spatial>operator<只有当它的元素类型都有operator<.你的spatial班级没有,因此你的waytypedef也没有operator<.

operator<为你的spatial班级创建一个你没问题.

如果您认为spatial类不应该具有可比性但坚持在way集合中,您还可以创建自己的比较器(带有两个const way&参数的仿函数/ lambda,true如果第一个小于第二个,则返回)并将其作为第二个模板传递你的集合的参数.

无论如何,因为std::set存储其元素已排序,要使用它,您必须在某个时刻定义元素的顺序.