6

マップ内の値を見つけるための私のコードは次のとおりです。

bool myclass::getFreqFromCache( plVariablesConjunction& varABC, vector<plFloat>& freq )
{
std::map<plVariablesConjunction, std::vector<plFloat>>::iterator freqItr;
    freqItr = freqCache.find(varABC);

    if (freqItr != freqCache.end())
        {
        freq = freqItr->second;
        return true;
        }
 }

"PlVariablesConjunction" は ProBT ライブラリ データ型です。演算子「==」が含まれており、2 つの変数が同じである場合は true を返し、それ以外の場合は false を返します。

エラーは次のとおりです。

C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const plVariablesConjunction' (or there is no acceptable conversion)
1>          E:\ProBT22\probt-spl-2.2.0-expires-20121130-vc10-dynamic-release\include\plSymbol.h(71): could be 'bool operator <(const plSymbol &,const plSymbol &)' [found using argument-dependent lookup]
1>          while trying to match the argument list '(const plVariablesConjunction, const plVariablesConjunction)'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1>          with
1>          [
1>              _Ty=plVariablesConjunction
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=plVariablesConjunction
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1>          with
1>          [
1>              _Kty=plVariablesConjunction,
1>              _Ty=std::vector<plProbValue>,
1>              _Pr=std::less<plVariablesConjunction>,
1>              _Alloc=std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,
1>              _Mfl=false
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1>          ]
1>          e:\probt22\work\yasin\testmmhcfinalversion\testmmhc_mi_probt_sw\mmhc\slidingWindow.h(55) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=plVariablesConjunction,
1>              _Ty=std::vector<plProbValue>
1>          ]
4

3 に答える 3

13

std::map(通常) 二分探索木として実装され、ほとんどの場合、赤黒木です。ツリー内の正しい位置を見つけるには、キー値の線形順序を定義する必要があります。そのため、挿入されたキー値std::mapを呼び出そうとします。operator<

あなたのクラスは提供していませんoperator<。クラスに定義するかoperator<、テンプレートに比較関数を提供します: std::map<plVariablesConjunction, std::vector<plFloat>, my_comparison_function>.

于 2012-07-25T10:49:58.617 に答える
5

マップ クラスを使用するには、テンプレートに 2 つ、場合によっては 3 つの型が必要です。

std::map <key_type, data_type, [comparison_function]>

比較関数を提供するか、キー クラスで < 演算子をオーバーロードする必要があります。

比較関数が角かっこで囲まれていることに注意してください。これは、key_type に小なり演算子 < が定義されている限り、オプションであることを示しています。

于 2012-07-25T10:49:39.820 に答える
4

operator==map<> は、挿入された値のチェックには使用しません。operator<キー値の比較が必要です。

于 2012-07-25T10:46:19.440 に答える