如何在 find_if 算法中使用谓词函数?

How can I use predicate function in find_if algorithm?

谓词函数:

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)); }
           ^~~~~~~~~~~~~~~~~~~~

这里谓词函数的真正用法是什么?

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

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

此外,您的谓词函数的签名不正确。它应该只有一个参数,这个参数不是迭代器,而是 collection/iterator 的值。使它成为常量引用可能也是一个好主意。

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

如果您需要为谓词函数提供参数,您有多种选择:

  • 不要使用单独的谓词函数,而是使用 lambda 表达式。
  • 使用std::bind().
  • 使用函数对象。

您可能误解了 Predicate 的概念。它必须是一个函数,它接受集合中的一个 个元素 和 returns 个 bool。现在为范围中的每个元素调用此函数,直到第一次 returns 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);