簡體   English   中英

如何在 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);

此外,您的謂詞函數的簽名不正確。 它應該只需要一個參數,這個參數不是迭代器,而是集合/迭代器的值。 將其設為常量引用可能也是一個好主意。

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

如果您需要為謂詞函數提供參數,則有多種選擇:

  • 不要使用單獨的謂詞函數,而是使用 lambda 表達式。
  • 使用std::bind()
  • 使用函數對象。

您可能誤解了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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM