gpt4 book ai didi

c++ - 多映射中的 std::find_if 返回带有仿函数的错误

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:24 26 4
gpt4 key购买 nike

在下面的代码片段中,我尝试在多重映射中找到等于 myPairA.second 值的值,它对应于 int f = 0。但是在 std::find_if STL 算法中,这显示了一个错误:

 /usr/include/c++/5/bits/predefined_ops.h:234:30: error: no match for call to ‘(EqualFunctor<int>) (std::pair<const int, int>&)’
{ return bool(_M_pred(*__it)); }
^`

multimaps_2.cpp:12:10: note: candidate: bool EqualFunctor<T>::operator()(std::pair<const int, T*>) [with T = bool operator() (std::pair<const int, T*> myPair)

这是我的程序:(带有 std::find_if 的行会产生错误)请参阅下面的第二个版本

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

template <typename T>
class EqualFunctor
{
T *t_;
public:
EqualFunctor(T *t) : t_(t) {}
bool operator() (std::pair<const int, T*> myPair)
{ return myPair.second == t_; }
};

int main()
{
// h, i & j are duplicates of f and g
int f = 0, g = 1,
h = 0, i = 1, j = 1;

// declare five pairs
std::pair<const int, int> myPairA (1, f),
myPairB (2, g),
myPairC (3, h),
myPairD (4, i),
myPairE (5, j);

std::multimap<int, int> myMultimap;

// insert pairs above in multimap with the exception of myPairA

myMultimap.insert(myPairB);
myMultimap.insert(myPairC);
myMultimap.insert(myPairD);
myMultimap.insert(myPairE);

std::multimap<int, int>::iterator it;

// pointer to f = 0, since the EqualFunctor class accepts a pointer
int *ptrMyPairA = &myPairA.second;

// find in multimap the pair that is equal to f, ie myPairA.second
// with the EqualFunctor class
// Problem is here
it = std::find_if(myMultimap.begin(), myMultimap.end(),
EqualFunctor<int>(ptrMyPairA));

// print to screen
std::cout << (*it).second << std::endl;
return 0;
}

期望的输出是显示 0在对应于多重映射中第一次出现的重复项的屏幕上。 (myPairC.second 等于 int h = 0)

我已经尝试阅读与此相关的其他帖子 stackoverflow问题,但它并没有帮助我解决它。此外,我的问题与 stackoverflow 上的问题几乎相同。但它仍然无助于解决问题。谢谢

编辑:改变 std::pair<const int, int>std::pair<const int, int*>仍然报错:

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

template <typename T>
class EqualFunctor
{
T *t_;
public:
EqualFunctor(T *t) : t_(t) {}
bool operator() (std::pair<const int, T*> myPair)
{ return myPair.second == t_; }
};

int main()
{
// h, i & j are duplicates of f and g
int f = 0, g = 1,
h = 0, i = 1, j = 1;

int *ptrF = &f, *ptrG = &g,
*ptrH = &h, *ptrI = &i, *ptrJ = &j;
// declare five pairs
std::pair<const int, int*> myPairA (1, ptrF),
myPairB (2, ptrG),
myPairC (3, ptrH),
myPairD (4, ptrI),
myPairE (5, ptrJ);

std::multimap<int, int> myMultimap;

// insert pairs above in multimap with the exception of myPairA

myMultimap.insert(myPairB);
myMultimap.insert(myPairC);
myMultimap.insert(myPairD);
myMultimap.insert(myPairE);

std::multimap<int, int>::iterator it;

// find in multimap the pair that is equal to f, ie myPairA.second
// with the EqualFunctor class
// Problem is here
it = std::find_if(myMultimap.begin(), myMultimap.end(),
EqualFunctor<int>(myPairA.second));

// print to screen
std::cout << (*it).second << std::endl;
return 0;
}

报错no viable overloaded '='在 std::find_if 所在的行上。

最佳答案

0x499602D2 已经在评论中解释了问题,所以请等待他发布答案后再接受任何其他重播。

The element type of the map isn't pair<const int, int*>, 
it's pair<const int, int> as you have written.
Your functor takes the former which is why it doesn't work.

您可以在 Wandbox 上找到固定代码.我决定发布一个答案来展示固定代码,以及如何使用大括号初始化、lambda、auto 和构造函数推导来简化代码:

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

template <typename K, typename V>
class EqualFunctor
{
V t_;
public:
EqualFunctor(V t) : t_(t) {}
bool operator() (std::pair<K const, V> const& myPair)
{ return myPair.second == t_; }
};

int main()
{
// use C++17 constructor deduction
auto const myPairA = std::pair(1, 0);

auto const myMultimap = std::multimap<int,int>{ {2,1}, {3,0}, {4,1}, {5,1}};

// find in multimap the pair that is equal to f, ie myPairA.second
// with the EqualFunctor class
// Problem is here
auto it = std::find_if(myMultimap.begin(), myMultimap.end(),
EqualFunctor<int,int>(myPairA.second));

// print to screen
std::cout << it->second << std::endl;

// simpler with lambda
it = std::find_if(myMultimap.begin(), myMultimap.end(),
[&myPairA](auto const& x) {return x.second == myPairA.second;} );

// print to screen
std::cout << it->second << std::endl;

return 0;
}

关于c++ - 多映射中的 std::find_if 返回带有仿函数的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50358749/

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