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



谓词函数:

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

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

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

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

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

在代码中,您正在调用谓词,而不是将其传递给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);

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

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

此外,谓词函数的签名不正确。它应该只接受一个参数,这个参数不是迭代器,而是集合/迭代器的值。将其作为常量引用可能也是一个好主意。

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

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

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

最新更新