地图上的find_if问题

时间:2011-05-06 07:59:00

标签: c++ boost

我正在尝试在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;

执行此代码的结果是Not 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有关,但我不确定如何解决这个问题。

2 个答案:

答案 0 :(得分:3)

以下是您可以做的事情:

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

答案 1 :(得分:1)

正如其他人所说,您的代码已经适用于我(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 docs