如何在find_if算法中使用谓词功能?

时间:2019-12-27 10:55:01

标签: c++ predicate

谓词功能:

bool Schedule::predicateFunc(map<pair<string,int>,pair<string,Array> >::iterator it,string &a)
{
    return (it->first).first == a;
}

我必须使用谓词func的函数:

 void Schedule::studentSchedule() {
    string s,c;
    cout<<"Enter the student and course name to create schedule"<<endl;
    cin>>s>>c;
    list<string>::iterator studentLoc;
    map<pair<string,int>,pair<string,Array> >::iterator courseL;
    map<pair<string,int>,pair<string,Array> >::iterator location;

    studentLoc = find(getStudentList().begin(),getStudentList().end(),s);
    location = find_if(getMatchMap().begin(), getMatchMap().end(), predicateFunc(courseL,c) )       
    if(studentLoc != getStudentList().end() && location != getMatchMap().end())
        {
            cout<<"I found it"<<endl;
        }
        else
            cout<<"I cant found it"<<endl;
    }

当我尝试在此处使用谓词功能时:

location = find_if(getMatchMap().begin(), getMatchMap().end(), predicateFunc(courseL,c) )  

我收到这样的错误:

In file included from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:71,
                 from C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:61,
                 from C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:4:
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = bool]':
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:104:42:   required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<bool>]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:161:23:   required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = __gnu_cxx::__ops::_Iter_pred<bool>]'
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algo.h:3930:28:   required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::pair<std::__cxx11::basic_string<char>, int>, std::pair<std::__cxx11::basic_string<char>, std::array<int, 6> > > >; _Predicate = bool]'
C:\Users\Fatih\Desktop\clion\SchoolProject1\Schedule.cpp:25:93:   required from here
C:/PROGRA~1/MINGW-~1/X86_64~1.0-P/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/predefined_ops.h:283:11: error: expression cannot be used as a function
  { return bool(_M_pred(*__it)); }
           ^~~~~~~~~~~~~~~~~~~~

这里谓词功能的真正用法是什么?

2 个答案:

答案 0 :(得分:1)

您正在调用谓词功能,但必须提供对谓词功能的引用

location = find_if(getMatchMap().begin(), getMatchMap().end(), predicateFunc);

此外,谓词函数的签名不正确。它仅应使用一个参数,并且该参数不是迭代器,而是集合/迭代器的值。使其成为const-reference也是一个好主意。

bool Schedule::predicateFunc(const map<pair<string,int>,pair<string,Array> >::value_type& x);

如果您需要为谓词函数提供一个参数,则有几种选择:

  • 请勿使用单独的谓词函数,而应使用lambda表达式。
  • 使用std::bind()
  • 使用功能对象。

答案 1 :(得分:1)

您可能误解了Predicate的概念。它必须是一个使用集合中 个元素并返回bool的函数。现在将为范围中的每个元素调用此函数,直到它第一次返回true(请参阅here)。

在您的代码中,您正在调用谓词,而不是将其传递给find_if

此外,谓词的签名是错误的:它使用两个参数而不是一个。签名应该是

bool Schedule::predicateFunc(const map<pair<string,int>,pair<string,Array> >::value_type& x);

如果您要传递一个附加参数,例如要比较的字符串,您可以执行以下操作:

bool Schedule::compareName(map<pair<string,int>,pair<string,Array> >::value_type& x,string &a)
{
    return (x.first).first == a;
}

,然后在调用代码中:

std::string expected_name = "some name";
auto predicate = [&](auto& course) { return compareName(course, expected_name); };
location = find_if(getMatchMap().begin(), getMatchMap().end(), predicate);
相关问题