gpt4 book ai didi

c++ - 在对 vector 中查找函数时出错

转载 作者:行者123 更新时间:2023-11-28 00:32:57 24 4
gpt4 key购买 nike

在这个函数中,我在 vector 缓存中搜索 pair.first。 vector 是:

vector<pair<double,unsigned int> > cache;

我用于查找函数的自定义函数是:

struct comp
{
comp(double const& s) : _s(s) { }

bool operator () (pair<double, unsigned int> const& p)
{
return (p.first == _s);
}

double _s;
};

我将查找函数称为:

it = find(cache.begin(),cache.end(),comp(value));

在编译时,我遇到了很多错误。前几行是:

In file included from /usr/include/c++/4.6/algorithm:63:0, from my_class.hpp:5, from main.cpp:5: /usr/include/c++/4.6/bits/stl_algo.h: In function ‘RandomAccessIterator std::_find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator*, std::vector > >, _Tp = MyCode::MyClass::comp]’: /usr/include/c++/4.6/bits/stl_algo.h:4326:45: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator*, std::vector > >, _Tp = MyCode::MyClass::comp]’ my_class.hpp:76:53: instantiated from here /usr/include/c++/4.6/bits/stl_algo.h:162:4: error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = std::pair*, _Container = std::vector >, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = std::pair& == __val’

我该如何解决这个错误?

最佳答案

您传递的是谓词,而不是值,因此您需要 find_if(),而不是 find()

关于c++ - 在对 vector 中查找函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22127095/

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