我试图在结构向量中找到一个元素。该代码在以区分大小写的方式进行搜索时有效。当我尝试将其增强为不区分大小写时,我遇到了两个问题。
简单地包括
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;
}