在 binary_search 和 replace_if 上使用 Lambda

Using Lambda on binary_search and replace_if

提问人:MrPermafrost 提问时间:7/12/2021 最后编辑:MrPermafrost 更新时间:7/12/2021 访问量:211

问:

我一直在努力解决这个问题,binary_search回答 if 语句是真是假。我过去没有遇到过这个问题,但是我在让它搜索我编造的结构中的 tempStr 时遇到了问题。我试图通过binary_search实现的是让它说出产品是否存在,并决定是否要费心去做一个 for 循环,或者不去实际获取产品的名称。我在执行时遇到错误,我不明白为什么。

我遇到的第二个问题是我想用用户输入的价格替换向量中已有的价格,但这会产生与binary_search类似的错误。这是我天真地试图实现这一目标时所做的,任何和所有的帮助都将不胜感激。

如果要求我这样做,将澄清。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct grocery{
    string name;
    string expdate;
    string price;
};

int main(){
    grocery tempProd;
    string tempStr;
    vector<grocery> produce;
    
    cout << "What grocery product are you looking for?";
    getline(cin, tempStr);
    if(binary_search(produce.begin(), produce.end(), [&] (const grocery &p) {return p.name == tempStr;})){
        cout << "Product has been found!" << endl;
        for(int i= 0; i<produce.size(); i++){
            if(produce[i].name == tempStr){
                string tempStr2;
                cout << "What is going to be the new price of the product?";
                getline(cin, tempStr2);
                replace_if(produce.begin(), produce.end(), produce[i].price,
                    [&] (const grocery &p) {return p.price == tempStr2;});

            }
        }
        
    }
    else{
        cout << "Product does not exist." << endl;
    }
}

为此,我主要搞砸了 lambda,但它并没有给我带来太多成功,如果有的话。指针似乎也是我的答案,但这对我也没有多大好处,我似乎发现自己回到了这里,在这个地方。

以下是结构体的设置方式,以及向量的设置方式,因为我知道这两者都很重要。

错误

