gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:49:09 25 4
gpt4 key购买 nike

谓词函数:

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

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

最佳答案

你可能误解了 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);

关于c++ - 如何在 find_if 算法中使用谓词函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com