stl find_if和不区分大小写的字符串比较

时间:2013-05-01 09:41:11

标签: c++ string boost c++03

我有一个Models的向量,如下所示:

struct Model
{
    std::string mName;
    // .......
};

给定一个表示模型名称的字符串,我想知道是否可以在向量中找到其中一个模型。

现在我有这个:

std::string assetName = "monkey";
std::vector<Model>::iterator iter = std::find_if(mModels.begin(), mModels.end(), boost::bind(&Model::mName, _1) == assetName);

但是这不会对不区分大小写的字符串进行比较。因此,我阅读了boost/algorithm/string.hppboost::iequals,这是正确的。

这是我尝试使用它:

std::vector<Model>::iterator iter = std::find_if(mModels.begin(), mModels.end(), boost::iequals(boost::bind(&Model::mName, _1), assetName));

然而,这不会编译并报告数百行编译错误。我相信std :: find_if需要第三个param函数只有一个参数。

有一个简单的解决方法吗?

编辑:我忘了提到我不能使用C ++ 11,但我可以使用boost!

EDIT2:下面的答案似乎给了我这个编译错误,使用:

std::vector<Model>::iterator iter = std::find_if(mModels.begin(), mModels.end(), boost::bind(&boost::iequals<std::string, std::string>, boost::bind(&Model::mName, _1), assetName));


bind.hpp(69): error C2825: 'F': must be a class or namespace when followed by '::'
2>          bind\bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
2>          with
2>          [
2>              R=boost::_bi::unspecified,
2>              F=bool (__cdecl *)(const std::string &,const std::string &,const std::locale &)
2>          ]
2>          resourcemanifest.cpp(24) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
2>          with
2>          [
2>              R=boost::_bi::unspecified,
2>              F=bool (__cdecl *)(const std::string &,const std::string &,const std::locale &),
2>              L=boost::_bi::list2<boost::_bi::bind_t<const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &,boost::_mfi::dm<std::string,Model>,boost::_bi::list1<boost::arg<1>>>,boost::_bi::value<std::string>>
2>          ]

1 个答案:

答案 0 :(得分:4)

调用而不是绑定函数

boost::iequals(boost::bind(&Model::mName, _1), assetName)

绑定是一个红色的鲱鱼:这个目前不是一个仿函数,而是一个函数调用。

您(意外)尝试立即调用它,并使用布尔结果作为std::find_if的比较函数。当然,这是不正确的。

您绑定模型名称是正确的,但您仍然必须将实际调用绑定到iequals

以下是a prior example on the Boost users' mailing list - bind iequals的第一个Google结果。

尝试类似:

boost::bind(
   &boost::iequals<std::string,std::string>,
   boost::bind(&Model::mName, _1), // 1st arg to iequals
   assetName,                      // 2nd arg to iequals
   std::locale()                   // 3rd arg to iequals
)

请注意,此处无法进行模板参数推断;还注意到我们必须明确地提供boost::iequals的默认第三个参数,因为默认值不能为我们提供绑定函数时能够省略参数的魔力。

完整的工作测试用例:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/algorithm/string/predicate.hpp>

struct Model
{
    Model(const std::string& name) : name(name) {};
    std::string mName() const
    {
        return name;
    }

private:
    std::string name;
};

int main()
{
    std::vector<Model> mModels;
    mModels.push_back(Model("a"));
    mModels.push_back(Model("b"));
    mModels.push_back(Model("c"));

    const std::string assetName = "B";

    std::vector<Model>::iterator it = std::find_if(
        mModels.begin(),
        mModels.end(),
        boost::bind(
            &boost::iequals<std::string,std::string>,
            boost::bind(&Model::mName, _1),
            assetName,
            std::locale()
        )
    );

    assert(it != mModels.end());
    std::cout << it->mName() << std::endl; // expected: "b"
}

(看到它正常工作here。)


为什么原始代码有效?

boost::bind(&Model::mName, _1) == assetName中,与==一起使用 magic 时,运算符boost::bind会超载;虽然看起来就像你正在进行直接比较一样,但实际上并没有在std::find_if的参数中评估比较,而是推迟到以后(主要通过执行隐式bind你看不到的。)

在普通函数调用的情况下,例如boost::iequals,我们必须自己应用“魔法”,这就是上述内容。