Stl find_if and case insensitive string comparison

I have a vector Modelsas shown below:

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

For the string representing the model name, I want to see if I can find one of the models in the vector.

Now I have this:

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

But that does not do random string comparisons. So I read about boost/algorithm/string.hpp, with boost::iequals, which does it right.

Here is my attempt to use it:

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

However, this does not compile or report several hundred lines of compilation errors. I believe std :: find_if expects a third param function with only one parameter.

Is there a simple solution for this?

EDIT: I forgot to mention that I can't use C ++ 11, but I can use boost!

EDIT2: The answer below seems to give me this compilation error using this:

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>          ]
+5
1

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

- : , .

() std::find_if. , .

, , iequals.

Boost - Google bind iequals.

- :

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"
}

(, .)


?

boost::bind(&Model::mName, _1) == assetName == boost::bind ; , , std::find_if, ( , bind, ).

, , , boost::iequals, "" , .

+4

All Articles