2

考虑下面的类

#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

卡在这一点上。感谢任何帮助。

4

2 回答 2

8

您的way类型必须有一个operator<定义才能在std::set.

std::pairoperator<如果两种类型都有,则有operator<inthas operator<,没关系,但vector<spatial>只有operator<当它的元素类型 has 时 has operator<。您的spatial班级没有,因此您的waytypedef 也没有operator<

operator<为您的spatial班级创建一个,您就可以了。

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

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

于 2013-01-20T12:05:05.920 回答
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;
}
于 2013-01-20T12:26:09.747 回答