首页 文章

模板比较迭代器

提问于
浏览
1

我有一个声明如下的类比较器:

template <typename _Type, typename _Comparator = std::equal_to<typename _Type::value_type> >
        class CSearch {
public:    
    CSearch() : comp(_Comparator()) {
    };

    CSearch(const _Comparator &_cmp) : comp(_cmp) {
    };
    void Add(int id, const _Type & needle);
    set<int> Search(const _Type & hayHeap) const;    
protected:
    _Comparator comp;
    vector<pair<int, _Type> > m_Db;
};

template <typename _Type, typename _Comparator>
void CSearch<_Type, _Comparator>::Add(int id, const _Type& needle) {
    m_Db.push_back(pair<int, _Type>(id, needle));
}

template <typename _Type, typename _Comparator>
set<int> CSearch<_Type, _Comparator>::Search(const _Type & hayHeap) const {
    set<int> s_search; 
    typename vector< pair<int, _Type> >::const_iterator it;
    typename _Type::const_iterator hayStackIterator;

    for (hayStackIterator = hayHeap.begin(); hayStackIterator != hayHeap.end(); ++hayStackIterator){        
        for(it=m_Db.begin(); it!=m_Db.end(); ++it){
            if(comp(*it, *hayStackIterator))
                s_search.insert(it->first);                
        }
    }
    return s_search;
}

//-------------------
int main(int argc, char** argv) {

    CSearch <string> test1;
    test1 . Add    ( 0, "hello" );
    test1 . Add    ( 1, "world" );
    test1 . Add    ( 2, "rld" );
    test1 . Add    ( 3, "ell" );
    test1 . Add    ( 4, "hell" );
    printSet ( test1 . Search ( "hello world!" ) );
    // 0, 1, 2, 3, 4
    printSet ( test1 . Search ( "hEllo world!" ) );
    // 1, 2

    CSearch <string, bool (*)(const char &, const char &)> test2 ( upperCaseCompare );
    test2 . Add    ( 0, "hello" );
    test2 . Add    ( 1, "world" );
    test2 . Add    ( 2, "rld" );
    test2 . Add    ( 3, "ell" );
    test2 . Add    ( 4, "hell" );
    printSet ( test2 . Search ( "HeLlO WoRlD!" ) );
    // 0, 1, 2, 3, 4
    printSet ( test2 . Search ( "hallo world!" ) );
    // 1, 2

    CSearch <string, CharComparator> test3 ( CharComparator ( false ) );
    test3 . Add    ( 0, "hello" );
    test3 . Add    ( 1, "world" );
    test3 . Add    ( 2, "rld" );
    test3 . Add    ( 3, "ell" );
    test3 . Add    ( 4, "hell" );
    printSet ( test3 . Search ( "heLLo world!" ) );
    // 0, 1, 2, 3, 4
    printSet ( test3 . Search ( "Well, templates are hell" ) );
    return 0;

}

搜索功能不转换条件:

if(comp(*it, *hayStackIterator))...

你能帮我吗?

翻译错误:

main.cpp:101:44:从这里实例化main.cpp:55:13:错误:无法调用'(const std :: equal_to)(const std :: pair>&,const char&)'/ usr / include / c /4.6/bits/stl_function.h:205:12:注意:候选人是:/ usr / include / c /4.6/bits/stl_function.h:208:7:注意:bool std :: equal_to <_Tp > :: operator()(const _Tp&,const _Tp&)const [with _Tp = char] / usr / include / c /4.6/bits/stl_function.h:208:7:注意:来自'const的参数1没有已知的转换std :: pair>'to'conc char&'main.cpp:在成员函数'std :: set CSearch <_Type,_Comparator> :: Search(const _Type&)const [with _Type = std :: basic_string,_Comparator = bool( *)(const char&,const char&)]':main.cpp:112:44:从这里实例化main.cpp:55:13:错误:从类型'const std的表达式初始化'const char&'类型的引用无效:: pair>'main.cpp:在成员函数'std :: set CSearch <_Type,_Comparator> :: Search(const _Type&)const [with _Type = std :: basic_string,_Comparator = CharC omparator]':main.cpp:123:44:从这里实例化main.cpp:55:13:错误:不匹配调用'(const charComparator)(const std :: pair>&,const char&)'main . cpp:64:7:注意:候选者是:main.cpp:69:16:注意:bool CharComparator :: operator()(const char&,const char&)const main.cpp:69:16:注意:没有已知的转换参数1从'const std :: pair>'到'const char&'main.cpp:在成员函数'std :: set CSearch <_Type,_Comparator> :: Search(const _Type&)const [with _Type = std :: vector ,_Comparator = std :: equal_to]':main.cpp:134:52:从这里实例化main.cpp:55:13:错误:不匹配调用'(const std :: equal_to)(const std :: pair >&,const int&)'/ usr / include / c /4.6/bits/stl_function.h:205:12:注意:候选人是:/ usr / include / c /4.6/bits/stl_function.h:208:7:注意:bool std :: equal_to <_Tp> :: operator()(const _Tp&,const _Tp&)const [with _Tp = int] / usr / include / c /4.6/bits/stl_function.h:208:7:注意:没有来自'const的参数1的已知转换std :: pair>'to'const int&'main.cpp:在成员函数'std :: set CSearch <_Type,_Comparator> :: Search(const _Type&)const [with Type = std :: vector>, Comparator = std :: equal_to>]':main.cpp:145:53:从这里实例化main.cpp:55:13:错误:不匹配调用'(const std :: equal_to>)(const std :: pair >> &,const std :: basic_string&)'/ usr / include / c /4.6/bits/stl_function.h:205:12:注意:候选者是:/ usr / include / c /4.6/bits/stl_function.h:208: 7:注意:bool std :: equal_to <_Tp> :: operator()(const _Tp&,const _Tp&)const [with _Tp = std :: basic_string] / usr / include / c /4.6/bits/stl_function.h:208 :7:注意:参数1从'const std :: pair >>>到'const std :: basic_string&'没有已知的转换

1 回答

  • 1

    错误消息非常清楚 .

    if(comp(*it, *hayStackIterator))
    

    *it 的类型为 const std::pair<int, std::string>& ,但 *hayStackIterator 的类型为 const char& . 你无法比较它们 .

相关问题