使用 boost phoenix,如何使用starts_with 调用find_if 调用?
我试图在结构向量中找到一个元素。该代码在以区分大小写的方式搜索时有效。当我尝试将其增强为不区分大小写时,我遇到了两个问题。
简单地包含
boost/algorithm/string.hpp
会破坏之前工作的 VS2010 构建。错误是“'boost::phoenix::bind':对重载函数的不明确调用”。在 Xcode 中构建正常。有什么方法可以消除绑定的歧义吗?我想我在第二个(注释掉的)find_if 行中添加了 istarts_with 调用,语法错误。我从 phoenix 标头中收到错误消息“错误:没有名为‘type’的类型”。假设问题 #1 可以得到解决,我应该如何更正这一行?
谢谢!
代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp> // This include breaks VS2010!
#include <boost/phoenix/bind.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
using namespace boost::phoenix;
using boost::phoenix::arg_names::arg1;
using boost::istarts_with;
using std::string;
using std::cout;
// Some simple struct I'll build a vector out of
struct Person
{
string FirstName;
string LastName;
Person(string const& f, string const& l) : FirstName(f), LastName(l) {}
};
int main()
{
// Vector to search
std::vector<Person> people;
std::vector<Person>::iterator dude;
// Test data
people.push_back(Person("Fred", "Smith"));
// Works!
dude = std::find_if(people.begin(), people.end(), bind(&Person::FirstName, arg1) == "Fred");
// Won't build - how can I do this case-insensitively?
//dude = std::find_if(people.begin(), people.end(), istarts_with(bind(&Person::FirstName, arg1), "Fred"));
if (dude != people.end())
cout << dude->LastName;
else
cout << "Not found";
return 0;
}
I'm trying to find an element in a vector of structs. The code works when searching in a case-sensitive manner. When I try enhancing it to be case-insensitive, I run into two issues.
Simply including
boost/algorithm/string.hpp
breaks the previously working VS2010 build. The error is "'boost::phoenix::bind' : ambiguous call to overloaded function". Builds OK in Xcode. Any way to disambiguate the bind?I guess I've got the syntax wrong in the second (commented out) find_if line, adding the istarts_with call. I get errors from the phoenix headers saying "error: no type named 'type'". Assuming issue #1 can be fixed, how should I correct this line?
Thanks!
Code:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp> // This include breaks VS2010!
#include <boost/phoenix/bind.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
using namespace boost::phoenix;
using boost::phoenix::arg_names::arg1;
using boost::istarts_with;
using std::string;
using std::cout;
// Some simple struct I'll build a vector out of
struct Person
{
string FirstName;
string LastName;
Person(string const& f, string const& l) : FirstName(f), LastName(l) {}
};
int main()
{
// Vector to search
std::vector<Person> people;
std::vector<Person>::iterator dude;
// Test data
people.push_back(Person("Fred", "Smith"));
// Works!
dude = std::find_if(people.begin(), people.end(), bind(&Person::FirstName, arg1) == "Fred");
// Won't build - how can I do this case-insensitively?
//dude = std::find_if(people.begin(), people.end(), istarts_with(bind(&Person::FirstName, arg1), "Fred"));
if (dude != people.end())
cout << dude->LastName;
else
cout << "Not found";
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要两次绑定才能使其工作。首先定义:
然后使用以下内容作为
find_if
的谓词:两条注释:
bind
,即使用boost: :phoenix::bind
。istw
的定义可能是不必要的,但我找不到替换它的正确方法。You'd need two binds to make it work. First define:
and then use the following as the predicate for the
find_if
:Two comments:
bind
, namely, useboost::phoenix::bind
.istw
is probably unnecessary but I could not find the right way to replace it.