【问题标题】:Error in Find function in vector of pairs对向量中的查找函数错误
【发布时间】:2014-04-03 08:09:52
【问题描述】:

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

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));

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

在 /usr/include/c++/4.6/algorithm:63:0 包含的文件中, 来自 my_class.hpp:5, 来自 main.cpp:5: /usr/include/c++/4.6/bits/stl_algo.h: 在函数'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: 从 '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator*, std::vector > >, _Tp = MyCode::MyClass::comp]' my_class.hpp:76:53: 从这里实例化 /usr/include/c++/4.6/bits/stl_algo.h:162:4: 错误:'_first.中的'operator=='不匹配。_gnu_cxx::__normal_iterator<_iterator _container> ::operator* with _Iterator = std::pair*, _Container = std::vector >, __gnu_cxx::__normal_iterator<_iterator _container>::reference = std::pair& == __val'

我该如何解决这个错误?

【问题讨论】:

  • 很遗憾,您错过了实际上是错误的错误部分,而不是有关错误的一些额外信息。
  • 它仍然不存在。这可能是下一行。

标签: c++ vector compilation iteration std


【解决方案1】:

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

【讨论】:

  • 嘿,谢谢。解决了这个问题。 :)
【解决方案2】:

您的呼叫应使用std::find_if,而不是std::find

 it = find_if(cache.begin(),cache.end(),comp(value));
 if ( it != cache.end() )
 {


 }

【讨论】:

  • 哈!击败你 8 秒 :-)
猜你喜欢
  • 1970-01-01
  • 2020-08-22
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多