地图上的find_if有问题

Problem with find_if on map

本文关键字:if 有问题 find 地图      更新时间:2023-10-16

我正在尝试在 std::map 上使用 std::find_if,寻找与字符串匹配的特定对象,如下所示:

class MyString
{
public:
    MyString() {}
    MyString(const std::string& x) : m_x(x) {}
    const std::string& value() const
    {
            return m_x;
    }
private:
    std::string m_x;
};
std::map<int,MyString> squaresS;
std::map<int,MyString>::iterator iIt;
squaresS[1] = MyString("1");
squaresS[2] = MyString("4");
squaresS[3] = MyString("9");
const std::string sTarget = "4";
iIt = std::find_if(squaresS.begin(), squaresS.end(),
    boost::bind(std::equal_to<std::string>(),
        boost::bind(&MyString::value,
            boost::bind(&std::map<int,MyString>::value_type::second, _1)),
        sTarget));
if (iIt != squaresS.end())
    std::cout << "Found " << iIt->second.value() << std::endl;
else
    std::cout << "Not Found" << std::endl;

执行此代码的结果为"未找到";我预计会输出 Found 4。但是,如果我使用整数做大致相同的事情,那么它可以工作,即输出是 找到 4:

class MyInteger
{
public:
    MyInteger() {}
    MyInteger(int x) : m_x(x) {}
    int value() const
    {
        return m_x;
    }
private:
    int m_x;
};
std::map<int,MyInteger> squaresI;
std::map<int,MyInteger>::iterator sIt;
squaresI[1] = MyInteger(1);
squaresI[2] = MyInteger(4);
squaresI[3] = MyInteger(9);
int iTarget = 4;
sIt = std::find_if(squaresI.begin(), squaresI.end(),
    boost::bind(std::equal_to<int>(),
        boost::bind(&MyInteger::value,
            boost::bind(&std::map<int,MyInteger>::value_type::second, _1)),
        iTarget));
if (sIt != squaresI.end())
    std::cout << "Found " << sIt->second.value() << std::endl;
else
    std::cout << "Not Found" << std::endl;

我怀疑这与 std::equal_to 有关,但我不确定如何解决这个问题。

以下是您可以执行的操作:

class MyString
{
public:
    MyString() {}
    MyString(const std::string& x) : m_x(x) {}
    const std::string& value() const
    {
            return m_x;
    }
private:
    std::string m_x;
};
class mystringmatch
{
   MyString _target;
public:
   mystringmatch(const MyString& target):_target(target)
   {
   }
   bool operator()(const std::pair<int, MyString>& src) const
   {
      return src.second.value() == _target.value();
   }
};
int _tmain(int argc, _TCHAR* argv[])
{
   std::map<int,MyString> squaresS;
   std::map<int,MyString>::iterator iIt;
   squaresS[1] = MyString("1");
   squaresS[2] = MyString("4");
   squaresS[3] = MyString("9");
   const std::string sTarget = "4";
   iIt = std::find_if(squaresS.begin(), squaresS.end(), mystringmatch(sTarget));
   if (iIt != squaresS.end())
       std::cout << "Found " << iIt->second.value() << std::endl;
   else
       std::cout << "Not Found" << std::endl;
    return 0;
}

在我看来,这种代码使人们从C++转向其他语言。几乎不可能阅读。

sIt = std::find_if(squaresI.begin(), squaresI.end(),
    boost::bind(std::equal_to<int>(),
        boost::bind(&MyInteger::value,
            boost::bind(&std::map<int,MyInteger>::value_type::second, _1)),
        iTarget));

正如其他人所指出的,您的代码已经对我有用(VC++ 2010 SP1)。也就是说,可以进行一个微不足道的更改来减少嵌套bind的数量 - boost::bind的返回类型(与std::bind的返回类型不同)具有所有关系和逻辑运算符重载,包括operator==,消除(或至少减轻)对std::equal_to<>等适配器的需求。利用这一点将代码简化为:

typedef std::map<int, MyString> squares_t;
squares_t::const_iterator iIt = std::find_if(
    squaresS.begin(),
    squaresS.end(),
    boost::bind(
        &MyString::value,
        boost::bind(&squares_t::value_type::second, ::_1)
    ) == sTarget
);

有关详细信息,请参阅 Boost.Bind 文档。