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

时间:2013-01-20 12:02:36

标签: 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);
  }
};

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

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

坚持这一点。感谢任何帮助。

2 个答案:

答案 0 :(得分:8)

您的way类型必须定义operator<才能在std::set中使用。

如果两种类型都有std::pair,则

operator<operator<intoperator<,没关系,但只有当vector<spatial>的元素类型为operator<operator<才有spatial。您的way课程没有,因此您的operator< typedef也没有operator<

为您的spatial课程创建spatial即可。

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

无论如何,因为{{1}}将其元素存储为已排序,要使用它,您必须在某个时刻定义元素的顺序。

答案 1 :(得分:1)

根据您的使用情况,您还可以使用unordered_set。你需要一个operator==和一个哈希函数:

#include <unordered_set>
#include <vector>
using namespace std;

class foo {
public:
  struct spatial {
    bool block;
    char status;  // H, M, Z

    bool operator== (spatial const & that) const {
       return block==that.block && status==that.status;
    }

    size_t hash() const {
       return status & static_cast<int>(block) << 9;
    }
  };

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

  struct way_hash {
    size_t operator()(const way &w) const{
      return w.first;
    }
  };

  typedef unordered_set< way, foo::way_hash > 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);
  }
};

int main() {
   foo f;
   f.bar();

   return 0;
}
相关问题