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<intを持っていますoperator<、それは大丈夫ですが、その要素タイプがを持っている場合にのみ持っていvector<spatial>ます。クラスには何もないため、typedefにもありません。operator<operator<spatialwayoperator<

operator<クラス用にを作成spatialすれば大丈夫です。

spatialクラスを比較するべきではないと思うが、セットに含めることを主張する場合は、独自のコンパレーター(最初のパラメーターが2番目のパラメーターよりも小さい場合に返されるway2つのパラメーターを持つファンクター/ラムダ)を作成し、それを2番目のテンプレートとして渡すこともできます。セットのパラメーター。const way&true

とにかく、std::setその要素はソートされて保存されるため、それを使用するには、ある時点で要素の順序を定義する必要があります。

于 2013-01-20T12:05:05.920 に答える
1

ユースケースによっては、 も使用できますunordered_setoperator==そこにはと ハッシュ関数が必要です:

#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 に答える