C++ stl find_if和不区分大小写的字符串比较
我有一个C++ stl find_if和不区分大小写的字符串比较,c++,string,boost,c++03,C++,String,Boost,C++03,我有一个模型的向量,如下所示: struct Model { std::string mName; // ....... }; 给定一个表示模型名称的字符串,我想看看是否可以在向量中找到一个模型 现在我有这个: std::string assetName = "monkey"; std::vector<Model>::iterator iter = std::find_if(mModels.begin(), mModels.end(), boost::bind(&a
模型的向量,如下所示:
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);
然而,它并没有编译并报告大约一百行的编译错误。我相信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> ]
std::vector::iterator iter=std::find_if(mModels.begin(),mModels.end(),boost::bind(&boost::iequals,boost::bind(&Model::mName,_1),assetName));
bind.hpp(69):错误C2825:“F”:后跟“:”时必须是类或命名空间
2> bind\bind_template.hpp(15):请参阅正在编译的类模板实例化“boost::_bi::result_traits”的参考
2> 与
2> [
2> R=增压::bi::未指定,
2> F=bool(uu cdecl*)(常量std::string&,常量std::string&,常量std::locale&)
2> ]
2> cpp(24):请参阅对正在编译的类模板实例化“boost::_bi::bind_t”的引用
2> 与
2> [
2> R=增压::bi::未指定,
2> F=bool(uu cdecl*)(常量std::string&,常量std::string&,常量std::locale&),
2> L=boost::_bi::列表2
2> ]
调用而不是绑定函数
绑定是一个转移视线的问题:它当前不是一个函子,而是一个函数调用
您(无意中)试图立即调用它,并将布尔结果用作std::find_if
的比较函数。当然,这是不对的
您绑定模型名称是正确的,但仍然需要将实际调用绑定到iequals
这是第一个谷歌搜索绑定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::bind(&Model::mName,_1)==assetName
中,当与boost::bind
一起使用时,操作符=
被重载以执行魔术;虽然看起来像是在进行直接比较,但比较实际上并不是在std::find_if
的参数中计算的,而是推迟到以后(基本上是通过执行您看不到的隐式bind
)
但是,在正常函数调用的情况下,比如对boost::iequals
,我们必须自己应用“魔法”,这就是上面所说的;我似乎得到了一些编译错误,虽然与此相关,更新了主question@KaiserJohaan:我的错;虽然iequals
的第三个参数“locale”有一个默认值,但在绑定时它并不有用。更新了我的答案。
boost::iequals(boost::bind(&Model::mName, _1), assetName)
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
)
#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"
}