In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h: In instantiation of 'bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Tp = main()::<lambda(const body&)>]':
teste.cpp:45:117:   required from here
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: error: no match for 'operator<' (operand types are 'const main()::<lambda(const body&)>' and 'body')
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:64,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:454:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)'      
  454 |     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:454:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::pair<_T1, _T2>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:331:5: note: candidate: 'template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)'
  331 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:331:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::reverse_iterator<_Iterator>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:369:5: note: candidate: 'template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)'
  369 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:369:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::reverse_iterator<_Iterator>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1163:5: note: candidate: 'template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)'
 1163 |     operator<(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1163:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::move_iterator<_IteratorL>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1169:5: note: candidate: 'template<class _Iterator> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)'
 1169 |     operator<(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:1169:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::move_iterator<_IteratorL>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6226:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)'
 6226 |     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6226:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6239:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)'
 6239 |     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6239:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6251:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)'
 6251 |     operator<(const _CharT* __lhs,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:6251:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   mismatched types 'const _CharT*' and 'main()::<lambda(const body&)>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:46,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:208:3: note: candidate: 'bool std::operator<(const std::error_code&, const std::error_code&)'
  208 |   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
      |   ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:208:31: note:   no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'const std::error_code&'
  208 |   operator<(const error_code& __lhs, const error_code& __rhs) noexcept
      |             ~~~~~~~~~~~~~~~~~~^~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:282:3: note: candidate: 'bool std::operator<(const std::error_condition&, const std::error_condition&)'
  282 |   operator<(const error_condition& __lhs,
      |   ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\system_error:282:36: note:   no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'const std::error_condition&'
  282 |   operator<(const error_condition& __lhs,
      |             ~~~~~~~~~~~~~~~~~~~~~~~^~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\vector:67,
                 from teste.cpp:3:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1905:5: note: candidate: 'template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)' 1905 |     operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_vector.h:1905:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\algorithm:62,
                 from teste.cpp:5:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2259:39: note:   'const main()::<lambda(const body&)>' is not derived from 'const std::vector<_Tp, _Alloc>'
 2259 |       return __i != __last && !(__val < *__i);
      |                                ~~~~~~~^~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h: In instantiation of 'void std::replace_if(_FIter, _FIter, _Predicate, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Predicate = std::__cxx11::basic_string<char>; _Tp = main()::<lambda(const body&)>]':
teste.cpp:53:82:   required from here
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:4401:12: error: no match for call to '(std::__cxx11::basic_string<char>) (body&)'
 4401 |  if (__pred(*__first))
      |      ~~~~~~^~~~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:4402:13: error: no match for 'operator=' (operand types are 'body' and 'const main()::<lambda(const body&)>')
 4402 |    *__first = __new_value;
      |    ~~~~~~~~~^~~~~~~~~~~~~
teste.cpp:9:8: note: candidate: 'body& body::operator=(const body&)'
    9 | struct body{
      |        ^~~~
teste.cpp:9:8: note:   no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'const body&'
teste.cpp:9:8: note: candidate: 'body& body::operator=(body&&)'
teste.cpp:9:8: note:   no known conversion for argument 1 from 'const main()::<lambda(const body&)>' to 'body&&'
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:71,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Value = const main()::<lambda(const body&)>]':
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:979:14:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = 
__gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Tp = main()::<lambda(const body&)>; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algo.h:2257:22:   required from 'bool std::binary_search(_FIter, _FIter, const _Tp&) [with _FIter = __gnu_cxx::__normal_iterator<body*, std::vector<body> >; _Tp = main()::<lambda(const body&)>]'
teste.cpp:45:117:   required from here
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h:65:22: error: no match for 'operator<' (operand types are 'body' and 'const main()::<lambda(const body&)>')
   65 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:915:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)'
  915 |     operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:915:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:71,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h:65:22: note:   'body' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
   65 |       { return *__it < __val; }
      |                ~~~~~~^~~~~~~
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:67,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:922:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)'
  922 |     operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_iterator.h:922:5: note:   template argument deduction/substitution failed:
In file included from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:71,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,
                 from c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,
                 from teste.cpp:1:
c:\cpp\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\predefined_ops.h:65:22: note:   'body' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
   65 |       { return *__it < __val; }

编辑:是的,我知道这个例子还没有杂货店,但是有没有杂货店并没有引起问题,因为我正在处理文本文件中的信息,这实际上是导致我遇到的问题的最低限度。

C 可视化-C++ C ++-标准库

评论

0赞 Paul Sanders 7/12/2021
执行时出现错误请问什么错误?此外,我们需要一个最小的可重现示例
0赞 MrPermafrost 7/12/2021
提供了最小可重现的示例,以及我得到的错误。

答:

0赞 Paul Sanders 7/12/2021 #1

即使与相关的编译器错误已修复(这有点棘手,因为搜索 a 而不是 a),它也不会帮助您。这是因为需要对向量进行排序才能正常工作,而这里并非如此。binary_searchbinary_searchgrocerystringbinary_search

因此,请将其替换为 std::find_if,它没有此要求:

auto it = std::find_if (produce.begin(), produce.end(), [&] (const grocery &g) {return g.name == tempStr;});

这样做意味着您不需要再次在向量中搜索匹配项,因为您已经有一个迭代器指向它。相反,您可以执行以下操作:

if (it != produce.end()){
    cout << "Product has been found!" << endl;
    cout << "What is going to be the new price of the product?";
    string tempStr2;
    getline(cin, tempStr2);
    it->price = tempStr2;
}
else{
    cout << "Product does not exist." << endl;
}

总而言之,这是一个更好的解决方案。

现场演示

评论

0赞 MrPermafrost 7/12/2021
谢谢你!我之前尝试过一些带有“find_if”的东西,但似乎没有任何效果,这就是为什么我试图用 for 循环来替换它的原因,非常感谢!